Search code examples
node.jsgoogle-slides-apigoogle-slides

Is it possible to duplicate a specific slide using google slide api?


Google Slides Api (Node JS / JavaScript) - is it possible to create a duplicate of a specific slide like for example assuming slide no 5 needs to be duplicated 3 times?


Solution

  • I believe your goal and your current situation as follows.

    • You want to copy the specific slide in Google Slides several times.
      • In your question, you want to copy No. 5 slide 3 times.
    • You want to achieve this using googleapis for Node.js.
    • You have already been able to get and put values for Google Slides using Slides API.

    In this case, I thought that the method of batchUpdate in Slides API can be used.

    Sample script:

    In this sample script, please use auth retrieved from your script. If you want to see the script for authorizing for Node.js, you can see Quickstart for Node.js. Ref In this case, please use the scope of https://www.googleapis.com/auth/presentations.

    const presentationId = "###"; // Please set the presentation ID (Google Slides ID).
    const pageNumber = 5; // Please set the page number. In your question, it's 5.
    const numberOfCopy = 3; // Please set the number of copy. In your question, it's 3.
    
    const slides = google.slides({ version: "v1", auth: auth });
    slides.presentations.get(
      {
        presentationId: presentationId,
        fields: "slides(objectId)",
      },
      (err, res) => {
        if (err) {
          console.log(err);
          return;
        }
        const pageObjectId = res.data.slides[pageNumber - 1].objectId;
        const requests = [];
        for (let i = 0; i < numberOfCopy; i++) {
          requests.push({ duplicateObject: { objectId: pageObjectId } });
        }
        slides.presentations.batchUpdate(
          {
            presentationId: presentationId,
            resource: { requests: requests },
          },
          (err, res) => {
            if (err) {
              console.log(err);
              return;
            }
            console.log(res.data);
          }
        );
      }
    );
    
    • When above script is run, the page object ID of No. 5 slide is retrieved from the Google Slides using the get method, and create the request body for copying 3 times and request it using the batchUpdate method.

    Note:

    • In this sample script, it supposes that auth retrieved from your authorization script can be used. Please be careful this.

    References: