Skip to content

Diagnostic reference

Cannot read properties of null (reading 'getRange')

Fix Apps Script's null getRange error: check the sheet tab name, confirm the spreadsheet, and guard missing lookups before reading cells.

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

The code before and after the fix.

Problem 1 changed line
function readData() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
  return sheet.getRange('A1:B10').getValues();
}
Fixed 4 changed lines
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 the failing line in Executions. If sheet.getRange fails, inspect the assignment to sheet first.
  • Compare getSheetByName with the tab label at the bottom of the spreadsheet. Match capitalization and spaces, including trailing spaces.
  • Confirm that the script opened the intended spreadsheet. Log spreadsheet.getUrl() after checking that spreadsheet is not null.
  • When there is no active spreadsheet, use SpreadsheetApp.openById with the spreadsheet ID from its URL before looking up the tab.
  • Check the result of getSheetByName before calling getRange.
  • Throw a useful error when the tab is missing, or choose another explicit recovery path.

Things to check

  • Decide what the script should do when the sheet is missing. The guard throws an error by default.
  • An undefined value has a different cause: check for an unassigned variable or an array index beyond the available sheets.
  • myFunction cannot know the intended sheet name.