How to copy a slide from one presentation to another?

前端 未结 1 1531
走了就别回头了
走了就别回头了 2021-01-19 05:36

I am trying to copy slides from a source presentation and append them to the end of the destination presentation. I have searched solutions on SO but they use the google app

相关标签:
1条回答
  • 2021-01-19 05:52

    Unfortunately, in the current stage, there are no methods for directly copying a slide (It's like the method of copyTo of Sheets API.) to other Google Slides, yet. So in order to copy a slide to other Slides, I think that there are 2 workarounds.

    1. After get the all objects and formats in a slide by get method, create new slide and puts the objects using batchUpdate method.
    2. Create an API using Google Apps Script, because the slides service of GAS has the method for directly copying a slide.

    I recommend the latter, because I think that the former will be the complicated script. So I would like to propose a sample script for the latter workaround.

    When you use this script, please do the following flow.

    Preparation flow

    1. Log in to Google Drive. https://drive.google.com/drive/my-drive
    2. Create new standalone project.
      • Please create new project at https://script.google.com/.
    3. Set the project name and copy&paste the following sample script.
    4. Deploy Web Apps.
      1. On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
      2. Select "Me" for "Execute the app as:".
      3. Select "Anyone, even anonymous" for "Who has access to the app:".
      4. Click "Deploy" button as new "Project version".
      5. Automatically open a dialog box of "Authorization required".
        1. Click "Review Permissions".
        2. Select own account.
        3. Click "Advanced" at "This app isn't verified".
        4. Click "Go to ### project name ###(unsafe)"
        5. Click "Allow" button.
      6. Copy "Current web app URL:".
        • It's like https://script.google.com/macros/s/#####/exec.
      7. Click "OK".

    By this flow, the Web Apps was deployed as own API. In this sample, "Anyone, even anonymous" for "Who has access to the app:" was used as a test. If you want to use the access token, please modify this. You can see the detail information at below "References".

    Sample script

    Google Apps Script:

    function doGet(e) {
      var srcId = e.parameters.srcId;
      var dstId = e.parameters.dstId;
      var srcPage = e.parameters.srcPage;
      var srcSlide = SlidesApp.openById(srcId);
      var dstSlide = SlidesApp.openById(dstId);
      var copySlide = srcSlide.getSlides()[srcPage - 1];
      dstSlide.appendSlide(copySlide);
      return ContentService.createTextOutput("Done.");
    }
    

    curl command:

    After Web Apps was deployed, you can use the Web Apps as own API. The sample curl for requesting to the deployed Web Apps is as follows.

    Before you use this, please set the source and destination file ID of Slides. When you want to copy the 1st page of the source Slides, please set 1 to srcPage. And also please set the endpoint which was retrieved above.

    curl -GL \
      -d "srcId=### fileId of source Slides ###" \
      -d "dstId=### fileId of destination Slides ###" \
      -d "srcPage=1" \
      "https://script.google.com/macros/s/#####/exec"
    

    When this command is run, Done. is returned. At that time, you can see the appended slide to the last page in the destination Slides.

    References

    • Standalone Scripts
    • Web Apps
    • Taking advantage of Web Apps with Google Apps Script
    • openById()
    • getSlides()
    • appendSlide()
    0 讨论(0)
提交回复
热议问题