Error fix
Fix "Cannot read properties of undefined (reading 'range')" in onEdit
Cannot read properties of undefined (reading 'range')
The event object e only exists when a trigger fires. Press Run in the editor and e is undefined. Guard for it, and test with a fake event.
Set it up
- Add if (!e || !e.range) return at the top of your handler.
- Use testOnEdit to run your logic by hand.
- Test the real thing by editing a cell, then open Executions.
The script
/**
* Fix: "Cannot read properties of undefined (reading 'range')".
* The event object only exists when a trigger fires. Running onEdit from the editor
* passes nothing, so guard for it and keep a manual test function.
*/
function onEdit(e) {
if (!e || !e.range) return 'no-event';
return 'edited:' + e.range.getA1Notation();
}
function testOnEdit() {
var sheet = SpreadsheetApp.getActiveSheet();
return onEdit({ range: sheet.getRange('A2'), value: 'test' });
}What you should see
| Execution | Result |
|---|---|
| Run onEdit | no-event |
| Edit A2 | edited:A2 |
Run from the editor now returns no-event instead of throwing. Sample values, not output from a real account.
Common errors and fixes
- It works when I edit but fails on Run
- That is expected. Run passes no event.
- e.range is undefined in a time-driven trigger
- Time-driven triggers pass a different event. Read the sheet directly.
Permissions. Running this script asks you to grant Google permissions for the services listed above, to your own script. TriggerShelf never receives your Google data. See the permissions table. Google will show "Google hasn't verified this app"; that is expected for a script you paste yourself.
TriggerShelf is independent and not affiliated with or endorsed by Google LLC. Google Sheets, Gmail, Google Drive and Apps Script are trademarks of Google LLC. Quota figures come from Google's Apps Script quotas page. Test on a copy first.