简体   繁体   中英

Google apps script: Google Slide - exporting selected slides to a new slide presentation

I am trying to figure out a way to export selected slides to a new slide presentation. After holding ctrl and selecting multiple slides in the film strip (in the screenshot example it is slide 2,3,4) how would I export only those selected slides to a new deck presentation?

I found an older question on stack answered by User Tanaike ( Google app script: exporting active slide (google slides) as PDF ) that exports the the active slide in view and converts it to PDF, but no matter what I did to edit the script it would only copy over a single slide or return an error when I ran an edited script. I was not able to edit the code so that it would export all the selected slides.

Thank you in advance for your help. Copying the part of Tanaike's code I was using as reference.

    function myFunction() {

  // 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();
}

截屏

I believe your goal is as follows.

  • You want to copy the slides to a new Google Slides by selecting several slides in Google Slides.
  • You want to achieve this using Google Apps Script.

In your situation, how about the following sample script?

Sample script:

In this case, at first, please select the slides from the active Google Slides. And, please run this script. By this, the selected slides are copied to the new Google Slides. The new Google Slides is created to the root folder.

function myFunction() {
  // 1. Retrieve the selected slides.
  const s = SlidesApp.getActivePresentation();
  const pageRange = s.getSelection().getPageRange();
  if (!pageRange) return;
  const slides = pageRange.getPages();

  // 2. Create a new Google Slides.
  const newSlides = SlidesApp.create("newSlides");

  // 3. Copy the selected slides to the new Google Slides.
  slides.reverse().forEach(s => newSlides.insertSlide(0, s.asSlide(), SlidesApp.SlideLinkingMode.LINKED));

  // 4. Delete the initial slide in the new Google Slides.
  newSlides.getSlides()[slides.length].remove();
}
  • When you selected no slides, this script is not run. Please be careful this.

  • If you want to copy the selected slides to the existing Google Slides, please modify const newSlides = SlidesApp.create("newSlides"); to const newSlides = SlidesApp.openById("###GoogleSlidesID###"); .

  • When you want to link the copied slides to the original one, please use slides.reverse().forEach(s => newSlides.insertSlide(0, s.asSlide(), SlidesApp.SlideLinkingMode.LINKED)); .

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM