Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Google Sheets Automation: How to Script It

· 5 min read · Home Automation

In this article
  1. What Can You Automate in Google Sheets?
  2. How to Create Your First Script
  3. Using Triggers to Run Automatically
  4. Macros vs. Apps Script
  5. Common Problems and How to Fix Them
  6. Costs and Limits
  7. Frequently Asked Questions

To script Google Sheets automation, you use Apps Script, a JavaScript-based platform built into Google Workspace, accessed via Extensions > Apps Script. From there, you can write custom functions, create macros, or set time-driven triggers to run tasks automatically—like sending emails, importing data, or formatting rows—without manual intervention.

What Can You Automate in Google Sheets?

Apps Script can control nearly every aspect of your spreadsheet. Common automations include:

  • Data import/export: Pull data from external APIs (e.g., weather, stock prices) or push data to other Google services like Docs or Gmail.
  • Email notifications: Send an email when a cell changes, when a form is submitted, or when a deadline approaches.
  • Report generation: Create a PDF of a specific range and email it to a list of recipients on a schedule.
  • Data cleaning: Remove duplicates, trim whitespace, or standardize date formats across a sheet.
  • Conditional actions: Highlight rows, move data to another sheet, or update a dashboard based on criteria.

How to Create Your First Script

Open your Google Sheet, click Extensions > Apps Script. This opens the editor in a new tab. You’ll see a blank function myFunction() {}. Replace it with a simple example:

function sendWelcomeEmail() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var email = sheet.getRange("A2").getValue();
  var subject = "Welcome!";
  var body = "Thanks for signing up.";
  MailApp.sendEmail(email, subject, body);
}

Click the save icon, give your project a name, then run the function by selecting it from the dropdown and clicking the play button. The first run will ask for permissions—review them and allow. This sends an email to the address in cell A2. To make it run automatically, you’ll add a trigger (see below).

Using Triggers to Run Automatically

Triggers are the heart of automation—they tell your script when to run. Apps Script supports two types:

  • Simple triggers: Like onEdit(e) or onOpen(), these run automatically on certain actions. They cannot access services that require authorization (like Gmail) and have a 30-second execution limit.
  • Installable triggers: Set up via the editor’s clock icon (Triggers). They can run on time (every hour, daily, weekly) or on events like form submit or spreadsheet open. They have longer time limits and can use any service.

To set an installable trigger: click the clock icon in the Apps Script editor, click Add Trigger, choose the function, select the event source (e.g., Time-driven), and set the frequency. For example, to run a report every weekday at 8 AM, choose Day timer and set the hour.

Macros vs. Apps Script

If you’re new to coding, start with Macros. Google Sheets lets you record a sequence of actions (formatting, data entry, sorting) and replay them. Go to Extensions > Macros > Record macro. After recording, you can assign it to a button or run it from the menu. Macros are essentially Apps Script functions behind the scenes—you can view the generated code in the Apps Script editor. However, macros only replay recorded steps; they can’t make decisions or use logic. For conditional automation, you need to write or edit the script.

Common Problems and How to Fix Them

Beginners often hit these issues:

  • Permissions errors: If your script uses Gmail or other services, you must authorize it. If you see “Exception: You do not have permission to call MailApp.sendEmail,” re-run and accept the prompt. For published scripts, you may need to verify the app if it’s used by many users.
  • Triggers not firing: Check that the trigger is set correctly and that the function name matches exactly. Also, simple triggers like onEdit won’t run if the edit is made by a script or if the user lacks edit access.
  • Execution timeouts: Free accounts have a 6-minute limit per execution. If your script runs longer, break it into chunks or use a time-based trigger to process a batch each run.
  • Data not updating: If you’re using getValue() in a loop, it’s slow. Use getValues() to fetch a range into an array, process it, then write back with setValues().

Costs and Limits

Apps Script is free for personal and business Google Workspace accounts. However, there are quotas: for consumer accounts, you get 20,000 email recipients per day (if using Gmail), and 90 minutes of execution time per day. Business accounts have higher limits (e.g., 1,500 emails per day for Gmail). If you exceed these, your script will throw an error. For heavy data processing, consider BigQuery or external services, but for most spreadsheet tasks, the free limits are sufficient.

Frequently Asked Questions

Can I automate Google Sheets without coding?
Yes, use Macros for simple repetitive tasks. For more complex logic, you’ll need to write Apps Script, but you can find many free templates online that you can copy and adapt.

How do I run a script when a form is submitted?
In the Apps Script editor, click the clock icon, add a trigger, select the function that processes the form response, and choose “From spreadsheet” > “On form submit.” Ensure the form is linked to the spreadsheet.

Can I schedule a script to run every day?
Yes, use an installable time-driven trigger. In the trigger setup, choose “Time-driven” and then “Day timer” to pick a specific hour. You can also use “Week timer” or “Month timer.”

Why is my script not sending emails?
Check that you’ve authorized the script (run it once manually), that the email address is valid, and that you haven’t exceeded the daily email quota. Also, ensure you’re using MailApp.sendEmail() correctly with the recipient as the first argument.

Is Apps Script the same as Excel VBA?
No, Apps Script uses JavaScript, while Excel uses VBA. The concepts are similar (macros, triggers, object model), but the syntax and services differ. If you know VBA, you can learn Apps Script quickly.

Can I use Python to automate Google Sheets?
Yes, you can use the Google Sheets API with Python, but that requires setting up a Google Cloud project and OAuth credentials. Apps Script is simpler for most users because it’s integrated directly into the sheet.