Skip to content

How to Set Rules for a Mail Merge in Gmail and Google Sheets

A rule tells Mail Merge to change an email based on the values in a row of your sheet. For example, a guest with a confirmed place gets an arrival time, and a guest on the waiting list gets their position on the list. Write your template and add your merge fields first. Then add the rules.

Mail Merge has a rule for each of these tasks:

You want to Use Example
Type a value once and show it in many places A variable The workshop date and the name of the teacher
Show different text to different rows An if and else condition One arrival time for the morning, one for the afternoon
Print the number of the row The Row Number field A booking number, like WL-2
Count only the rows in a group A counting column Each guest’s position on the waiting list
Show several items in one email A list in a cell The three pieces a guest will glaze
Show a different table to each group A table for each group The timetable for the guest’s session
Leave out rows that match a value A filter on the sheet No email for a cancelled booking

Maya at Willow Lane Studio runs a glazing workshop on Saturday, March 20. Her sheet has one row for each booking:

First Name Email Address Session Guests Status Pieces
Angus angus@example.com Morning 2 Confirmed Mug, Bowl, Vase
Daniel daniel.okafor@example.net Afternoon 1 Confirmed Teapot
Sofia sofia@example.org Morning 1 Waitlist
Tom tom@example.net Afternoon 1 Cancelled
Aiko aiko@example.com Afternoon 3 Waitlist

She writes one email with five rules in it:

{% assign day = "Saturday, March 20" %}
{% assign teacher = "Elizabeth" %}
Hi {{First Name}},
{% if {{Status}} == "Confirmed" %}
Your place at the glazing workshop on {? day ?} is confirmed. Your booking number is WL-{{Row Number}}.
{% if {{Session}} == "Morning" %}
Please arrive by 9:30 am. {? teacher ?} starts at 10 am.
{% else %}
Please arrive by 1:30 pm. {? teacher ?} starts at 2 pm.
{% endif %}
{% assign pieces = {{Pieces}} | split: ", " %}
You will glaze:
{% for piece in pieces %}
{? forloop.index ?}. {? piece ?}
{% endfor %}
{% else %}
The glazing workshop on {? day ?} is full. You are number {{Waitlist #}} on the waiting list.
{% endif %}
Maya

Each row gets a different email from the same template:

Row What the email says
Angus Confirmed, booking number WL-2, arrive by 9:30 am, three pieces to glaze
Daniel Confirmed, booking number WL-3, arrive by 1:30 pm, one piece to glaze
Sofia The workshop is full, number 1 on the waiting list
Tom No email. Maya filters the sheet to skip his row
Aiko The workshop is full, number 2 on the waiting list

Type a Value Once and Use It in Several Places

Section titled “Type a Value Once and Use It in Several Places”

Maya sends the same email before every workshop. Only the date and the teacher change, and neither is in her sheet. A variable holds each value at the top of the message, so Maya types the date once.

  1. Go to Extensions > Mail Merge with Attachments > Open Mail Merge, open your campaign and move to the Template step.

  2. Click at the start of the Message box. Type one {% assign %} tag for each value, with a name on the left and the value in straight quotes:

    {% assign day = "Saturday, March 20" %}
    {% assign teacher = "Elizabeth" %}
  3. Click where the value should appear and type the name between {? and ?}. Repeat for every place that needs the value.

    Your place at the glazing workshop on {? day ?} is confirmed.
    Please arrive by 9:30 am. {? teacher ?} starts at 10 am.
  4. Click Send Test Email and send the email to yourself. Every {? day ?} should read Saturday, March 20.

Before the next workshop, Maya changes the two lines at the top. Every sentence that uses {? day ?} or {? teacher ?} changes with them.

Mail Merge doesn’t ask for the value when the campaign runs. Change the {% assign %} tag before you send.

A variable works only in the box where you type the tag. To use the date in the Subject too, type the tag at the start of the subject:

Subject
{% assign day = "Saturday, March 20" %}Glazing workshop on {? day ?}
Output
Glazing workshop on Saturday, March 20

Morning guests and afternoon guests need different arrival times. A condition checks the Session column and picks the sentence for each row.

  1. Click where the text should go in the Message box.

  2. Type the opening tag. Put the column title in double curly braces and the value to compare in straight quotes:

    {% if {{Session}} == "Morning" %}
  3. Type the text for rows that match. Then type {% else %} and the text for every other row:

    {% if {{Session}} == "Morning" %}
    Please arrive by 9:30 am. {? teacher ?} starts at 10 am.
    {% else %}
    Please arrive by 1:30 pm. {? teacher ?} starts at 2 pm.
  4. Type the closing tag after the last sentence:

    {% endif %}
  5. Click Send Test Email. The test uses row 2 of your sheet, so change the Session cell in that row to check both sentences.

Angus is in the morning session and reads Please arrive by 9:30 am. Daniel is in the afternoon session and reads Please arrive by 1:30 pm.

The tag can compare a column in several ways:

Comparison You write True for
Equal to {% if {{Session}} == "Morning" %} Angus and Sofia
Not equal to {% if {{Status}} != "Confirmed" %} Sofia, Tom and Aiko
Greater than {% if {{Guests}} > 1 %} Angus and Aiko
Greater than or equal to {% if {{Guests}} >= 2 %} Angus and Aiko
Less than or equal to {% if {{Guests}} <= 1 %} Daniel, Sofia and Tom
Is blank {% if {{Pieces}} == "" %} Sofia, Tom and Aiko
Is not blank {% if {{Pieces}} != "" %} Angus and Daniel

Put text in quotes, but not numbers. Capital letters must match, so "morning" doesn’t match a cell that says Morning.

To choose between three results, add {% elsif %} between the opening tag and {% else %}:

{% if {{Status}} == "Confirmed" %}
Your place is confirmed.
{% elsif {{Status}} == "Waitlist" %}
You are number {{Waitlist #}} on the waiting list.
{% else %}
Your booking is cancelled.
{% endif %}

Conditional Text covers and, or, conditions in the subject and files attached only for some rows.

The {{Row Number}} field prints the number of the row in your sheet. The field is always available, and you don’t need a column for it.

Your booking number is WL-{{Row Number}}.
Output
Your booking number is WL-2.

Angus is in row 2, below the header row, so his number is 2. To start the count at 1, subtract 1 with a filter:

You are guest {? {{Row Number}} | minus: 1 ?} on our list.
Output
You are guest 1 on our list.

Sort the sheet before you send, for example by sign-up time. The row number follows the order of the sheet and counts hidden rows too.

A waiting list needs its own count. Sofia is in row 4 of the sheet, but she is first on the waiting list. A formula column counts only the rows that say Waitlist.

  1. Type Waitlist # in the first empty cell of row 1. In Maya’s sheet, that cell is G1, and Status is column E.

  2. Click G2 and enter this formula:

    =IF(E2="Waitlist", COUNTIF(E$2:E2, "Waitlist"), "")
  3. Select G2, then drag the small blue dot at its bottom-right corner down to the last row. Sofia’s cell shows 1 and Aiko’s cell shows 2. The other cells stay empty.

  4. Write the new column in your template as a merge field:

    You are number {{Waitlist #}} on the waiting list.

Sofia reads You are number 1 on the waiting list. Aiko reads You are number 2 on the waiting list.

Mail Merge sends one email for each row. To show several items in one email, keep the items in one cell of that row, with a comma between them. A loop then prints one line for each item.

Angus made three pieces in an earlier class, so his Pieces cell says Mug, Bowl, Vase.

  1. Click where the list should go in the Message box.

  2. Type an {% assign %} tag that cuts the cell into a list at each comma:

    {% assign pieces = {{Pieces}} | split: ", " %}
  3. Type the loop. Mail Merge repeats the line between {% for %} and {% endfor %} once for each item:

    You will glaze:
    {% for piece in pieces %}
    {? forloop.index ?}. {? piece ?}
    {% endfor %}
  4. Click Send Test Email and check that every item is on its own line.

Angus reads:

You will glaze:
1. Mug
2. Bowl
3. Vase

Daniel has one piece, so his list has one line. {? forloop.index ?} prints the number of each line.

Your items may be in separate rows on another tab, with one row for each piece. A formula can collect each person’s items into one cell.

Say Maya has a Pieces tab with the guest’s email address in column A and the piece in column B. In the Pieces column of her mailing list, she enters this formula in row 2 and fills it down:

=IFERROR(TEXTJOIN(", ", TRUE, FILTER(Pieces!B:B, Pieces!A:A=B2)), "")
Output
Mug, Bowl, Vase

FILTER finds every piece whose email address matches B2. TEXTJOIN joins the pieces with commas. IFERROR leaves the cell empty for a guest with no pieces.

When a whole group shares the same list, keep the list in its own tab and add the tab to the email as a table. Maya’s Workshop Plan spreadsheet has a Morning tab and an Afternoon tab. Each tab has that session’s timetable in cells A1 to B5.

{{Embed Range, 1AbC…789, address={{Session}}!A1:B5}}

Angus gets the table from the Morning tab, and Daniel gets the table from the Afternoon tab. See how to add a table from Google Sheets for the steps.

Tom cancelled his booking, so he should get no email. Mail Merge leaves out every row that a filter hides.

  1. Click any cell in your data, then open the Data menu and choose Create a filter. A filter icon appears in each column header.

  2. Click the filter icon in the Status column and open Filter by condition.

  3. Choose Text does not contain and type Cancelled.

  4. Click OK. Tom’s row disappears from view.

  5. Run your campaign as usual. Mail Merge sends to the rows you can see.

A filter says which rows to keep, so choose the opposite of the rows you want to skip:

To skip rows where Choose Type
Status is Cancelled Text does not contain Cancelled
Pieces is empty Is not empty
Guests is above 2 Less than or equal to 2
Session is Afternoon Text is exactly Morning

To skip one or two rows by hand, type SKIP in the row’s Mail Merge Status cell. See how to email specific rows for hidden rows and other ways to skip.

  • Rules work in the message, the subject and the sender name. Mail Merge doesn’t read tags in Send to, CC, BCC or Reply-to.
  • A tag can’t stop an email. A condition changes the text, but the row still gets the email. Only a hidden row, a filtered row or a SKIP status leaves a row out.
  • Type tags as plain text with straight quotes. A tag that is partly bold or coloured stops working.
  • The row number changes when you sort the sheet or insert a row. Sort first, and send the booking numbers once.
  • Google Sheets doesn’t apply a filter again when a cell changes. If a guest cancels after you set the filter, click the filter icon and click OK again.
  • An empty list prints no lines. Sofia has no pieces, so a loop over her Pieces cell prints nothing.

The status column shows “Scriptlet error in the template”

Section titled “The status column shows “Scriptlet error in the template””

A tag has a mistake, so Mail Merge didn’t send the email for that row. The usual cause is an {% if %} with no {% endif %}, or a {% for %} with no {% endfor %}. Fix the template and run the campaign again. See the list of error messages.

The {% assign %} tag is in the message, and a variable works only in the box where you type the tag. Type the tag at the start of the Subject too.

Check that the value in quotes matches the cell, with the same capital letters and no extra space. For > or <, the cell must hold a plain number with no comma, currency sign or unit.