Site icon

Google sheets automation: How to Automate Email Updates (Google Scripts)

How to automate email updates google sheet

Tl;dr: How to send weekly email updates by sending email through Google sheets automation with a step by step explanation of the code

A lot of people use Google Sheets to collaborate on a wide number of topics. Many use it to manage their KPIs. Do you know what no one knows how to do that would be really cool if it was possible?

Sending an email update directly from Sheets!

Yes, that’s right you can and personally, I had no clue you could do that until this weekend. I used it to make a sweet OKR, PPP and KPI tracking sheet which would then send out an update of the PPPs on a weekly basis, at a time and date I picked.

I recommend you get it so you can see how it works to put this all in perspective.

You can get it here!

Now, this is going to be a functional class, so let’s just get into things.

Note, I don’t know how to code. I had to teach myself how to do this. So if you can’t code, trust me you can figure it out as I did!

The setup for Google sheets automation

Sheet setup

Open up Google Sheets.

Name the sheet ‘System.’

You want to add a few data points now.

In the first row write in: Email address, Message, Person, Email count and type 1 in E.

In the second row add the data

It should look like this:

Let me explain what’s happening here.

Of course, you can set up your sheet differently and write your own code, but I’m going to teach you how to do the exact example I have done.

Script setup

There is a hidden section (to me at least) called ‘Script editor.’ You will find it under Tools in the menu.

Open that up and that’s where you write the script. There will be only 3 lines when you open it for the first time.

It’s here where you are going to copy and paste the code. Let’s look at the script now to understand what it does.

The script

Here is a screenshot so you see what it should look like:

And now here is the code so you can paste it in.

 

function sendEmails() {
// Get the sheet where the data is, in sheet 'system'
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("System")
var startRow = 2; // First row of data to process since there is a header row
var numRows = sheet.getRange(1,5).getValue(); // Number of rows to process is set by a formula which counts rows
// Fetch the range of cells A2:B6 where the emails and messages are
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range to input into the mailing system
var data = dataRange.getValues();
// This processes the emails you want to send
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column is the email address
var message = row[1]; // Second column is the message
var subject = "READ: Weekly PPP update"; // This is the subject of the email
// This parses the data for the email to send
MailApp.sendEmail(emailAddress, subject, message);
}
}

What are these // things?

See those “//”? These are used to comment. Using // means to a computer ‘ignore this stuff, it’s for puny humans.’

I’ve used this commenting system to explain every line of code for you. But let’s go through it

The code explained

function sendEmails() {
// Get the sheet where the data is, in sheet 'system'
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("System")
startRow = 2; // First row of data to process since there is a header row
var numRows = sheet.getRange(1,5).getValue(); // Number of rows to process is set by a formula which counts rows
// Fetch the range of cells A2:B6 where the emails and messages are
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range to input into the mailing system
var data = dataRange.getValues();
// This processes the emails you want to send
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column is the email address
var message = row[1]; // Second column is the message
var subject = "READ: Weekly PPP update"; // This is the subject of the email
// This parses the data for the email to send
MailApp.sendEmail(emailAddress, subject, message);
}
}

That’s the code you need to play with.

I know this might be a lot to get your head around, but you can. If you need to change any code to do something else, then check the documentation. Yes, it seems like trying to understand a Latin word by being told to read the definition in a Latin dictionary, but it’s possible 😉

The documentation is here: https://developers.google.com/apps-script/reference/mail/

Automating the sending of the emails

Now, I spent ages Googling code to figure out how to send emails at the date and time I wanted. I did… then I realized that I wanted this to work in any country… so next mission was to figure out how to adjust the sheet to the time zone of each person! I figured that out… and then I found a really dumb, simple way of doing it! Gosh darn!

Let’s forget that code… Just do this.

If you want to change the timing or day, do this:

You need to click on the clock button. The settings are easy to understand:

Change ‘Every Friday’ to ‘Every Monday’ if you want to send on Monday. You can set things daily by changing week timer. You change the time button… well you guess. Fiddle, press save. Done. FML.

Authorising Gmail so it actually, like works

Now it will send the email from your Google account, so you need to authorise Google to do that for your account. You do this as follows:

 

The Google sheets automation does a lot of the heavy lifting

There is a lot of fancy stuff you can do if you can code good (I can’t and can do it with Excel). All the formatting etc for the message is done in Excel. What I had to do to make this super dynamic is really complicated and would take an hour to explain, so I’m not going to 😉 So figure out how you want to make use of your new superpower now.

I hope learning how to automate email updates through Google sheets automation was useful to you! If it was, be social and push one of the pretty buttons below:

Exit mobile version