Apps Script's DocumentApp gives you full programmatic access to Google Docs — reading existing content, inserting new text, formatting paragraphs, and building documents from scratch.
Open a document
// Bound script: opens the doc the script is attached toconst doc =DocumentApp.getActiveDocument();// Standalone script: open by IDconst doc =DocumentApp.openById('YOUR_DOC_ID');const body = doc.getBody();
Read all text from a document
functionreadDocText(){const doc =DocumentApp.getActiveDocument();const body = doc.getBody();const text = body.getText();Logger.log(text);}
functionappendContent(){const body =DocumentApp.getActiveDocument().getBody(); body.appendParagraph('New section heading').setHeading(DocumentApp.ParagraphHeading.HEADING2); body.appendParagraph('This paragraph was added programmatically by Apps Script.'); body.appendHorizontalRule();Logger.log('Content appended.');}
Insert text at the top of a document
functioninsertAtTop(){const body =DocumentApp.getActiveDocument().getBody();const timestamp =`Report generated: ${newDate().toLocaleString()}`;// Insert as the very first paragraph body.insertParagraph(0, timestamp).setItalic(true).setForegroundColor('#888888');}
Format text: bold, italic, font size
functionformatText(){const body =DocumentApp.getActiveDocument().getBody();const para = body.appendParagraph('This is important text.');// Get the text element and apply formattingconst text = para.editAsText(); text.setBold(0,7,true);// Bold "This is" text.setFontSize(0,24,14);// Font size 14 for first 24 chars text.setForegroundColor(0,7,'#1a73e8');// Blue for "This is"}
Insert and populate a table
functioninsertTable(){const body =DocumentApp.getActiveDocument().getBody();const tableData =[['Name','Score','Grade'],['Alice','95','A'],['Bob','82','B'],['Carol','91','A'],];const table = body.appendTable(tableData);// Style the header rowconst headerRow = table.getRow(0);for(let i =0; i < headerRow.getNumCells(); i++){ headerRow.getCell(i).setBackgroundColor('#1a73e8').editAsText().setForegroundColor('#ffffff').setBold(true);}}
Search and replace text
functionsearchAndReplace(){const body =DocumentApp.getActiveDocument().getBody(); body.replaceText('\\{\\{company\\}\\}','Acme Corp'); body.replaceText('\\{\\{date\\}\\}',newDate().toDateString());Logger.log('Placeholders replaced.');}
Clear a document and rewrite it
functionrewriteDocument(){const body =DocumentApp.getActiveDocument().getBody(); body.clear(); body.appendParagraph('Fresh Start').setHeading(DocumentApp.ParagraphHeading.TITLE); body.appendParagraph('This document was rewritten by Apps Script.');DocumentApp.getActiveDocument().saveAndClose();}
Tips
body.clear() removes all content but preserves document metadata (title, sharing settings).
replaceText() uses Java regular expressions — escape special characters with \\.
Changes are saved automatically; call doc.saveAndClose() explicitly when done from a standalone script.
Use doc.getUrl() to get a shareable link to the document after creating or modifying it.