Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting text with google app script in Docs

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.

like image 914
DavidR Avatar asked Oct 24 '25 17:10

DavidR


1 Answers

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);
   }
}
like image 138
OblongMedulla Avatar answered Oct 26 '25 15:10

OblongMedulla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!