0
0
Google Sheetsspreadsheet~5 mins

Triggers (onEdit, onOpen) in Google Sheets - Step-by-Step Guide

Choose your learning style9 modes available
Introduction
Triggers in Google Sheets let you run a script automatically when you open a sheet or edit a cell. This helps you automate tasks without clicking buttons.
When you want to show a welcome message every time someone opens the sheet
When you want to check data or update values automatically after a user changes a cell
When you want to log changes made to the sheet without manual effort
When you want to send notifications after edits happen
When you want to prepare or reset the sheet layout each time it opens
Steps
Step 1: Open
- Google Sheets file
Your spreadsheet is visible and ready
Step 2: Click
- Extensions menu > Apps Script
The Apps Script editor opens in a new tab
Step 3: Type
- Code editor
You enter a function named onOpen or onEdit
💡 Use exact function names: onOpen() or onEdit(e) for automatic triggers
Step 4: Write
- Inside the function
Your script runs automatically when the event happens
💡 For example, onOpen runs when the sheet opens; onEdit runs after any cell edit
Step 5: Save
- Apps Script editor (File > Save or Ctrl+S)
Your script is saved and ready to run
Step 6: Return
- Google Sheets tab
Your script triggers will run automatically on open or edit
Before vs After
Before
No script runs automatically when opening or editing the sheet
After
A welcome message appears on open or a cell value updates automatically after editing
Settings Reference
Simple Triggers
📍 Apps Script editor, function names onOpen() and onEdit(e)
Run code automatically when the sheet opens or a cell is edited
Default: No triggers run until you add these functions
Installable Triggers
📍 Apps Script editor > Triggers (clock icon) > Add Trigger
Create triggers with more permissions and options than simple triggers
Default: No triggers installed by default
Common Mistakes
Naming the function something other than onOpen or onEdit
Simple triggers only run if the function name is exactly onOpen or onEdit
Use the exact function names onOpen() or onEdit(e) for automatic triggers
Expecting onEdit to run without the event object parameter
onEdit needs the event object (e) to know what was edited
Define onEdit with a parameter like onEdit(e) to access edit details
Trying to run scripts that require authorization inside simple triggers
Simple triggers have limited permissions and cannot perform some actions
Use installable triggers from the Triggers menu for scripts needing more permissions
Summary
Triggers run scripts automatically when you open or edit a Google Sheet
Use onOpen() to run code on sheet open and onEdit(e) to run code after edits
Simple triggers have limits; use installable triggers for advanced needs