Skip to content

Diagnostic reference

Cannot read properties of null (reading 'getDataRange')

Fix Google Apps Script TypeError: Cannot read properties of null (reading 'getDataRange') by guarding getSheetByName(), tab names, and openById().

Updated December 6, 2026

Apps Script error

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

What it means

Apps Script throws this error when the variable before .getDataRange() is null. In almost every case, getSheetByName() returned null because the tab name was renamed, had different capitalization, or carried a trailing space, or getActiveSheet() returned null in an unbound standalone script or time-driven trigger.

Related error messages

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

Before and after

The code before and after the fix.

Problem 3 changed lines
function exportAllRows() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Orders');
  // Throws when no tab matches 'Orders' (case-sensitive, spaces included).
  const values = sheet.getDataRange().getValues();
  return values.slice(1);
}
Fixed 14 changed lines
function exportAllRows(spreadsheetId) {
  const spreadsheet = spreadsheetId
    ? SpreadsheetApp.openById(spreadsheetId)
    : SpreadsheetApp.getActiveSpreadsheet();
  if (!spreadsheet) {
    throw new Error('No active spreadsheet; pass a spreadsheet ID.');
  }

  const sheet = spreadsheet.getSheetByName('Orders');
  if (!sheet) {
    const names = spreadsheet.getSheets().map((s) => s.getName()).join(', ');
    throw new Error('Sheet "Orders" not found. Available tabs: ' + names);
  }

  const values = sheet.getDataRange().getValues();
  return values.slice(1);
}

How to fix it

  • Guard getSheetByName() with if (!sheet) before calling sheet.getDataRange(), and log spreadsheet.getSheets().map(s => s.getName()) when it fails.
  • Check the tab label at the bottom of the Google Sheet for exact capitalization and accidental leading or trailing spaces.
  • If the script runs from a standalone project, web app, or time-driven trigger, open the file with SpreadsheetApp.openById() instead of getActiveSpreadsheet().
  • If the error names getSheetByName instead of getDataRange, the spreadsheet lookup itself returned null.

Things to check

  • getSheetByName() returns null rather than throwing when no tab matches, so the crash appears on the following getDataRange() line.
  • Calling getDataRange() on a brand-new empty sheet returns a 1x1 range (A1), not null; null always comes from the sheet lookup before it.

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. Writing Apps Script with an AI coding agent? Connect the Apps Script MCP server to run the same static checks before pasting code.