Copying Multiple Slides From A Master Slide Desk Using Google Apps Script
I have created a code which replaces placeholders on google slides. The starting point of this project is a google form. Once the google form has been submitted - then the relevant
Solution 1:
- You want to copy the 5, 7, and 9 pages (indexes of 4, 6 and 8) in the master Google Slides to other Google Slides.
- You want to achieve this using Google Apps Script.
I could understand like above. If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Flow:
- Retrieve all slides from the master Google Slides.
- Copy the required slide to the destination Google Slides in the loop.
Pattern 1:
Sample script:
Before you run the script, please set the variables.
function myFunction() {
var copyPageNumbers = [5, 7, 9]; // Please set the page number. This is not the index. So the 1st page is 1.
var masterGoogleSlidesId = "###"; // Please set the master Google Slides ID.
var destinationGoogleSlidesId = "###"; // Please set the destination Google Slides ID.
var offset = 1;
var src = SlidesApp.openById(masterGoogleSlidesId);
var dst = SlidesApp.openById(destinationGoogleSlidesId);
var slides = src.getSlides();
var page = 0;
slides.forEach(function(slide, i) {
if (copyPageNumbers.indexOf(i + 1) != -1) {
dst.insertSlide(offset + page, slide);
page++;
}
});
}
Pattern 2:
Sample script:
function myFunction() {
var copyPageNumbers = [5, 7, 9]; // Please set the page number. This is not the index. So the 1st page is 1.
var masterGoogleSlidesId = "###"; // Please set the master Google Slides ID.
var destinationGoogleSlidesId = "###"; // Please set the destination Google Slides ID.
var offset = 1;
var src = SlidesApp.openById(masterGoogleSlidesId);
var dst = SlidesApp.openById(destinationGoogleSlidesId);
var slides = src.getSlides();
copyPageNumbers.forEach(function(p, i) {
dst.insertSlide(offset + i, slides[p - 1]);
});
}
Note:
- In both patterns, the pages of 5, 7 and 9 of the master Google Slides are copied from 2nd page in the destination Google Slides.
- When
var offset = 0
is used, the pages of 5, 7 and 9 of the master Google Slides are copied from 1st page in the destination Google Slides.
- When
Reference:
Solution 2:
Thanks, @Tanaike, for the solutions!
With your code as a base, I was able to build a script to add slides to multiple slide decks in a single folder. I wanted to share for those who may need it. Code below...
function copySlides() {
// Array of slide numbers to grab [1, 2, 3,...]
var copyPageNumbers = [1]; // Please set the page number. This is not the index. So the 1st page is 1.
// Which deck contains the slides to copy
var masterGoogleSlidesId = '###'; // Please set the master Google Slides ID.
// Folder containing Slides to copy to
var folderID = '###';
var folderToCopyTo = DriveApp.getFolderById(folderID);
// Slide files in folder
var slidesInFolder = folderToCopyTo.getFilesByType(MimeType.GOOGLE_SLIDES);
var counter = 0;
var total = 0;
while (slidesInFolder.hasNext()) {
var file = slidesInFolder.next();
var destinationGoogleSlidesId = file.getId(); // Please set the destination Google Slides ID.
var offset = 0;
try {
var src = SlidesApp.openById(masterGoogleSlidesId);
var dst = SlidesApp.openById(destinationGoogleSlidesId);
var slides = src.getSlides();
copyPageNumbers.forEach(function (p, i) {
dst.insertSlide(offset + i, slides[p - 1]);
});
counter++;
}
catch (e) {
console.log(destinationGoogleSlidesId);
console.log(e);
}
total++;
}
console.log(counter + ' of ' + total + ' Slide Decks Updated');
}
Post a Comment for "Copying Multiple Slides From A Master Slide Desk Using Google Apps Script"