Apps Script: TypeError: Cannot read properties of undefined (reading 'range')
Last verified
TypeError: Cannot read properties of undefined (reading 'range')What it means
This is a plain JavaScript error from the V8 runtime: the code tried to read a property of a variable that is undefined. In Apps Script it appears most often in trigger functions such as onEdit(e), onFormSubmit(e) or doGet(e), and the property in parentheses (range, source, values, parameter) is a property of the event object e.
The classic cause is clicking Run on the trigger function in the editor. When a trigger fires, Apps Script passes an event object; when you run the function by hand, nothing is passed, so e is undefined and e.range fails on the first line.
The function is usually fine. It just was not started by the event it was written for.
What it looks like
function onEdit(e) {
if (!e || !e.range) return; // started from the editor
const sheet = e.range.getSheet();
...
}
function testOnEdit() {
const range = SpreadsheetApp.getActiveSheet().getRange('B2');
onEdit({ range, value: range.getValue(), source: SpreadsheetApp.getActive() });
}Why it happens
- Running onEdit, onFormSubmit or doGet manually from the editor, so no event object is passed.
- An installable trigger set to the wrong event type, so the event object lacks the expected property (for example e.range on a time-driven trigger).
- Reading e.values in a form-bound trigger, which only exists in the spreadsheet-bound onFormSubmit event.
- A different variable that is genuinely undefined, such as getSheetByName returning null for a misspelled tab, followed by a property read.
How to fix it
- Do not run event-driven functions from the editor; test onEdit by editing a cell in the sheet.
- Check Executions for the run type: if it says Editor, the error is expected and can be ignored.
- Add a guard at the top: if (!e || !e.range) return;
- To test in the editor, write a small helper that builds a fake event object and calls the trigger function with it.
- If the error names a property you did not expect, log the variable just before the failing line to see which one is undefined.
How to stop it recurring
Keep event handlers thin: validate the event object, then call a normal function that does the work and can be tested independently. Name installable-trigger handlers clearly so it is obvious which event they expect.
Official documentation: developers.google.com/apps-script/guides/triggers. See all Apps Script error messages or the Apps Script limits and quotas.
Related errors
Definitions
- Google Apps ScriptGoogle Apps Script is Google's cloud-hosted JavaScript platform for automating Google Workspace: Sheets, Gmail
- Error HandlingError handling is the practice of anticipating, detecting, and responding to errors in software execution, inc
- Rate LimitingRate limiting is a technique that controls the number of requests a client can make to a server within a given
- OAuth 2.0OAuth 2.0 is the IETF authorization standard (RFC 6749) that lets an app access your account in another servic
Quick answers
What does "TypeError: Cannot read properties of undefined (reading 'range')" mean in Apps Script?
This is a plain JavaScript error from the V8 runtime: the code tried to read a property of a variable that is undefined . In Apps Script it appears most often in trigger functions such as onEdit(e) , onFormSubmit(e) or doGet(e) , and the property in parentheses ( range , source , values , parameter ) is a property of the event object e . The classic cause is clicking Run on the trigger function in the editor.
How do I fix "TypeError: Cannot read properties of undefined (reading 'range')"?
1. Do not run event-driven functions from the editor; test onEdit by editing a cell in the sheet. 2. Check Executions for the run type: if it says Editor, the error is expected and can be ignored. 3. Add a guard at the top: if (!e || !e.range) return; 4. To test in the editor, write a small helper that builds a fake event object and calls the trigger function with it. 5. If the error names a property you did not expect, log the variable just before the failing line to see which one is undefined.
Run the job somewhere with its own limits
If a script keeps hitting its host's quotas, an Autonoly workflow can run the same steps on a schedule in an isolated cloud container, and the AI agent has a terminal with Python for steps that need code. Autonoly has limits of its own, priced in credits.
Also see Agent terminal & SSH.