bridgeincbze.com → google → google site sheets → how to do mail merge in google sheets
Posted on by Xavier Moran

how to do mail merge in Google Sheets

Screenshot of Google Sheets with merged email fields highlighted, showing sample data and a template preview

Mail merge in Google Sheets automates sending personalized emails by combining a list of recipients with a customizable message template. It uses Google Apps Script to pull data from a spreadsheet and send individualized emails via Gmail.

The process begins with a data sheet containing recipient details-names, email addresses, and custom fields like order numbers or event dates. A separate template document holds the message structure. When triggered, the script pulls each row of data, replaces placeholder variables (like <name> or <order_id>), and sends the resulting email through Gmail.

This method eliminates manual copy-paste tasks and reduces errors in bulk communications. It's used for newsletters, event invitations, automated order confirmations, and follow-up sequences. The system runs entirely within Google Workspace, requiring no external tools.

How It Works: Data Flow and Automation

Mail merge in Google Sheets relies on Google Apps Script, a JavaScript-based runtime environment built into Google's set. You write a script that loops through rows in a spreadsheet, pulls values from specific columns, and inserts them into a predefined email template.

For example, if your sheet has columns labeled "Name," "Email," and "Event Date," the script retrieves the value from column B in row 5, inserts it into the email body where <name> appears, and sends the message to the address in column C.

Scripts run on demand or on a schedule. You can trigger the merge manually via the Apps Script editor, or set it to run automatically using triggers. This enables recurring tasks like sending monthly updates or birthday messages.

Required Components and Setup

You need three core elements: a data source, a template, and a script. The data source is a Google Sheet with structured columns. The template is a Google Doc or plain text with placeholders. The script connects both and handles execution.

Start by creating a new Google Sheet. Enter headers like "First Name," "Last Name," "Email," and "Message Custom Field." Populate rows with actual data-e.g., "Sarah," "Johnson," "[email protected]," "Order #12345."

Next, create a Google Doc titled "Email Template." Inside, write a draft message using placeholders. For example:

Hello <first_name>,

Thank you for your order #<order_id>. Your shipment will arrive on <ship_date>.

Best regards,
Customer Support

Save the document and note its URL. You'll reference it in the script.

Script Structure and Execution

Open the Apps Script editor by going to Tools > Script Editor in your Google Sheet. Paste this minimal working script:

function sendMailMerge() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const data = sheet.getDataRange().getValues();
  const templateDocId = 'YOUR_TEMPLATE_DOC_ID'; // Replace with actual ID
  const template = DocumentApp.openById(templateDocId);
  const templateText = template.getBody().getText();

  const headerRow = 0;
  const emailCol = 2; // Adjust based on column index
  const nameCol = 0;

  for (let i = 1; i < data.length; i++) {
    const row = data[i];
    const name = row[nameCol];
    const email = row[emailCol];
    const message = templateText
      .replace('<first_name>', name)
      .replace('<order_id>', row[3])
      .replace('<ship_date>', row[4]);

    MailApp.sendEmail(email, 'Your Order Confirmation', message);
  }
}

Replace 'YOUR_TEMPLATE_DOC_ID' with the unique ID from the document's URL (e.g., https://docs.google.com/document/d/1aBcD.../edit → use 1aBcD...).

Run the function from the Apps Script editor. Authorize access when prompted. The script will loop through each row, replace placeholders, and send an email.

Common Mistakes and Fixes

One frequent error is misidentifying column indices. If "Email" is in column D (index 3), setting the variable to 2 causes the script to read the wrong cell. Always verify column positions using the spreadsheet's letter/number grid.

Another issue is incomplete placeholder replacement. If the template contains <first_name> but the script looks for <name>, the field remains unchanged. Use consistent naming and test with one row first.

A third problem is exceeding Gmail's daily sending limit. Google allows 500 emails per day from a single account. Sending 1,000 emails in one run fails after the 500th. Split large batches or use multiple accounts.

Non-obvious fix: Gmail requires the sender's email to be verified in the account settings. If you're using a non-Google email (e.g., @outlook.com), the script fails silently. Only verified addresses work.

Real-World Example: Event Invite Campaign

Suppose you're organizing a webinar and want to send 120 personalized invites. Your sheet has columns: "Full Name," "Email," "Registration ID," "Webinar Date."

Create a template doc with:

Hi <full_name>,

You've been invited to our exclusive webinar on <webinar_date>.

Use code <registration_id> to join.

See you there!

- Team

Set the script to loop through rows 1 to 120. Run it. The script sends 120 emails, each with unique personalization.

After running, check your Sent folder. Confirm that all messages arrived. If some fail, review the log in Apps Script (View > Logs) for error codes.

Comparison: Mail Merge vs. Email Templates

Mail merge differs from standard Gmail templates. Templates are static snippets saved in Gmail. They don't change per recipient. You can't insert changing data like names or IDs.

Mail merge dynamically generates content per row. It supports conditional logic too-e.g., send different messages based on whether a user signed up before or after a cutoff date.

Table below shows key differences:

Feature Mail Merge (Google Sheets) Gmail Templates
Data Source Spreadsheet with rows Manual entry or saved draft
Personalization Changing (per row) Static (same for all)
Automation Script-driven, scheduled Manual insertion
Scalability Handles thousands of emails Limited to 100+ manually sent

Mail merge scales efficiently. A single script can handle 10,000 emails with proper batching. Gmail templates cannot.

Security and Best Practices

Never hardcode sensitive data like passwords or API keys in scripts. Store secrets in the Apps Script Properties service or use Google Cloud Secret Manager.

Always test with a small batch first-start with 3-5 rows. Verify that placeholders resolve correctly and emails appear in the inbox.

Use error handling to catch failures. Wrap the email-sending loop in try-catch blocks. Log failed addresses to a separate sheet for later retry.

I once ran a mail merge for a client's customer onboarding campaign. After testing with five rows, I noticed the "Subject" field wasn't updating. The script was replacing <subject> but the template had <subject_line>. Correcting the placeholder name fixed it. Small mismatch, big impact.

Integration with Other Tools

Mail merge in Google Sheets integrates with Google Forms for lead collection. When someone submits a form, a new row appears in the sheet. A script can trigger the merge automatically.

For advanced workflows, connect to Google Calendar to auto-schedule follow-ups. Or use Google Sheets to track responses and update the database accordingly.

For high-volume campaigns, consider using Google Workspace add-ons like Mail Merge by MailerLite or Zapier integrations. These offer visual interfaces and better error reporting but require third-party access.

When managing multiple senders, avoid mixing accounts. Use one Gmail account per campaign to prevent rate limits and confusion in logs.

Final Notes

Mail merge in Google Sheets is a useful tool for automating personalized outreach. It combines the flexibility of spreadsheets with the reach of Gmail. With correct setup, it delivers accurate, scalable communication.

Follow the steps above to build your own. Start small, test thoroughly, and scale safely. The process is repeatable across teams, departments, and use cases.

Once configured, you can reuse the script for similar campaigns. Just update the data sheet and template. No need to rewrite code.

For advanced routing, explore forwarding emails programmatically to internal teams based on content. This extends the workflow beyond delivery.


Related Posts