简体   繁体   中英

Google apps script: Google Slides - Is it possible to pull in slide text or slide number when using "SlidesApp.create("?

I am trying to use find out if there is a way to add a unique identifier so that when a script with "SlidesApp.create" is used it generates a different presentation name each time. For context I have a deck with arounds 40 slides. I'm using the below script graciously provided by user Tanaike to export slides that I have selected into a new deck presentation. In my screenshot example below I selected slides 2 and 3 and used the script to export only those slides into a new deck presentation. Currently the script uses SlidesApp.create("newSlides"); to name the newly created slide presentation "newSlides". My goal is to be able to appended the title so that the newly created presentation with the exported slides can pull in either the first selected slide's actual text or just the slide number from the original deck.

Using my screenshot example below it would be either

  • "newSlides 2" - since the first slide of the multiple selected slides in the original deck presentation is the slide 2 or
  • "newSlides CAT"

截屏

for context below is the full script I am using

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();
}
function myFunction() {
  // 1. Retrieve the selected slides.
  const s = SlidesApp.getActivePresentation();
  
  const pageRange = s.getSelection().getPageRange();
  if (!pageRange) return;
  const slides = pageRange.getPages();
  
  // the first slide in the selected range
  var selectedSlide = slides[0]
  var allSlides = s.getSlides()

  // find the index by objectId 
  const selectedIndex = allSlides.findIndex(slide=>slide.getObjectId()===selectedSlide.getObjectId())
  console.log(selectedIndex)

  const PAGE_ELEMENT_INDEX = 0 // assuming the title you are interested in is the first element on the page
  const pageElement = selectedSlide.asSlide().getPageElements()[PAGE_ELEMENT_INDEX]
  selectedText = pageElement.asShape().getText().asString()
  console.log(selectedText)
  
  // use selectedIndex or selectedText
}

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