I made a function in Google Apps Script that searches for all words in a Google Docs and changes their colors to a desired color. It takes as inputs: the doc ID, the color desired and the word to look for.
However, what I really need is a function that finds all the words that start with a particular string. For example, "change all words that start with # to blue". I tried messing with findText()
but had no luck. Any ideas on how to fix the function below to do what I need? Thanks!
Currently, my function looks like this:
function colorTheWords(findMe,color,documentID) {
//color input must be formatted in CSS notation like '#ffffff'
//documentID must be formated as text in between ''
//findMe word must be formatted as ''
//open doc
var document = DocumentApp.openById(documentID);
var body = document.getBody();
var foundElement = body.findText(findMe);
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();
// Change the current color to the desired color
foundText.setForegroundColor(start, end, color);
// Find the next match
foundElement = body.findText(findMe, foundElement);
}
}