Google Sheets Script Triggers: Setup and Usage Guide
In this article
Google Sheets script triggers allow you to automate the execution of Apps Script functions based on specific events, such as opening a spreadsheet, editing a cell, or a specific time interval. To set one up, you write a function in the Apps Script editor and then either name it as a simple trigger (like onEdit) or configure an installable trigger via the Triggers menu in the Apps Script dashboard. This removes the need to manually run scripts, enabling the spreadsheet to react dynamically to data changes or schedule recurring reports automatically.
What You'll Need
- A Google account with access to Google Sheets.
- A spreadsheet containing the data you wish to automate.
- Basic familiarity with JavaScript (the language used by Google Apps Script).
- Chrome or another modern web browser.
Step-by-Step
- Open the Apps Script Editor: Open your Google Sheet. In the top menu, navigate to Extensions > Apps Script. This opens a new tab with the script editor where you will write your code. Note that menu labels may differ slightly depending on your region or account type.
- Write Your Function: Create the function you want to trigger. For example, if you want a script to log the date whenever a cell is changed, you might write a function that identifies the active cell and inserts a timestamp into the adjacent column. Ensure your function is saved by clicking the disk icon or pressing Ctrl+S (Cmd+S on Mac).
- Choose Your Trigger Type: Decide whether you need a Simple Trigger or an Installable Trigger.
- Simple Triggers: These are reserved function names. For example, naming a function
onOpen(e)makes it run every time the sheet is opened. Naming oneonEdit(e)makes it run every time a user changes a value. These require no setup but have strict limitations: they cannot access services that require authentication (like sending an email). - Installable Triggers: These are configured manually and can perform complex tasks, such as sending emails or accessing other Google Drive files, because they run as the user who created the trigger.
- Simple Triggers: These are reserved function names. For example, naming a function
- Configure an Installable Trigger: If you chose an installable trigger, look at the left-hand sidebar of the Apps Script editor and click the clock icon (Triggers). Click the + Add Trigger button in the bottom right corner.
- Set the Event Source: In the trigger configuration window, select the function you want to run from the first dropdown menu. Under "Select event source," choose between "From spreadsheet" (for event-based triggers) or "Time-driven" (for scheduled triggers).
- Define the Event Type: If you selected "From spreadsheet," choose the event type. Common options include "On open," "On edit," "On change" (which detects structural changes like adding a sheet), or "On form submit" (if the sheet is linked to a Google Form).
- Set the Schedule (Optional): If you selected "Time-driven," choose the frequency. You can set it to run every minute, hour, day, or week, or pick a specific date and time.
- Authorize the Script: Click Save. A pop-up window will appear asking you to choose a Google account. You will likely see a warning that "Google hasn't verified this app" because you wrote the script yourself.
- Test the Trigger: Return to your spreadsheet and perform the action that should trigger the script (e.g., edit a cell or refresh the page). Check the "Executions" tab in the Apps Script editor to see if the script ran successfully or if it encountered an error.
Common Mistakes
One of the most frequent errors is attempting to use a Simple Trigger (like onEdit) to perform an action that requires authorization, such as MailApp.sendEmail. Simple triggers run in a restricted mode; if the code tries to access a protected service, it will fail silently or throw an error in the execution log. The solution is to rename the function to something unique and set up an Installable Trigger instead.
Another common mistake is creating an infinite loop. This happens when an onEdit trigger modifies the same cell or range that triggers the script. For example, if a script is set to capitalize a cell whenever it is edited, the script's own action of capitalizing the cell counts as an "edit," which triggers the script again, and so on. To prevent this, always include a conditional check (an if statement) at the beginning of your script to ensure it only runs under specific conditions, such as checking if the edited cell is in a specific column.
Users also frequently forget to save the script before setting the trigger. If you create a trigger for a function name that hasn't been saved in the editor yet, the trigger will fail because it cannot find the corresponding code. Always save your project before navigating to the Triggers menu.
Tips
To make your triggers more efficient, use the event object (usually denoted as e in the function parameters). Instead of using SpreadsheetApp.getActiveRange(), which requires an extra API call to the server, use e.range. This provides the exact range that was edited directly from the event, significantly speeding up the execution time of your script.
When using time-driven triggers, be mindful of the Google Apps Script quotas. While daily quotas are generous for personal accounts, running a script every single minute can quickly exhaust your total trigger runtime per day. If your task can be handled hourly or daily, choose the longer interval to ensure your scripts remain operational throughout the day.
For complex projects, use a "Master Trigger" approach. Instead of having ten different triggers for ten different functions, create one main function that is triggered and then uses logic to call other helper functions based on the data it finds. This makes debugging easier and reduces the number of active triggers you need to manage in the dashboard.
Troubleshooting
If your trigger is not firing, the first step is to check the Executions tab in the Apps Script editor. This log shows every time a script was attempted, whether it completed, and the exact line of code where it failed. If there are no entries in the Executions log, the trigger itself is not firing, which usually means the event criteria (like the specific cell range) were not met.
If the log shows a "Permission Denied" error, you likely added a new service to your code (like adding a call to Google Calendar) after the trigger was already created. Installable triggers do not automatically update their permissions. You must delete the existing trigger and recreate it to trigger the authorization prompt again.
If the script runs but the changes don't appear in the sheet immediately, try refreshing the browser. Occasionally, the Google Sheets UI lags behind the backend script execution. If the issue persists, check for conflicting scripts or formulas in the sheet that might be overwriting the script's output.
If you receive a "Too many simultaneous executions" error, it means your script is taking too long to run and multiple triggers are stacking up. To fix this, optimize your code by reducing the number of calls to getValue() and setValue(). Instead, read a whole range into an array using getValues(), process the data in memory, and write it back once using setValues().
Frequently Asked Questions
Can a trigger run if the spreadsheet is closed?
Yes, but only if it is a Time-driven trigger or a trigger based on an external event like a Google Form submission. Event triggers like onOpen or onEdit require a user to actually be interacting with the spreadsheet for the event to occur.
How many triggers can I have per project?
Google imposes limits on the number of triggers per script and the total runtime per day. While the exact number can vary by account type (Workspace vs. Personal), most users can create dozens of triggers without issue, provided they aren't running high-frequency time-based tasks.
What is the difference between onEdit and onChange?
The onEdit trigger only fires when a user changes a value in a cell. The onChange trigger is broader; it fires when a user changes the structure of the spreadsheet, such as adding a new sheet, deleting a column, or changing the formatting of a cell.
Can I trigger a script from a button instead?
Yes. You can insert a drawing or image (Insert > Drawing), then right-click the object in the sheet and select "Assign script." Type the name of your function. This is not technically a "trigger" in the Apps Script dashboard sense, but it achieves a similar result of automating a function.
Do triggers work on the Google Sheets mobile app?
Simple triggers like onEdit generally work on mobile. However, custom menus created via onOpen do not appear in the mobile app, and some complex installable triggers may behave differently depending on the mobile OS version.