Skip to content

Diagnostic reference

Cannot read properties of null (reading 'getRange')

Why 'Cannot read properties of null' points at the sheet lookup one line earlier, not the getRange call, and the guard that fixes it.

Updated September 12, 2026

Apps Script error

TypeError: Cannot read properties of null (reading 'getRange')

What it means

A lookup returned null and the next line used it anyway. getSheetByName returns null when no tab matches, and getActiveSpreadsheet can return null when there is no active spreadsheet. Check both lookups; use openById when the execution has no active spreadsheet.

Related error messages

  • TypeError: Cannot read properties of null (reading 'getDataRange')
  • TypeError: Cannot call method "getRange" of null

Before and after

See the change before you make it.

Problem
function readData() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
  return sheet.getRange('A1:B10').getValues();
}
Safer version
function readData() {
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  if (!spreadsheet) throw new Error('No active spreadsheet; open one by ID.');
  const sheet = spreadsheet.getSheetByName('Data');
  if (!sheet) throw new Error('Sheet "Data" not found; check the tab name.');
  return sheet.getRange('A1:B10').getValues();
}

How to fix it

  • Check that there is an active spreadsheet before looking up a tab.
  • Check the result of getSheetByName before calling getRange.
  • Throw a useful error when the tab is missing, or choose another explicit recovery path.

What stays with you

  • Decide what the script should do when the sheet is missing; the guard throws by default.
  • myFunction cannot know the intended sheet name.