2

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.

Mishal
  • 450
  • 9
  • 27
  • I have to apologize for my poor English skill. I think that the several slides can be copied to other Google Slides. But from `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?`, I cannot understand about your goal. Can I ask you about your goal and current issue? – Tanaike Feb 12 '20 at 23:19
  • @Tanaike thank you for your response. My goal is to copy multiple slides from a master slide deck to a copied template. I would like to control this on a google sheet which is why I was asking how I could make this possible. – Mishal Feb 13 '20 at 04:00
  • Thank you for replying. I could understand about `copy multiple slides from a master slide deck to a copied template`. About `I would like to control this on a google sheet`, When I could correctly understand about your goal and clearly see the vision of it, I would like to think of the solution. – Tanaike Feb 13 '20 at 04:59
  • For example, I have 10 slides on Master deck. I will have a google form that lets people choose which slides they want to include. I have an apps script that run on the backround once the form has been submitted. The responses of the form are linked to the google sheet from where the appscript is executed. – Mishal Feb 13 '20 at 05:24
  • So basically I am looking to copy multiple slides based on responses recorded on google sheet from google form. If I can know how I can copy multiple sheets at the same time then I can reverse engineer the form to make sure the slide references are matching with the master slide deck. – Mishal Feb 13 '20 at 05:26
  • Thank you for replying. You want to copy the several slides of the source Google Slides to the destination Google Slides. I could understand like this. From your replying, the goal of your this question is only to copy the several slides of the source Google Slides to the destination Google Slides. If my understanding is correct, I think that [your latest question](https://stackoverflow.com/q/60200617/7108653) will be useful. How about this? If I misunderstood your replying, I apologize. – Tanaike Feb 13 '20 at 05:37
  • That has helped in copying one slide - could you guide me on copying multiple slides. Say from example I want to copy slides 5, 7, 9 from a Master Slide Deck of 10 slides. – Mishal Feb 13 '20 at 05:51
  • Thank you for replying. About `copy slides 5, 7, 9 from a Master Slide Deck of 10 slides`, 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. Is my understanding correct? – Tanaike Feb 13 '20 at 05:56
  • Yes that’s correct. – Mishal Feb 13 '20 at 06:02
  • Thank you for replying. I proposed a sample script for this. Could you please confirm it? If I misunderstood your goal and that was not the direction you want, I apologize. – Tanaike Feb 13 '20 at 06:16
  • When I prepared this sample script, I could notice that about [your latest question](https://stackoverflow.com/q/60200617/7108653), my answer was not suitable. I apologize for this. – Tanaike Feb 13 '20 at 06:19

2 Answers2

4
  • 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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @Mohammed Mishal Thank you for replying. I'm glad your issue was resolved. – Tanaike Feb 13 '20 at 07:48
  • if slides are reordered the index changes. using title is safer. – aNewb Feb 28 '21 at 00:49
  • @aNewb Thank you for your comment. But I have to apologize for my poor English skill. I cannot understand about `if slides are reordered the index changes. using title is safer.`, can I ask you about the detail of it? – Tanaike Feb 28 '21 at 00:53
  • My mistake I am thinking about the elements on the slide not the slide itself. – aNewb Feb 28 '21 at 01:01
  • "Specifying object IDs on creation When creating pages or page elements using ... When your application wants to keep track of objects over a longer period of time, don't rely on the object ID, because it may change. See the following section for more details." https://developers.google.com/slides/how-tos/overview?utm_campaign=gsuite_update_slides_api_110916&utm_source=gdev&utm_medium=yt-desc This sounds like it includes the pages and the elements???? – aNewb Mar 01 '21 at 00:49
  • @aNewb Thank you for replying. I have to apologize for my poor English skill again. Unfortunately, I cannot understand about your replying. Can I ask you about the detail of your goal? – Tanaike Mar 01 '21 at 01:12
  • I probably should post a question. I am trying to get an image (page element) from a slide into a sidebar. That way the presentation can be shared without having to share drive access to images. – aNewb Mar 03 '21 at 15:50
  • @aNewb Thank you for replying. Unfortunately, I cannot image about `I am trying to get an image (page element) from a slide into a sidebar. That way the presentation can be shared without having to share drive access to images.`. This is due to my poor English skill. I deeply apologize for this. – Tanaike Mar 04 '21 at 00:23
1

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');

}
Paul Murray
  • 95
  • 2
  • 9