0

I am trying to adjust the position of Google Apps Script chart on Google Slide. First, the chart is created on a spreadsheet and inserted into the slide. I tried to change the position to right side or center using setLeft() and alignOnPage() methods. But still, the image is positioned top left on the slide. Could someone please show me the proper way to place the chart on the slide? Thank you!

var chart2 = sheet.newChart().setChartType(Charts.ChartType.PIE).addRange(sheet.getRange(range2)).setOption('applyAggregateData',0).setPosition(5, 5, 0, 0).build();
sheet.insertChart(chart2);
shapes = slide.getShapes();
slide.insertSheetsChart(chart2).setLeft(0).alignOnPage(SlidesApp.AlignmentPosition.CENTER).setWidth(300).setHeight(185);
Tanaike
  • 181,128
  • 11
  • 97
  • 165
xcen
  • 652
  • 2
  • 6
  • 22
  • Have you tried using the `VERTICAL_CENTER` property instead of `CENTER` in the [`alignOnPage()`](https://developers.google.com/apps-script/reference/slides/page-element#alignOnPage(AlignmentPosition)) method? – MαπμQμαπkγVπ.0 Dec 12 '18 at 11:07

1 Answers1

1

In your situation, when alignOnPage() is used, at first, please resize the chart using setWidth() and setHeight(). Then use alignOnPage(SlidesApp.AlignmentPosition.CENTER). By this, you can put the resized chart to center of the slide.

When alignOnPage() is run before the chart is resized, the size before the resize is used. So when the chart is resized after the adjustment, the position you don't want is reflected.

Modified script:

From:
slide.insertSheetsChart(chart2)
  .setLeft(0)
  .alignOnPage(SlidesApp.AlignmentPosition.CENTER)
  .setWidth(300)
  .setHeight(185);
To:
slide.insertSheetsChart(chart2)
  .setWidth(300)
  .setHeight(185)
  .alignOnPage(SlidesApp.AlignmentPosition.CENTER);

Note:

  • If you want to use setLeft() for this modified script, please add it to after alignOnPage().

If this was not the result what you want, I'm sorry.

Added:

About your additional question, how about the following sample script? In this sample, the following flow is run.

  1. Set width and height of the chart.
  2. Set position of top and left.

Sample script:

var s = SlidesApp.getActivePresentation();
var w = 300;
var h = 185;
var ph = s.getPageHeight();
var pw = s.getPageWidth();

slide.insertSheetsChart(chart2)
  .setWidth(w)
  .setHeight(h)
  .setTop(ph - h)
  .setLeft(pw - w);
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you for the answer and for noticing my mistake. Can you suggest the way to position the chart **right bottom** on the slide? I only found methods `setLeft() ` , `setTop()` and `alignOnPage()` which are not helping me. – xcen Dec 13 '18 at 00:07
  • @Akira I'm really sorry for my incomplete answer. I added the sample script for your next question. Could you please confirm it? – Tanaike Dec 13 '18 at 00:18
  • It worked. Thank you. But I had to edit the code as below. `slide.insertSheetsChart(chart2) .setWidth(w) .setHeight(h) .alignOnPage(SlidesApp.AlignmentPosition.CENTER) .setTop(ph - h) .setLeft(pw - w);` – xcen Dec 13 '18 at 01:21