Skip to content

Instantly share code, notes, and snippets.

@ex-hota911
Created July 25, 2018 06:58
Show Gist options
  • Select an option

  • Save ex-hota911/0953dd95f02811e2b1c0624cb2a58d54 to your computer and use it in GitHub Desktop.

Select an option

Save ex-hota911/0953dd95f02811e2b1c0624cb2a58d54 to your computer and use it in GitHub Desktop.
Change color of text matching pattern in Google Docs.
var pattern = "//.*";
function changeColor(text, pattern) {
var foundText = text.findText(pattern);
while (foundText != null) {
foundText.getElement().asText().setForegroundColor(
foundText.getStartOffset(),
foundText.getEndOffsetInclusive(),
"#888888");
foundText = text.findText(pattern, foundText);
}
}
function updateFormat() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var selection = doc.getSelection();
if (selection != null) {
var elements = selection.getRangeElements();
for (var i = 0; i < elements.length; i++) {
var text = elements[i].getElement().asText();
changeColor(text, pattern);
}
} else {
changeColor(body, pattern);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment