Search code examples
pythongoogle-apps-scriptgoogle-slides-api

Google Slide API-How do i duplicate a slide several times and creating a unique object id each time


In general i want people to put some data into a google sheet and provide a button, which takes the data and automatically fills a prefabricated google slide. I have chosen the approach to duplicate a master slide and then replacing individual text modulars with the data. Is there a better approach for my goal?

This is the Master-Slide

Master-Slide

This is the code i use for the duplication, but it is only able to duplicate the master slide once, because the objectId it creates, is always the same: "copiedSlide_001". I have tried to use a random integer for the objectId, but i dont get it to work. Also, i guess the name has to be predictable to be used in the next step. (importing the data into newly copied slide).

function duplicateSlide() {  

    var presentationId = "1XLtOv7FHWwv6R2QEZF_LEhF7twXTlY3IbvsVDKxdDtc";
    var pageId = "g75e5d89173_0_0"
    var requests = [{

      "duplicateObject": {
        "objectId": pageId,
        "objectIds": {
          "g75e5d89173_0_0": "copiedSlide_001",

        }
      }
    } ]Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
    }

How do i get the duplication process to work in my favor?

I would appreciate any help highly!


Solution

    • You want to copy the master slide several times in Google Slides.
    • You want to give the unique object ID for each copied slide like copiedSlide_001.
      • You are required to know the object ID of the copied slide before copying it.
    • You want to achieve this using Google Apps Script.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    Modification point:

    • In this modification, it requests several DuplicateObjectRequest using the method of batchUpdate.

    Modified script:

    Before you run the script, please enable Slides API at Advanced Google services.

    function duplicateSlide() {
      var presentationId = "1XLtOv7FHWwv6R2QEZF_LEhF7twXTlY3IbvsVDKxdDtc"; // Please set the Slides ID.
      var pageId = "g75e5d89173_0_0"; // Please set the page ID of the source slide.
      var newIDs = ["copiedSlide_001", "copiedSlide_002", "copiedSlide_003"]; // Please set the unique ID here.
    
      var requests = newIDs.reverse().map(function(id) {
        var obj = {};
        obj[pageId] = id;
        return {duplicateObject: {objectId: pageId, objectIds: obj}};
      });
    //  requests.push({deleteObject: {objectId: pageId}});
      Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
    }
    
    • When you run the script, as a test case, the source slide is copied 3 times. And each object ID is "copiedSlide_001", "copiedSlide_002" and "copiedSlide_003", respectively.
      • The page IDs of 2nd, 3rd and 4th pages are "copiedSlide_001", "copiedSlide_002" and "copiedSlide_003", respectively.
    • If you use requests.push({deleteObject: {objectId: pageId}});, after the copy was finished, the 1st page, which is the source slide, is deleted. But I'm not sure whether this is the result you want. So I put it in the script as a comment.

    References:

    If I misunderstood your question and this was not the direction you want, I apologize.