Google Sheets Script Spreadsheet Events: How to Use
In this article
Yes, Google Sheets can run script code automatically when something happens in your spreadsheet, such as a cell being edited or a form being submitted. These are called simple triggers and installable triggers in Google Apps Script, and setting them up takes just a few minutes. This guide shows you how to open the script editor, add the right function names, and use the triggers panel so your spreadsheet reacts to events like edits, changes, form submissions, and opening the file.
What You'll Need
- A Google account with access to Google Sheets
- A spreadsheet you own or can edit (trigger management needs edit access; some trigger types need the owner)
- A web browser, since Apps Script editing works best on desktop
- Basic familiarity with JavaScript helps but is not required for the examples here
Step-by-Step
- Open your spreadsheet in Google Sheets on a desktop browser and make sure you are signed in.
- Open the script editor by choosing Extensions > Apps Script from the menu bar. A new browser tab opens with the editor. Menu labels can differ slightly by Google account type or region, but Extensions > Apps Script is the current standard path.
- Delete any placeholder code in the editor, then add a simple trigger function. Simple triggers use fixed function names and run without any setup beyond saving the script. For example, to react to any edit:
function onEdit(e) { e.range.setNote('Edited on ' + new Date()); } - Save the project by clicking the floppy disk icon or pressing Ctrl+S (Cmd+S on Mac). Name the project if prompted.
- Test it: switch back to your spreadsheet and edit any cell. The onEdit function should run and add a note to the cell you changed.
- Learn the other simple trigger names.
onOpen(e)runs when the spreadsheet is opened,onEdit(e)runs on any cell edit,onChange(e)runs on broader changes such as row insertion, andonFormSubmit(e)runs when a linked Google Form gets a response. Each must be spelled exactly, with matching capitalisation. - Use the event object
einside the function to get context. For onEdit,e.rangeis the edited cell,e.valueis the new value, ande.sourceis the spreadsheet. Guard against missing values, since e.value is undefined when a cell is cleared. - When you need more power, such as sending emails or accessing other files, create an installable trigger instead, because simple triggers cannot use services that require authorisation. In the script editor, click the clock icon in the left sidebar to open Triggers.
- Click Add Trigger (bottom right), choose your function name under "Choose which function to run", and pick an event source such as "From spreadsheet".
- Select the event type: On open, On edit, On change, or On form submit. Leave the deployment setting as Head, and click Save.
- Authorise the trigger the first time it saves. Google shows a permissions prompt: choose your account, and if you see a "Google hasn't verified this app" screen, click Advanced and then the link to continue to your own script. This is normal for personal scripts you wrote yourself.
- Verify it works by performing the matching action in the spreadsheet. For an On form submit trigger, submit a test response through the linked Google Form.
- To manage or remove triggers later, reopen Extensions > Apps Script, click the clock icon, and use the three-dot menu next to any trigger to edit or delete it.
Common Mistakes
- Misspelling the simple trigger function name.
oneditorOnEditwill never fire; it must be exactlyonEdit. - Trying to send email or call external services from a simple trigger. Simple triggers run with limited permissions and cannot authorise; use an installable trigger instead.
- Forgetting to save the script. Triggers only run the saved version, not the code sitting unsaved in the editor.
- Creating both a simple onEdit function and an installable On edit trigger, so the code runs twice on every edit. Pick one.
- Assuming e.value exists when a cell was cleared. Check for undefined before using it.
- Expecting onFormSubmit to work when the form is not actually linked to the sheet. The trigger fires only for responses landing in that spreadsheet through a form's linked-sheet setup.
Tips
- Use
e.range.getA1Notation()in a temporaryLogger.log()orconsole.log()call to see exactly what fired your trigger while debugging. View logs in the editor's Executions panel. - Restrict triggers to a specific sheet with
e.range.getSheet().getName()so edits on other tabs are ignored. - Keep trigger functions fast. Google limits how long a script can run, so avoid heavy loops over entire columns; use
getLastRow()to bound your range. - The Executions panel (left sidebar in the editor) shows every trigger run, its status, and duration. It is the fastest way to see if a trigger fired at all.
- For time-based tasks like daily reports, use the same Triggers panel with "Time-driven" as the event source instead of a spreadsheet event.
Troubleshooting
If your trigger does not fire at all, first confirm the script is saved and the function name is spelled correctly, then check the Executions panel for any record of a run. No record means the trigger never fired; a failed record means it fired and errored.
If the trigger runs but fails on authorisation, re-open the Triggers panel and edit the trigger to re-grant permissions, or run the function once manually from the editor so the permission prompt appears.
If edits you make through a script or formula recalculation do not trigger onEdit, that is expected: onEdit fires on user edits, not on changes made by scripts or by formulas recalculating.
If a script you pasted from elsewhere will not run, check the Executions log for the error line, and make sure any functions or sheet names it references actually exist in your file.
If a trigger fires but behaves oddly on form submissions, confirm the form response actually lands in that sheet, and remember that the form response sheet inserts a new row rather than editing in place.
Frequently Asked Questions
What is the difference between onEdit and onChange?
onEdit fires when a user changes cell values. onChange fires on structural changes too, like inserting or deleting rows and columns, and it receives a changeType such as "INSERT_ROW" instead of an edited range.
Why does my simple trigger not send email?
Simple triggers run in a restricted mode that blocks services requiring authorisation, including Gmail. Create an installable trigger for the same function through the Triggers panel and authorise it.
Do spreadsheet triggers work on the mobile app?
Yes, triggers fire regardless of where the edit comes from, including the Google Sheets mobile app. You just cannot create or edit triggers from mobile; use the desktop script editor for that.
Can a trigger run when a specific cell changes?
Not directly. Triggers are spreadsheet-wide, so inside onEdit you check whether the edited range matches your target cell or sheet, and exit early if it does not.
Can other people's edits fire my triggers?
Yes. Simple triggers like onEdit fire for any editor of the spreadsheet, but they run under the account of whoever is editing, which can matter for permissions.
Is there a limit on how often triggers run?
Yes, Google Apps Script has quotas on total trigger runtime and execution time per day. Heavy spreadsheets with constant edits can hit these; check the Executions panel for quota errors.