From Google Docs Placeholders to Spreadsheet Automation

In the previous article, we learned how to replace placeholders like [name] or [date] inside a Google Doc using Apps Script. We also made sure to keep the original file intact by creating a copy before applying replacements.

That’s great for a single document… but what if you need to create dozens of personalized files? Contracts, certificates, letters, all with different values?

The answer: connect your Google Doc template to a Google Sheet.
Each row of your sheet will represent a new document, and the script will:

  1. Create a copy of the template
  2. Replace all placeholders with values from the sheet
  3. Paste the link of the generated file back into the spreadsheet

Step 1 – Prepare your Spreadsheet

Create a sheet with the following structure (headers in the first row):

Example spreadsheet containing source data for generating Google Docs templates

Notice the last column generatedLink, that’s where the script will paste the URL of each generated document.

Step 2 – Prepare your Google Doc Template

In your template, write placeholders between brackets:

Step 3 – The Script

This time the script will be bounded to our Google Sheets.

Open the Apps Script editor from your Spreadsheet (Extensions → Apps Script) and paste:

/**
 * Generate documents from a template Doc,
 * replacing placeholders with data from the sheet,
 * and storing links back in the sheet.
 */
function generateDocsFromSheet() {
  // IDs
  const TEMPLATE_ID = 'YOUR_TEMPLATE_DOC_ID_HERE';
  const FOLDER_ID = 'YOUR_TARGET_FOLDER_ID_HERE';

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();
  const data = sheet.getDataRange().getValues();
  const headers = data[0];

  // Loop through each row (skip headers)
  for (let i = 1; i < data.length; i++) {
    const row = data[i];

    // Skip if doc already generated
    if (row[headers.indexOf('generatedLink')]) continue;

    // Build replacements
    const replacements = {};
    headers.forEach((h, idx) => {
      if (h !== 'generatedLink') {
        replacements[`\\[.*${h}.*\\]`] = row[idx];
      }
    });

    // Make copy of template
    const templateFile = DriveApp.getFileById(TEMPLATE_ID);
    const copyFile = templateFile.makeCopy(
      `Generated_${row[0]}_${new Date().toISOString().slice(0,10)}`,
      DriveApp.getFolderById(FOLDER_ID)
    );

    // Open and replace placeholders
    const doc = DocumentApp.openById(copyFile.getId());
    const body = doc.getBody();
    for (let tag in replacements) {
      body.replaceText(tag, replacements[tag]);
    }
    doc.saveAndClose();

    // Save link back into sheet
    sheet.getRange(i+1, headers.indexOf('generatedLink')+1)
      .setValue(copyFile.getUrl());
  }
}

Step 4 – Run the Script

  1. Replace YOUR_TEMPLATE_DOC_ID_HERE with your template’s Doc ID
  2. Replace YOUR_TARGET_FOLDER_ID_HERE with the ID of the folder where generated docs should be stored
  3. Run the script → authorize it → watch your documents appear!

Each row in your sheet now produces a new Google Doc, and the link is written back automatically:

Wrap Up

With just a few lines of code, you’ve upgraded your document automation:

  • One Google Doc becomes a reusable template
  • One Google Sheet becomes a data source
  • Each row produces a personalized document with its link logged back

This setup is basically a DIY mail-merge for Docs

In the next article, we’ll explore how to send those generated documents automatically by email, turning your workflow into a fully automated system.

Related reading: For the underlying placeholder replacement technique, first read how to replace tags in Google Docs using Apps Script.

Replace Tags in Google Docs with Apps Script

Ever needed to prepare a contract, a template, or a letter where you just replace placeholders like [Client Name] or [Date] with real values?
Instead of manually editing each tag, we can automate the process with Google Apps Script.

This article shows you how to:

  • Add placeholders in your document
  • Replace them with real values in one click
  • Keep your templates clean and reusable

Why use placeholders?

Placeholders are short markers inside your document that can later be replaced with actual content.
For example:

Example Google Docs template with placeholders to replace using Apps Script

With Apps Script, you can easily replace [name] with “Lidia”, or any other dynamic value from a sheet, a form, or an API.

Step 1 – Prepare your Google Doc

Open a new Google Docs file, or write docs.new in your search bar, and write some sample content with tags between square brackets [ ].

Step 2 – Open the Script Editor

  1. In your Google Doc, go to Extensions → Apps Script

2. Delete the default code and paste the following function:

function replaceAllPlaceholders() {
  const body = DocumentApp.getActiveDocument()
    .getActiveTab()
    .asDocumentTab()
    .getBody();
    
  const replacements = {
    '\\[.*name.*\\]': 'Lidia',
    '\\[.*date.*\\]': '16 Sept 2025',
    '\\[.*company.*\\]': 'Apps Script Lab'
  };
  
  for (let tag in replacements) {
    body.replaceText(tag, replacements[tag]);
  }
}

You can adapt the placeholders to match whatever data you need to inject.
Each occurrence of the placeholder will be swapped automatically using the replaceText() method.

Keep in mind: the function expects a regular expression as the search patter

Make your template reusable

In this first example, we simply replaced the placeholders directly in the document.
But if you need to generate multiple documents while keeping your original file intact, it’s better to create a copy of the template and run the replacements in the copy.

/**
 * Creates a copy of the active Google Doc,
 * replaces placeholders in the copy,
 * and returns the URL of the generated document.
 */
function generateDocFromTemplate() {
  // Define placeholder values
  const replacements = {
    '\\[.*name.*\\]': 'Lidia',
    '\\[.*date.*\\]': '16 Sept 2025',
    '\\[.*company.*\\]': 'Apps Script Lab'
  };

  // Get the template (active document)
  const templateDoc = DocumentApp.getActiveDocument();
  const templateFile = DriveApp.getFileById(templateDoc.getId());

  // Create a copy next to the original
  const copyFile = templateFile.makeCopy(
    templateFile.getName() + ' - Generated ' + new Date().toISOString().slice(0, 10),
    templateFile.getParents().next() // same folder
  );

  // Open the copy for editing
  const copyDoc = DocumentApp.openById(copyFile.getId());
  const body = copyDoc.getBody();

  // Replace all placeholders
  for (let tag in replacements) {
    body.replaceText(tag, replacements[tag]);
  }

  // Save & close
  copyDoc.saveAndClose();

  // Return the URL (for logs or UI)
  Logger.log('Generated doc: ' + copyFile.getUrl());
  return copyFile.getUrl();
}

The script will create a new document based on your template.
Your original file remains untouched, and the placeholders are replaced only in the generated copy.

Much cleaner than editing manually, and your template is always safe for the next run.

Next Steps

In this tutorial, we learned how to:

  • Use placeholders in a Google Doc
  • Replace them automatically with Apps Script
  • Keep our original template safe by generating copies

The natural next step is to connect this process with Google Sheets.
That way, you can store your tags and values in a spreadsheet (e.g. [name], [date], [company]), run the script for each row, and automatically log the links to the generated documents back into the sheet.

This turns your simple template into a full document generation system

Related reading: Ready to use these placeholders with spreadsheet data? Continue with generating Google Docs from Google Sheets with Apps Script.

Add Invisible IDs to Google Docs Paragraphs with Apps Script

Ever wanted to track paragraphs in a Google Doc without adding visible clutter? Whether you’re syncing content with an external system, managing edits across collaborators, or linking comments to specific sections, this technique lets you embed invisible unique IDs inside your document using zero-width characters.

Let’s dive into how this works, and how you can use it in your own Docs.