Last Updated on February 1, 2023 by Jake Sheridan
Do you have a Google Sheets file containing email addresses and want to automate sending emails based on the values in certain cells? Don’t worry! Google Sheets makes this possible with a few lines of code using Apps Script. In this guide, we’ll show you how to use Apps Script to send an email based on a cell value in Google Sheets. This will save you time and help streamline your workflow.
Step 1: Open the Google Sheets File with Email Data
First, open the Google Sheets file that contains the email data.
Step 2: Access Apps Script
Next, select the Extensions > Apps Script option to open the Apps Script editor in a new tab.
Step 3: Add the Apps Script Code
Select the Code.gs file in the Apps Script editor and enter the following code:
function sendEmail() {
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("B2");
var emailAddress = emailRange.getValue();
}
The above code defines a function called sendEmail() that retrieves the string in cell B2 of the current project. The string is stored in the variable emailAddress.
Step 4: Set the Email Message and Subject
Next, we’ll create placeholder variables for the email message and subject using the following lines of code:
var message = 'This is an example message';
var subject = 'Email Alert from Google Sheets';
Step 5: Send the Email
The last line of the sendEmail() function is responsible for actually sending the email. The MailApp.sendEmail() function is a built-in method that sends an email to a specified recipient using the current user’s email address.
MailApp.sendEmail(emailAddress, subject, message);
Step 6: Save the Script
After entering the entire function, click the Save icon in the toolbar.
Step 7: Run the Function
Click the Run option to execute the sendEmail function.
Step 8: Check the Script Execution
The execution log should indicate that the function has completed its execution.
Step 9: Check the Sent Email
The recipient should now see an email similar to the one illustrated below.
Conclusion
This guide should provide you with all the necessary information to send an email based on a cell value in Google Sheets. Feel free to make a copy of this example file to test it out for yourself.
– Article written by Crawlan.com for bolamarketing.com –