find and format Specific text in google document using script
// Set the whole body to Roboto 10
var FontStyle = {};
FontStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Roboto';
FontStyle[DocumentApp.Attribute.FONT_SIZE] = 10;
body.setAttributes(FontStyle);
// Set some elements to bold
var target1 = "Private & Confidential"
var searchResult1 = body.findText(target1);
if (searchResult1 !== null) {
var thisElement1 = searchResult1.getElement();
var thisElement1Text = thisElement1.asText();
thisElement1Text.setBold(searchResult1.getStartOffset(), searchResult1.getEndOffsetInclusive(), true);
}
var target2 = "Your Reference:"
var searchResult2 = body.findText(target2);
if (searchResult2 !== null) {
var thisElement2 = searchResult2.getElement();
var thisElement2Text = thisElement2.asText();
thisElement2Text.setBold(searchResult2.getStartOffset(), searchResult2.getEndOffsetInclusive(), true);
}
var target3 = "Our Reference:"
var searchResult3 = body.findText(target3);
if (searchResult3 !== null) {
var thisElement3 = searchResult3.getElement();
var thisElement3Text = thisElement3.asText();
thisElement3Text.setBold(searchResult3.getStartOffset(), searchResult3.getEndOffsetInclusive(), true);
}
// Right align date
var searchType = DocumentApp.ElementType.PARAGRAPH
var target4 = "\\[Date\\]";
var searchResult4 = body.findText(target4);
while (searchResult4 = body.findElement(searchType, searchResult4)) {
var par = searchResult4.getElement().asParagraph();
if (searchResult4 != null) {
par.setAlignment(DocumentApp.HorizontalAlignment.RIGHT);
}
}
// Update date
var date = Utilities.formatDate(new Date(), "GMT", "d MMMMM yyyy");
body.replaceText("\\[Date\\]", date);
}