Nikhil Jain
Nikhil Jain

Reputation: 79

Google app script: exporting active slide (google slides) as PDF

I wanted to know how I can export the active slide on the presentation as a PDF. I was able to figure it out for google sheets but nothing for google slides.

Thank you!

Upvotes: 1

Views: 1479

Answers (1)

Tanaike
Tanaike

Reputation: 201378

I believe your goal as follows.

  • You want to export the current active slide on Google Slides as a PDF file to the specific folder in Google Drive using Google Apps Script.

For this, how about this answer? In this answer, I use the following flow.

  1. Retrieve the active slide.
  2. Create a temporal Google Slides.
  3. Copy the active slide to the temporal Google Slides.
  4. Delete the initial slide in the temporal Google Slides.
  5. Export the temporal Google Slides as a PDF file.
  6. Delete the temporal Google Slides.

Sample script:

Please copy and paste the following script to the container-bound script of Google Slides. And, please open a slide in the Google Slide and run this script. By this, the PDF file of the active slide is created in the folder of destinationFolderId.

function myFunction() {
  const destinationFolderId = "###";  // Please set the destination folder ID.

  // 1. Retrieve the active slide.
  const s = SlidesApp.getActivePresentation();
  const activeSlide = s.getSelection().getCurrentPage().asSlide();

  // 2. Create a temporal Google Slides.
  const temporalSlides = SlidesApp.create("temporalSlides");

  // 3. Copy the active slide to the temporal Google Slides.
  temporalSlides.insertSlide(0, activeSlide);

  // 4. Delete the initial slide in the temporal Google Slides.
  temporalSlides.getSlides()[1].remove();
  temporalSlides.saveAndClose();

  // 5. Export the temporal Google Slides as a PDF file.
  const file = DriveApp.getFileById(temporalSlides.getId());
  DriveApp.getFolderById(destinationFolderId).createFile(file.getBlob().setName(`${s.getName()}.pdf`));

  // 6. Delete the temporal Google Slides.
  file.setTrashed(true);
}

References:

Upvotes: 1

Related Questions