Skip to main content

How to Use Conditional Fields in Email Templates

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"))))

Comments

Popular posts from this blog

How to Get the Quiz Score in Google Forms with Apps Script

Teachers can easily create an online quiz using Google Forms and students can view their test scores immediately after form submission. Teachers can use Google Forms to create an online quiz and students can view their test scores immediately after  form submission . With Apps Script, you can set up automatic  email notifications  and send quiz scores to parents after a student has taken the quiz. Here’s a sample Google Script that will iterate through every answer in the most recent Google Form response and log the max score (points) of a gradable question and the score for the respondent’s submitted answer. function getGoogleFormQuizScore ( ) { // Returns the form to which the script is container-bound. var form = FormApp . getActiveForm ( ) ; // Get the most recently submitted form response var response = form . getResponses ( ) . reverse ( ) [ 0 ] ; // Gets an array of all items in the form. var items = form . getItems ( ) ; for ( var...

Let People Quickly Save your Events on their Calendars

Create Add to Calendar links for emails and websites and let users quickly save your events on their own Google Calendar, Outlook or Yahoo Calendar. You are organizing an online event - maybe a meeting on Zoom or a training session hosted on Google Meet - and you would like the attendees to add the event to their own calendars. Once added to their calendar, the event will act as an automatic reminder and attendees will get a notification when the conference is about to start. There are two way to go about this: You can create a new meeting in your online calendar (Google, Outlook or any other calendar) and add the individual attendees as guests so the event automatically gets added to their calendar as well. You can include an “Add to Calendar” link or button in your email messages,  forms  and website pages. Anyone can click the link to quickly save your event on to their calendars - see  live demo . Create Add to Calendar Links for Emails and Websites The  Add to C...

How to Test your Eyes using the Computer

They say that you should get your eyes checked every two years but if haven’t had the chance to see a doctor all this time, you can test your vision on your computer as well. Of course these self eye tests are no substitute for visiting your doctor but if you follow the steps well, you may get some idea about how good (or bad) your vision is.  Test your Eyes Online with the Snellen Eye Chart The Snellen Eye Chart Most of us are familiar with the Snellen Chart that has rows of alphabets of different sizes – you read these letters from a distance, usually twenty feet, and the smallest row that you can recognize accurately indicates whether you have normal vision or not. The various eye testing tools that are available online make use of the same Snellen chart. Test your Eyesight Online You should start with University at Buffalo’s  IVAC tool . Use a physical ruler to measure the length of the line on the screen (the length will vary depending on your screen resolution). Also mea...