Read and Write Google Docs Using Apps Script

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 to const doc = DocumentApp.getActiveDocument(); // Standalone script: open by ID const doc = DocumentApp.openById('YOUR_DOC_ID'); const body = doc.getBody();

Read all text from a document

function readDocText() { const doc = DocumentApp.getActiveDocument(); const body = doc.getBody(); const text = body.getText(); Logger.log(text); }

Read paragraph by paragraph

function readParagraphs() { const body = DocumentApp.getActiveDocument().getBody(); const paragraphs = body.getParagraphs(); paragraphs.forEach((p, i) => { Logger.log(`Paragraph ${i + 1}: ${p.getText()}`); }); }

Append text and paragraphs

function appendContent() { 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

function insertAtTop() { const body = DocumentApp.getActiveDocument().getBody(); const timestamp = `Report generated: ${new Date().toLocaleString()}`; // Insert as the very first paragraph body.insertParagraph(0, timestamp) .setItalic(true) .setForegroundColor('#888888'); }

Format text: bold, italic, font size

function formatText() { const body = DocumentApp.getActiveDocument().getBody(); const para = body.appendParagraph('This is important text.'); // Get the text element and apply formatting const 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

function insertTable() { 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 row const 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

function searchAndReplace() { const body = DocumentApp.getActiveDocument().getBody(); body.replaceText('\\{\\{company\\}\\}', 'Acme Corp'); body.replaceText('\\{\\{date\\}\\}', new Date().toDateString()); Logger.log('Placeholders replaced.'); }

Clear a document and rewrite it

function rewriteDocument() { 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.