Diagnostic reference
Cannot read properties of undefined (reading 'range')
Why onEdit(e) and onFormSubmit(e) fail with Cannot read properties of undefined (reading 'range') when you click Run, and how to test triggers.
Updated November 26, 2026
Apps Script error
TypeError: Cannot read properties of undefined (reading 'range')
What it means
Google Sheets and Google Forms pass an event object e to onEdit(e) and onFormSubmit(e) only when the trigger fires. When you click Run in the editor, no edit or submission happened, so Apps Script calls the function with e set to undefined. Reading e.range, e.value, or e.response then throws a TypeError.
Related error messages
- TypeError: Cannot read properties of undefined (reading 'value')
- TypeError: Cannot read properties of undefined (reading 'response')
- TypeError: Cannot read properties of undefined (reading 'namedValues')
- TypeError: Cannot read properties of undefined (reading 'source')
Before and after
The code before and after the fix.
function onEdit(e) {
const cell = e.range;
Logger.log(cell.getA1Notation());
}/** @param {Events.SheetsOnEdit} e */
function onEdit(e) {
if (!e || !e.range) {
Logger.log('Edit a cell in the spreadsheet to test onEdit, or call testOnEdit().');
return;
}
Logger.log(e.range.getA1Notation());
}
function testOnEdit() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = spreadsheet.getActiveSheet();
onEdit({
authMode: ScriptApp.AuthMode.LIMITED,
oldValue: '',
range: sheet.getRange('A2'),
source: spreadsheet,
triggerUid: '',
user: Session.getActiveUser(),
value: String(sheet.getRange('A2').getValue()),
});
}How to fix it
- To test onEdit(e), edit a cell in the bound spreadsheet instead of clicking Run. To test onFormSubmit(e), submit the form.
- For spreadsheet edit or sheet-bound form-submit handlers, add if (!e || !e.range) return; at the top. For form-bound submit handlers, guard if (!e || !e.response) return; instead, because form-bound events provide e.response rather than e.range.
- To debug from the editor, write a separate helper function that builds a sample event object and passes it to onEdit.
- Open Executions in the left sidebar to read Logger.log output from real trigger runs.
Things to check
- If the error happens during a real trigger run, check the trigger event type. A time-driven trigger does not provide e.range.
- Simple triggers such as onEdit cannot call services that require authorization. Install a trigger in the Triggers panel when you need GmailApp or DriveApp.
In the Apps Script editor
Catch errors like this before clicking Run.
myFunction (published as Error Checker for Google Apps Script on the Chrome Web Store) checks your code while you edit at script.google.com. Diagnostics, autocomplete, and rule-based quick fixes are free without an account, run in your browser, and request no Google Drive, Gmail, or Apps Script project OAuth scopes.