Copy One Slide from Google Slides into a New Presentation Using API

Copying a slide from one Google Slides presentation into another

Update at February 16, 2018

At February 13, 2018, the SlidesApp service was updated. Now, copying slides can be achieved by the native methods.

This sample script copies page 1 of srcPresentationId and inserts it as page 1 of the active presentation.

Sample script :

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

Reference :

  • insertSlide(insertionIndex, slide)

Original Answer

Do you still look for the method for copying slides? Unfortunately, I confirmed that copying using the Slides API cannot be done yet. But I thought of a workaround.

How about the following workaround? In a recent Google update, I noticed that Class SlidesApp was added. I used this. Since I didn't find the method for copying a slide directly to a new presentation, I used the following flow.

Flow :

  1. Copy the presentation using DriveApp.
  2. Open the copied presentation.
  3. Remove slides except for a slide you want to copy using remove().

Sample script :

function myFunction() {
var srcSlides = 3; // A page number of slide that you want to copy. In this case, the top number is 1.

var srcid = "1Lqtwb5z8NcU4VVj8OOR11AJyET70tlRRj6QIhxsEZZg";
var dstid = DriveApp.getFileById(srcid).makeCopy().getId();
var dstSlides = SlidesApp.openById(dstid).getSlides();
dstSlides.splice(srcSlides - 1, 1);
for (var i in dstSlides) {
dstSlides[i].remove();
}
}

References :

  • Class SlidesApp : https://developers.google.com/apps-script/reference/slides/slides-app
  • remove() : https://developers.google.com/apps-script/reference/slides/slide#remove

If this was not useful for you, I'm sorry.

How to copy a slide from one presentation to another?

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()

unable to copy a google slide file using google drive api

I believe your current situation and goal as follows.

  • From your script,
    • You are using googleapis for python.
    • You have already been able to use Drive API and Slides API using the service account.
    • an existing template ppt present is a Google Slides which is not the PowerPoint file.

Modification points:

  • From your script and But it is not working. I don't get any error. everything works fine but I don't see the new ppt., I understood that you might want to see the Google Slides copied by the service account at your Google Drive.

    • When the Google Slides is copied by the service account, the copied Google Slides is put to the Drive of the service account. The Drive of service account is different from your Google Drive. By this, you cannot see the copied Google Slides on your Drive. I thought that this might be the reason of your issue.
  • In order to see the Google Slides copied by the service account at your Google Drive, for example, the following workarounds can be used.

    1. Share the copied Google Slides with your email of Google account.
      • In this case, you can see the shared file at the shared folder.
    2. At first, it creates new folder in your Google Drive and share the folder with the email of the service account. And when the Google Slides is copied, it sets the shared folder as the destination folder.

Workaround 1:

In this workaround, it shares the copied Google Slides with your email of Google account. When this is reflected to your script, it becomes as follows.

Modified script:

In this case, new permission is created to the copied file using "Permissions: create".

From:
print(DECK_ID)

print('** Replacing placeholder text')
To:
print(DECK_ID)

permission = {
'type': 'user',
'role': 'writer',
'emailAddress': '###@gmail.com' # <--- Please set the email of your Google account.
}
DRIVE.permissions().create(fileId=DECK_ID, body=permission).execute()

print('** Replacing placeholder text')

Workaround 2:

In this workaround, the Google Slides is copied to the shared folder in your Google Drive. Before you use this script, please create new folder and share the folder with the email of service account. When this is reflected to your script, it becomes as follows.

Modified script:

In this case, the metadata is added to the request body of DRIVE.files().copy().

From:
DATA = {'name': 'Google Slides API template DEMO'}
To:
DATA = {'name': 'Google Slides API template DEMO', 'parents': ['###']}
  • Please set the folder ID of shared folder to ###.

References:

  • Permissions: create
  • Files: copy

Google Slide API - copy an object from a file to another file

duplicateObject only works with objects in the same presentation. Copying objects across presentations isn't supported just yet-- the feature request to follow is here.

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

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:

  • Method: presentations.get
  • Method: presentations.batchUpdate
  • DuplicateObjectRequest

Creating new copies of slides with data from google sheets without loosing original template

Create a duplicate and then work on the duplicate.


var copiedFile = createCopyUsingDriveApp()

values.forEach(function(row){
var templatevariable= row[0];
var templatevalue= row[1]
copiedField.replaceAllText(templatevariable,templatevalue);
})

function createCopyUsingDriveApp() {
// The Id of the presentation to copy
var templateId = "<SLIDES_ID>";

// Create a copy of the presentation using DriveApp
var template = DriveApp.getFileById(templateId);
var fileName = template.getName();
var copy = template.makeCopy();
return copy.setName("Copy of " + fileName);
}


Related Topics



Leave a reply



Submit