Adding a script to Google Sheets is easy! In just a few clicks you can add any script. Simply follow the steps below.
For this example, we will add a script to Google Sheets that allows you to automatically send emails.
Apps Script is a tool created by Google that allows you to run code scripts in your spreadsheet .
There are several things we can use Apps Script for such as creating new functions, adding custom menus or sidebars, building add-ons, integrating with other Google Workspace applications and a lot more.
Our script for sending emails automatically would be:
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet1=ss.getSheetByName('Sheet1');
var sheet2=ss.getSheetByName('Sheet2');
var subject = sheet2.getRange(2,1).getValue();
var n=sheet1.getLastRow();
for (var i = 2; i < n+1 ; i++ ) {
var emailAddress = sheet1.getRange(i,2).getValue();
var name=sheet1.getRange(i,1).getValue();
var ServiceAcquired=sheet1.getRange(i,3).getValue();
var message = sheet2.getRange(2,2).getValue();
message=message.replace("<name>",name).replace("<service>",ServiceAcquired);
MailApp.sendEmail(emailAddress, subject, message);
}
}
This is how it would look like in our editor.
Refer to our Send Emails Automatically In Google Sheets article for more information on how to edit this script and what it does.
You can name it however you want to and simply press the save icon to save it.
The run code button will be beside the save icon. This will then prompt you to grant access to your code since it is a custom and unknown script
To set up permissions: Click Review Permissions. Go to Advanced Settings -> Click “Go to Send Email” and allow the permissions. This will run your program and email your recipients.
Our script example will automatically send emails using Gmail based on our worksheet contents. We can see below how multiple emails were easily sent after running our script.
Using Apps Script, we can also use other scripts based on what we want to achieve with our data. Here are other Lido articles that will help you get started in automating with Google Sheets:
-Connect MongoDB to Google Sheets with Script
We hope this article has helped you and given you a better understanding of how to add a script to Google Sheets. You might also like our articles on how to export Google Calendar to Google sheets and how to create a Google Sheets button to run a script.
To optimize your workflow, we recommend reading our guide on how to send bulk emails from Google Sheets.