3
votes

Is it possible for an app script to highlight (as in select) text? I want to run the script from the menu and then have all matching instances of some text selected so they can be formatted in one go.

Specifically, I want to write a script to highlight all footnotes in a Google Doc so that they can be formatted simultaneously. I am the creator of the Footnote Stylist add on for Docs, which allows users to style footnotes. But I want to include the option of using any formatting, without having to include every available formatting choice in the add on itself.

1

1 Answers

3
votes

How about skip the highlighting portion and just format them direct? The code below searches for the word "Testing" and bolds it & highlights it yellow. Hope this helps.

function bold() {
var body = DocumentApp.getActiveDocument().getBody();
var foundElement = body.findText("Testing");

while (foundElement != null) {
    // Get the text object from the element
    var foundText = foundElement.getElement().asText();

    // Where in the element is the found text?
    var start = foundElement.getStartOffset();
    var end = foundElement.getEndOffsetInclusive();

    // Set Bold
    foundText.setBold(start, end, true);

     // Change the background color to yellow
    foundText.setBackgroundColor(start, end, "#FCFC00");

    // Find the next match
    foundElement = body.findText("Testing", foundElement);
   }
}