Created
July 25, 2018 06:58
-
-
Save ex-hota911/0953dd95f02811e2b1c0624cb2a58d54 to your computer and use it in GitHub Desktop.
Change color of text matching pattern in Google Docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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