How to Use Conditional Fields in Email Templates

H
Published in: Mail Merge for Gmail - Document Studio

The Mail Merge and Document Studio add-ons let you send personalized emails with the help of template markers.

The markers automatically create a mapping between the column names in the Google Sheet and the variable fields in your email message. When the email is sent, the marker fields in the email message are replaced with the values from the respective columns of the sheet.

Let’s say you have a column titled First Name in your Google Sheet and your email message body has a marker that says Dear {{First Name}}, (also notice the comma in the end).

Template Fields in Mail Merge

Fill-in Fields

If your Google Sheet rows has a value, say Alex, the text in the email message would read Dear Alex,. However, if the first name is not available for a particular row, the variable field would be replaced with a blank value and this first line in the email message would thus read Dear <space>, - something that you should totally avoid in personalized emails.

There are two ways to deal with this issue. You can either clean up your data before running merge, or you can use formulas in Google Sheets to offer an alternate value in case the original field doesn’t have a value. Let me explain:

  1. Add a new column in the Google Sheet title “Greeting”

  2. Add a formula in row #2 of the Greeting column.

=IF(ISBLANK(A2),"Hello",CONCATENATE("Dear", " ", A2))

The formula basically looks at the Name column, it is blank, the greeting is set to “Hello” else it uses the default “Hello FirstName” value.

  1. Now edit your email template and replace “Dear {{Name}},” with “{{Greeting}},“.

You can either copy-paste the formula in the remaining cells of the column manually or use the ArrayFormula function to copy it down for you.

If..Then..Else

The technique can be extended to add more customization to your email message. For instance, you may choose a different greeting in your email subject based on the country of the recipient.

Email Greetings

If the country is in column B, the Greeting formula would be:

=IFNA(
  IFS(
   B2 = "USA", "Hello",
   B2 = "Spain", "Hola",
   B2 = "India", "Namaste"
  ), "Greetings")

Calculated Fields

The templates fields in the email message are dumb and merely get replaced by values in the Google Sheet. If you wish to include any logic or calculations in the template fields, it should be done in the sheet itself.

Let me give you another example.

Conditional Fields in Mail Merge

The Google Sheet records the invoice details and sends email reminders for payments. Using the magic of Google Sheet formulas and template markers, the text of the email message can be dynamically changed based on when the invoice is due. If the due date has already passed, we send a different message.

First add a new column (say, Days Left) that calculates the number days between now and the invoice due date. Add this formula in row #2 of the column. It will only fill values when the due date is available.

=ArrayFormula(IF(ISBLANK(C2:C),"", ROUND(C2:C-TODAY())))

Add a new “Invoice Status” column and again use the ArrayFormula function to get the text for the email message body and subject.

=ArrayFormula(
  IF(ISBLANK(C2:C), "" ,
     IF(D2:D>0, CONCAT("due on ",TEXT(C2:C,"mmmm dd, yyyy")),
                CONCAT(ABS(D2:D)," days past due"))))

See more Google Sheets solutions.

Published in: Mail Merge for Gmail - Document Studio

Looking for something? Find here!

Meet the Author

Web Geek, Tech Columnist
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in GSuite and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. Read more on Lifehacker and YourStory

Get in touch