How to use these
Set up before you run
Copy a snippet into a script file, read its setup and notes, change the placeholder values, and save. For functions meant to run manually, choose the function name in the toolbar and press Run. Results appear in the Execution log unless the description says otherwise.
5
Replace placeholder text
Find placeholder text like {{Name}} in a document’s first tab and replace it with a real value, such as when filling in a template.
Code.gs
function replaceNamePlaceholder() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const body = DocumentApp.openById(documentId).getBody();
const customerName = 'Jamie Rivera';
body.replaceText('\\{\\{Name\\}\\}', customerName);
Logger.log('Replaced the Name placeholder.');
}
Good to know. replaceText treats the search pattern as a regular expression, so curly braces must be escaped as shown.
7
Insert a page break
Force the content that follows in a document’s first tab onto a new printed page.
Code.gs
function appendPageBreak() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const body = DocumentApp.openById(documentId).getBody();
body.appendPageBreak();
body.appendParagraph('This paragraph starts on a new page.');
Logger.log('Page break added.');
}
8
Get all body text
Read the entire text content of a document’s first tab as one plain string.
Code.gs
function logAllBodyText() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const body = DocumentApp.openById(documentId).getBody();
Logger.log(body.getText());
}