Author: By Raj

Part of our Google Workspace Automation guides. Need this built for your team? Hire a Google Apps Script developer.

Estimated reading time: 10 minutes

Google Docs Automation with Apps Script: Templates & Mail Merge

DocumentApp merges placeholders in Google Docs with rows from Sheets, contracts, proposals, and certificates at scale. Replace {{Name}} style tokens via replaceText or body.appendParagraph.

Export PDF with DriveApp.getFileById(docId).getAs('application/pdf') for email attachments. Keep template Doc in a Templates folder users cannot edit accidentally.

For PDF archival to Drive, combine with /blog/apps-script-google-drive-automation permissions patterns.

Mail merge from Sheets

Loop rows, DocumentApp.openById(templateId).makeCopy(name), replaceText('{{Field}}', value) for each column mapping.

Clear unused placeholders with replaceText('{{\w+}}','') regex caution, test carefully.

Tables and lists

Insert tables with body.appendTable for line items from array of arrays. Style header row bold via getChild(0).editAsText().setBold(true).

Page breaks via appendPageBreak before terms section.

PDF generation pipeline

Save PDF to client folder, email with GmailApp.sendEmail and attachBlob. Filename includes contract id.

Store PDF Drive link back in Sheet column SignedPDF.

Batch limits

Creating hundreds of docs per run hits time limits, process 20 per trigger with Properties cursor.

See /blog/google-apps-script-execution-time-limit.

Example code

function mergeRowToDoc(templateId, row, map) {
  const copy = DocumentApp.openById(templateId).makeCopy('Contract ' + row[0]);
  const body = copy.getBody();
  Object.keys(map).forEach(function (key) {
    body.replaceText('{{' + key + '}}', String(row[map[key]]));
  });
  copy.saveAndClose();
  return copy.getId();
}
ApproachBest forTradeoff
Apps Script nativeGoogle Workspace-centric workflows6-min limit, quotas
Zapier / MakeNo-code, many connectorsPer-task cost, vendor lock-in
Python + CloudHeavy data / MLHosting cost, separate auth
Google Workspace automationProduction custom logicBuild cost, you own code

FAQ

Can I insert images?

Yes with appendImage(blob) from Drive file or URL fetch, watch image size for performance.

Docs vs Slides automation?

SlidesApp similar for pitch decks, DocumentApp better for legal prose merge.

Track changes?

Suggest mode not API-friendly, merge to new copy per version with version suffix in filename.

Non-ASCII names?

UTF-8 safe in replaceText, verify PDF fonts embed correctly for diacritics.

Signer workflows?

Integrate DocuSign via UrlFetchApp, Docs merge generates draft before e-sign envelope.

Need this done for you? I handle this as part of my consulting work, fixed-price quote within 24 hours.

Book a call with Raj →

Get the full Google Docs Automation with Apps Script script template

I'll email you a production-ready, commented version you can deploy in 10 minutes.

Need help with this? I handle this as part of my Google Workspace Automation service.

Gmail, Drive, Docs, Forms, and Calendar automation.

See how it works →