0

I know it is possible to get the text style of a selected word using getSelection().getTextRange().getTextStyle()

but if I want to retrieve text styles from a previous slide word by word (getting each word's text style individually), is there a way to do that?

TheMaster
  • 45,448
  • 6
  • 62
  • 85

1 Answers1

1

You can iterate through the PageElements of the slide, and retrieve the text style from each element.

For each PageElement, you would have to check whether its type has TextRange as one of its properties (only Shape and TextCell elements have TextRange). If you try to retrieve text from an element that does not contain text, you might end up getting an error.

Then, for each textRange that you retrieve, you can iterate through its runs (the different segments contained in this text that have a different text style), and retrieve the text style for each run.

Code sample:

function getTextStyles() {
  var slideId = "your-slide-id"; ID of the slide you want to retrieve text style
  var presentation = SlidesApp.getActivePresentation();
  var slide = presentation.getSlideById(slideId);
  var pageElements = slide.getPageElements(); // Get all page elements in slide
  pageElements.forEach(function(pageElement) { // Loop through all page elements in slide
    if (pageElement.getPageElementType() == "SHAPE") { // Check that current page element is of type "SHAPE"
      var textRange = pageElement.asShape().getText(); // Get text belonging to current page element
      textRange.getRuns().forEach(function(run) { // Loop through all runs in text
        var textStyle = run.getTextStyle(); // Get current row text style
        console.log(textStyle);
      });
    };
  });
}

Note:

  • The code above logs the TextStyle of all the text belonging to Shape elements in the slide. TableCell elements can also contain text, so if you want to retrieve that information too, you would have to modify the script accordingly (take a look at Class Table).

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • I didn't know about the getRuns() method... thanks a million! – Safwat Ali Khan May 04 '20 at 06:02
  • @SafwatAliKhan You're welcome. If you consider this answer helpful, please consider [accepting it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). This is useful because this community relies on it to share knowledge to other users. – Iamblichus May 04 '20 at 07:03