I have set up a Google Apps Script to generate new templated slides from data contained in a spreadsheet. This data would be updated regularly. I plan to set a project trigger to generate new slides on a regular basis.
Right now, I avoid the problem of duplicating slides during triggered updates by simply removing previously generated slides (see code example below) but I would love to simply append new slides rather than deleting and regenerating (to preserve any edits to previously generated slides).
Any tips and tricks from more experienced Javascript developers for this wayward R coder on how to approach this problem?
function myFunction() {
var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/1RjA4BJ4IKyjlh_l6vNaC7Q8wgUet1eNBo4_KJiF1pbQ/edit";
var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
var deck = SlidesApp.getActivePresentation();
var sheet = ss.getSheetByName('test');
var values = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
var slides = deck.getSlides();
var templateSlide = slides[1];
var presLength = slides.length;
// remove previously generated slides
// TODO: figure out how to append rather than delete and regenerate
for (var i = 2; i < slides.length; i ){
slides[i].remove()
}
values.forEach(function(page){
if(page[0]){
templateSlide.duplicate();
slides = deck.getSlides();
newSlide = slides[2];
var shapes = (newSlide.getShapes());
shapes.forEach(function(shape){
shape.getText().replaceAllText('{{name}}',page[0]);
shape.getText().replaceAllText('{{value1}}',page[1]);
shape.getText().replaceAllText('{{value2}}',page[2]);
shape.getText().replaceAllText('{{value3}}',page[3]);
shape.getText().replaceAllText('{{value4}}',page[4]);
shape.getText().replaceAllText('{{value5}}',page[5]);
shape.getText().replaceAllText('{{value6}}',page[6]);
shape.getText().replaceAllText('{{value7}}',page[7]);
shape.getText().replaceAllText('{{value8}}',page[8]);
shape.getText().replaceAllText('{{value9}}',page[9]);
shape.getText().replaceAllText('{{value10}}',page[10]);
});
presLength = slides.length;
newSlide.move(presLength);
}
});
}
CodePudding user response:
Figured it out by updating the starting row by checking against presentation length!
function myFunction() {
var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/1RjA4BJ4IKyjlh_l6vNaC7Q8wgUet1eNBo4_KJiF1pbQ/edit";
var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
var deck = SlidesApp.getActivePresentation();
var sheet = ss.getSheetByName('test');
var slides = deck.getSlides();
var templateSlide = slides[1];
var presLength = slides.length;
// new data update //
var rowLength = sheet.getLastRow();
var startRow = 2;
// update startRow when not initial run
if(presLength != 2){
startRow = presLength;
}
// save newly generated data range
var values = sheet.getRange(startRow, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
values.forEach(function(page){
if(page[0]){
templateSlide.duplicate();
slides = deck.getSlides();
newSlide = slides[2];
var shapes = (newSlide.getShapes());
shapes.forEach(function(shape){
shape.getText().replaceAllText('{{name}}',page[0]);
shape.getText().replaceAllText('{{value1}}',page[1]);
shape.getText().replaceAllText('{{value2}}',page[2]);
shape.getText().replaceAllText('{{value3}}',page[3]);
shape.getText().replaceAllText('{{value4}}',page[4]);
shape.getText().replaceAllText('{{value5}}',page[5]);
shape.getText().replaceAllText('{{value6}}',page[6]);
shape.getText().replaceAllText('{{value7}}',page[7]);
shape.getText().replaceAllText('{{value8}}',page[8]);
shape.getText().replaceAllText('{{value9}}',page[9]);
shape.getText().replaceAllText('{{value10}}',page[10]);
});
presLength = slides.length;
newSlide.move(presLength);
}
});
}