Skip to content

Instantly share code, notes, and snippets.

@5argon
Created May 22, 2017 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5argon/dd9ed463162c4f2da216298ea781afc6 to your computer and use it in GitHub Desktop.
Save 5argon/dd9ed463162c4f2da216298ea781afc6 to your computer and use it in GitHub Desktop.
How to format an entire document using RegEx in Google Docs add-on. It will bold my name in string like "5argon : Hello World!"
function formatInkVN() {
var body = DocumentApp.getActiveDocument().getBody();
//all text!
var allText = body.editAsText();
var regexString = "^.*[ ]:[ ]";
var rangeElement = allText.findText(regexString);
while (rangeElement != null) {
var matchedText = rangeElement.getElement().asText();
var begin = rangeElement.getStartOffset();
var end = rangeElement.getEndOffsetInclusive();
Logger.log(begin + " " + end);
matchedText.setBold(false); //matchedText is the entire line, I want to reset the line first
matchedText.setBold(begin, end, true); //Then I bold only the part that RegEx matches.
rangeElement = allText.findText(regexString, rangeElement); //continue from previous point
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment