Copying Multiple Slides from a Master Slide Desk using Google Apps Script

坚强是说给别人听的谎言 提交于 2020-03-25 06:04:39

问题


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 data from google form is entered on the google slides template. See below code. I am looking to create a question on the form where people will be able to select multiple slides to be included (2 Credential slides for example out of 10)

function PoD() {

  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("A-PoD").activate();

  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lr = ss.getLastRow()

  for (var i =2;i<lr;i++){

    if(ss.getRange(i, 1).getValue()){

  //Make a copy of the template file
  var documentId = DriveApp.getFileById('1REHMrl6kfzXbgSipvBDkNitkfsM8tJsUSAICggxNsHw').makeCopy().getId();

  var Name_of_programme = ss.getRange(i, 2).getValue();

  DriveApp.getFileById(documentId).setName("PwC's Academy_"+Name_of_client+"_"+Name_of_programme+"_"+Month);

  var FileName = Name_of_programme;

  //Get the document body as a variable
  var body = SlidesApp.openById(documentId);

  body.replaceAllText('{Name of programme}', Name_of_programme);


  var lastSlide = body.getSlides();
  lastSlide[5].remove();

I am looking to continue the script to include a function to select multiple slides. I saw the below script to copy one slide but have not been able to figure out how to copy multiple slides easily.

var srcPresentationId = "### source fileId ###";
var copysrcSlideIndex = 0; // 0 means page 1.

var copydstSlideIndex = 0; // 0 means page 1.

var src = SlidesApp.openById(srcPresentationId).getSlides()[copysrcSlideIndex];
SlidesApp.getActivePresentation().insertSlide(copydstSlideIndex, src);

I want to give people the choice to select which slides to include on the google form as multiple choice.

At the back end of the script, would I need to map the names of the slides with slide numbers? or can have include a unique reference in a text box on each slide and then select the relevant slide? Thinking out loud here. Any guidance would be appreciated.


回答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:

  1. Retrieve all slides from the master Google Slides.
  2. 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.

Reference:

  • insertSlide(insertionIndex, slide)


来源:https://stackoverflow.com/questions/60196682/copying-multiple-slides-from-a-master-slide-desk-using-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!