0

Using Google App Script, is there a way to get all the TextRuns (a block of text with the same format) in Google Docs?

I'm trying to create a function that batch replaces one specific text format (from black text to white text).

I've looked through the GAS reference, but can't find anything that does this. findElement() doesn't allow the ElementType TextRun. And overall TextRun doesn't seem to be a functional/manipulable element, at least in the current ref docs -- even though the Document Structure doc does mention it as a sub-element of Paragraph.

So, is there a way to get all the TextRuns? Or is there an equivalent alternative?

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • `findElement(text)` and check text.getForegroundColor()==="black"? – TheMaster Jul 06 '20 at 16:25
  • Already tried that. findElement(text) will return a block per paragraph, ignoring the formatting within the block. getForegroundColor() would return null when the formatting is not uniform. This is why I was specifically searching for a way to get a TextRun instead of any text block. – user1439542 Jul 06 '20 at 16:52
  • One way would be to loop through the block for each offset: `.getForegroundColor(offset)`. Another thing to try would be the [api](https://developers.google.com/docs/api/reference/rest/v1/documents#ParagraphElement) through advanced Google services. Sidenote: See [ask]. If you've tried something, you should mention it in your question with your **code** and **documentation links**. It's time efficient. – TheMaster Jul 06 '20 at 17:49
  • 1
    Does this answer your question? [How do you change formatting within a google doc for multiple occurrences using findText()?](https://stackoverflow.com/questions/32674241/how-do-you-change-formatting-within-a-google-doc-for-multiple-occurrences-using) – Rubén Jul 06 '20 at 17:53

1 Answers1

1

Text runs can be retrieved using Document.get. The runs are located at Body> Structured Element (Paragraph)>Paragraph Element>TextRun in the document json. Advanced Google services may be used to access the api from apps script.

TheMaster
  • 45,448
  • 6
  • 62
  • 85