Mail merge with google docs
Mail merge with Google Docs! While Google Docs doesn't have a built-in mail merge feature like Microsoft Word, you can still achieve similar results using Google Sheets and Google Docs. Here's a step-by-step guide to help you do a mail merge with Google Docs:
Prerequisites:
- Google Sheets: You'll need a Google Sheet to store your data.
- Google Docs: You'll need a Google Doc to create your document.
- Google Apps Script: You'll need to use Google Apps Script to automate the mail merge process.
Step 1: Prepare your data in Google Sheets
Create a Google Sheet with the following columns:
- Column A: First name
- Column B: Last name
- Column C: Email address
- Column D: Document content (e.g., a template with placeholders for names and addresses)
Step 2: Create a Google Doc template
Create a new Google Doc and add placeholders for the data you want to merge. For example:
- {{First Name}}
- {{Last Name}}
- {{Email Address}}
Step 3: Write a Google Apps Script
In your Google Sheet, go to Tools > Script editor. This will open the Google Apps Script editor. Create a new script by clicking on the "Create" button.
In the script editor, paste the following code:
function mailMerge() {
var sheet = SpreadsheetApp.getActiveSheet();
var doc = DocumentApp.openById("YOUR_DOC_ID");
var data = sheet.getDataRange().getValues();
for (var i = 1; i < data.length; i++) {
var row = data[i];
var firstName = row[0];
var lastName = row[1];
var emailAddress = row[2];
var documentContent = row[3];
var docBody = doc.getBody();
docBody.replaceText("{{First Name}}", firstName);
docBody.replaceText("{{Last Name}}", lastName);
docBody.replaceText("{{Email Address}}", emailAddress);
var newDoc = doc.copy();
newDoc.setName(firstName + " " + lastName);
newDoc.saveAndClose();
}
}
Replace "YOUR_DOC_ID" with the ID of your Google Doc.
Step 4: Run the script
Go back to your Google Sheet and click on the "Run" button in the script editor or press Ctrl+Enter. The script will run and create a new Google Doc for each row in your sheet, replacing the placeholders with the corresponding data.
Step 5: Send the documents
You can now send the generated documents to the respective email addresses using Google Apps Script's Gmail service. You'll need to set up a Gmail account and enable the Gmail API in your Google Cloud Console.
Here's an updated version of the script that sends the documents:
function mailMerge() {
//... (rest of the script remains the same)
for (var i = 1; i < data.length; i++) {
//... (rest of the script remains the same)
var newDoc = doc.copy();
newDoc.setName(firstName + " " + lastName);
newDoc.saveAndClose();
var email = GmailApp.sendEmail(emailAddress, "Your Subject", "Your Message", { attachments: [newDoc.getAs("application/pdf")] });
}
}
This script will send each document as a PDF attachment to the corresponding email address.
That's it! You've successfully done a mail merge with Google Docs using Google Sheets and Google Apps Script.