I would to like to write a script to find all specific words and format their color.
I tryied the following, but it seems it is not working:
function ChangeColor() {
var body = DocumentApp.getActiveDocument().getBody();
var myword = body.findText("var");
while (myword !== null){
var mysearch = myword.getElement().asText().setForegroundColor('#DC64DC');
}
}
Can anyone help me, please?
You want to change the font color of "var" in the document. If my understanding is correct, how about this modification?
Modification points :
- In order to search next word, it uses
findText(searchPattern, from)
.
- In order to change the color of "var", it uses
setForegroundColor(startOffset, endOffsetInclusive, color)
.
- In your script, the offset of start and end can be retrieved by
myword.getStartOffset()
and myword.getEndOffsetInclusive()
, respectively.
Modified script :
function ChangeColor() {
var searchValue = "var";
var body = DocumentApp.getActiveDocument().getBody();
var myword = body.findText(searchValue);
while (myword) {
var mysearch = myword.getElement().asText()
mysearch.setForegroundColor(
myword.getStartOffset(), // startOffset
myword.getEndOffsetInclusive(), // endOffsetInclusive
'#DC64DC' // color
);
var myword = body.findText(searchValue, myword); // Search next word
}
}
References :
- findText(searchPattern, from)
- setForegroundColor(startOffset, endOffsetInclusive, color)
If I misunderstand your question, I'm sorry.