Reputation: 45
So I would like to be able to have the ability to search up to 30+ phrases within a Google doc. Currently I have to do this process manually with find and replace tool.
But could a script automate this process in someway where it would search for the words and highlight them within the doc for me to review.
Upvotes: 0
Views: 42
Reputation: 19339
If I understand you correctly, you want to highlight (by highlight, I assume you mean changing the background color) certain sentences if they are present in your document.
const sentences = ["First sentence", "Second"]; // Your sentences
function highlightSentences() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
sentences.forEach(sentence => {
const rangeElement = body.findText(sentence);
if (rangeElement) {
const startOffset = rangeElement.getStartOffset();
const endOffset = rangeElement.getEndOffsetInclusive();
const element = rangeElement.getElement();
if (element.editAsText) {
const textElement = element.editAsText();
textElement.setBackgroundColor(startOffset, endOffset, "#fcfc03");
}
}
});
}
Upvotes: 1