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);
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.
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);
setLeft()
for this modified script, please add it to after alignOnPage()
.If this was not the result what you want, I'm sorry.
About your additional question, how about the following sample script? In this sample, the following flow is run.
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);