3

I'm attempting to write a google apps script that takes the slide titles of every slide (as they are in the master slides) and put them in a nicely formatted table of contents on slide 2.

It doesn't necessarily have to be done with apps script, but this is the best way I could think of.

function readPageElementIds(presentationId, pageId) {
  var response = Slides.Presentations.get(
      presentationId);
  Logger.log(response.slides)
  for (var i = 0; i < response.slides.length; i++) {
    var slide = response.slides[i].pageElements;
    for (var j = 0; j < slide.length; j++) {
      if (slide[j].shape) {
        var texts = slide[j].shape.text.textElements;
        for (var k = 0; k < texts.length; k++) {
          if (texts[k].autoText) {
             Logger.log(texts[k].autoText.content);
          }
        }
      }
    }
  }
}

Yes that's a lot of for loops, I have no idea how to do this.

1
  • I could also add a flag of some kind to some property of each title very easily (such as a slightly lighter black). That was one of the things I thought of, but I have absolutely no clue how to implement that. Commented Mar 12, 2017 at 0:17

1 Answer 1

3

That's fairly close. Things you're missing:

  • You're iterating on all of the shapes, not just the titles. For that, filter on the shape.placeholder.type property. You only want to look at shapes with TITLE and CENTERED_TITLE placeholder types.

  • autoText text elements are relatively rare. Most of your text will be in TextRun text elements

This code would work:

function readPageElementIds(presentationId, pageId) {
  var response = Slides.Presentations.get(
      presentationId);
  Logger.log(response.slides)
  for (var i = 0; i < response.slides.length; i++) {
    var slide = response.slides[i].pageElements;
    for (var j = 0; j < slide.length; j++) {
      if (slide[j].shape && slide[j].shape.placeholder
          && (slide[j].shape.placeholder.type == 'TITLE'
              || slide[j].shape.placeholder.type == 'CENTERED_TITLE')) {
        var texts = slide[j].shape.text.textElements;
        var shapeText = "";
        for (var k = 0; k < texts.length; k++) {
          if (texts[k].autoText) {
             shapeText += texts[k].autoText.content;
          }
          if (texts[k].textRun) {
             shapeText += texts[k].textRun.content;
          }
        }
        Logger.log(shapeText);
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

The documentation for apps script gets me everytime (in a bad way). Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.