Reputation: 69
I am trying to add multiple charts into Google slides, from a designated google sheet using apps script. So 1 of my slides will have 2 charts, and if I need to insert more than 2 charts, the slide should automatically duplicate itself and then more charts can be inserted. However I can't get the right chart to be inserted, and when it duplicates the slide, it seems to be inserting the same chart over and over again.
However I am running into a couple of issues that I need help with, I was not able to find the answer on my own.
Here is the code, and the issues I am running into below that:
function importpieCharts() {
var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/1tPc0KU2uYuN4rO32tW-s6lql_IW57kbDUdSR8ZSkVXY/edit#gid=0"; //make sure this includes the '/edit at the end
var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
var deck = SlidesApp.getActivePresentation();
var deckTitle = SlidesApp.getActivePresentation().getName();
var sheet = ss.getSheetByName(deckTitle);
var charts = sheet.getCharts();
var slides = deck.getSlides();
var pieChartTemplate = slides[1];
charts.forEach(function(chart){
if(chart.modify().getChartType() == "PIE"){
pieChartTemplate.duplicate();
var updateSlide = deck.getSlides()[1];
var pageElements = updateSlide.getPageElements();
pageElements.forEach(function(pageElement){
if(pageElement.getPageElementType() == "SHAPE"){
if(pageElement.asShape().getShapeType() == "RECTANGLE"){
pageElement.asShape().replaceWithSheetsChart(chart);
}}});
var presLength = deck.getSlides().length;
updateSlide.move(presLength-2);
}});
}
Please help out with this, or point me in the right direction please! any help is much appreciated:) Here are the relevant file links: Google slides: https://docs.google.com/presentation/d/1CyUkJ7S4eq00MRol3RzcdyAG1m6ovUFBK0H9fxfu-28/edit?usp=sharing Google Sheets: https://docs.google.com/spreadsheets/d/1tPc0KU2uYuN4rO32tW-s6lql_IW57kbDUdSR8ZSkVXY/edit?usp=sharing
disclaimer: I am a total novice, and any knowledge I have about apps script is completely self learnt in order to make my day job easier. I am of course, genuinely interested in the code and its applications
Upvotes: 1
Views: 1089
Reputation: 201428
I believe your goal as follows.
{{placeholder for chart}}
with the Spreadsheet chart in Google Slides.{{placeholder for chart}}
. Also, there are several charts in Google Spreadsheet.{{placeholder for chart}}
with each Spreadsheet chart in order of the charts.chart
is used for replacing. By this, each slide has the same 2 charts. I thought that this might be the reason of your issue.{{placeholder for chart}}
with each Spreadsheet chart in order of the charts, only one chart is required to be replaced at the inner loop. For this, I would like to propose to add a condition for checking to copy the template slide.When above points are reflected to your script, it becomes as follows.
function importpieCharts() {
var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/1tPc0KU2uYuN4rO32tW-s6lql_IW57kbDUdSR8ZSkVXY/edit#gid=0"; //make sure this includes the '/edit at the end
var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
var deckTitle = SlidesApp.getActivePresentation().getName();
var sheet = ss.getSheetByName(deckTitle);
var charts = sheet.getCharts();
var slides = deck.getSlides();
var pieChartTemplate = slides[1];
// I modified below script.
var copy = true;
var updateSlide;
charts.forEach(function(chart, idx, a) {
if(chart.modify().getChartType() == "PIE"){
if (copy) {
updateSlide = pieChartTemplate.duplicate();
var presLength = deck.getSlides().length;
updateSlide.move(presLength - 2);
copy = false;
}
var pageElements = updateSlide.getPageElements();
for (var i = 0; i < pageElements.length; i++) {
var pageElement = pageElements[i];
if (pageElement.getPageElementType() == "SHAPE" && pageElement.asShape().getShapeType() == "RECTANGLE") {
pageElement.asShape().replaceWithSheetsChart(chart);
break;
}
}
if (!pageElements.some(function(p) {return p.getPageElementType() == "SHAPE" && p.asShape().getShapeType() == "RECTANGLE"})) {
copy = true;
}
// When the following script is used, the template sheet is removed at the last loop.
// if (idx == a.length - 1) deck.getSlides().pop().remove();
}
});
}
if (idx == a.length - 1) deck.getSlides().pop().remove();
in above script.updateSlide.move(presLength - 2)
is modified to updateSlide.move(presLength)
, when the script is finished, the template slide is 2nd page of Google Slides.Upvotes: 2