I have this code to add content from Google sheets to Google slides which works great however I want the new slides to be added in the end of the presentation. I am not sure if I could use something like appendSlide()
somewhere around this area of the code let slide = masterSlide.duplicate();
Now it acts like this... Title slide, Template Slide, [NEW SLIDE], Slide 1, Slide 2
Desired outcome... Title slide, Template slide, Slide 1, Slide 2, [NEW SLIDE]
// Replace <INSERT_SLIDE_DECK_ID> wih the ID of your
// Google Slides presentation.
let masterDeckID = "SLIDE_DECK_ID";
// Open the presentation and get the slides in it.
let deck = SlidesApp.openById(masterDeckID);
let slides = deck.getSlides();
// The 2nd slide is the template that will be duplicated
// once per row in the spreadsheet.
let masterSlide = slides[1];
// Load data from the spreadsheet.
let dataRange = SpreadsheetApp.getActive().getSheetByName('Sheet1').getDataRange();
let sheetContents = dataRange.getValues();
// Save the header in a variable called header
let header = sheetContents.shift();
// Create an array to save the data to be written back to the sheet.
// We'll use this array to save links to the slides that are created.
let updatedContents = [];
// Reverse the order of rows because new slides will
// be inserted at the top. Without this, the order of slides
// will be the inverse of the ordering of rows in the sheet.
sheetContents.reverse();
// For every row, create a new slide by duplicating the master slide
// and replace the template variables with data from that row.
sheetContents.forEach(function (row) {
// Insert a new slide by duplicating the master slide.
let slide = masterSlide.duplicate();
// Populate data in the slide that was created
slide.replaceAllText("{{firstName}}", row[0]);
slide.replaceAllText("{{lastName}}", row[1]);
slide.replaceAllText("{{grade}}", row[2]);
// Create the URL for the slide using the deck's ID and the ID
// of the slide.
let slideUrl = `https://docs.google.com/presentation/d/${deck.getId()}/edit#slide=id.${slide.getObjectId()}`;
// Add this URL to the 4th column of the row and add this row
// to the data to be written back to the sheet.
row[3] = slideUrl;
updatedContents.push(row);
});
// Add the header back (remember it was removed using
// sheetContents.shift())
updatedContents.push(header);
// Reverse the array to preserve the original ordering of
// rows in the sheet.
updatedContents.reverse();
// Write the updated data back to the Google Sheets spreadsheet.
dataRange.setValues(updatedContents);
// Remove the master slide if you no longer need it.
//masterSlide.remove();
}```
CodePudding user response:
I'm not sure how you are dealing with preventing duplicates. However, I made the test by deleting all the information in the sheet before adding a new one.
Here are some suggestions to add the slide at the end of the presentation:
Replace:
// Open the presentation and get the slides in it.
let deck = SlidesApp.openById(masterDeckID);
let slides = deck.getSlides();
// once per row in the spreadsheet.
let masterSlide = slides[1];
With:
let deck = SlidesApp.openById(masterDeckID);
let masterSlide = deck.getSlides()[1];
And also the following change, replace:
let slide = masterSlide.duplicate();
With:
let slide = deck.appendSlide(masterSlide);
Reference: