Skip to content

Diagnostic reference

Cannot read properties of null (reading 'getSheetByName')

Fix Apps Script TypeError: Cannot read properties of null (reading 'getSheetByName'): getActiveSpreadsheet() returns null outside a Sheet. Use openById.

Updated December 6, 2026

Apps Script error

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

What it means

SpreadsheetApp.getActiveSpreadsheet() returned null, so there was no spreadsheet to look up a tab in. It returns a spreadsheet only when the script is bound to one, meaning you opened the editor from Extensions, then Apps Script inside that Sheet. A standalone project from script.google.com, or a script opened from a Google Form or Doc, has no active spreadsheet. Open the spreadsheet by ID instead.

Related error messages

  • TypeError: Cannot read properties of null (reading 'getActiveSheet')
  • TypeError: Cannot read properties of null (reading 'getSheets')
  • TypeError: Cannot read properties of null (reading 'getId')
  • TypeError: Cannot call method "getSheetByName" of null

Before and after

The code before and after the fix.

Problem 2 changed lines
function readOrders() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Orders');
  Logger.log(sheet.getLastRow());
}
Fixed 6 changed lines
const ORDERS_SPREADSHEET_ID = 'your-spreadsheet-id'; // From the spreadsheet URL, between /d/ and /edit.

function readOrders() {
  const spreadsheet = SpreadsheetApp.openById(ORDERS_SPREADSHEET_ID);
  const sheet = spreadsheet.getSheetByName('Orders');
  if (!sheet) throw new Error('No tab named Orders in ' + spreadsheet.getName() + '.');
  Logger.log('Orders has ' + sheet.getLastRow() + ' rows.');
}

In a script opened from a Google Form

A script opened from the form editor is bound to the form, not to its response spreadsheet. Ask the form where it saves responses, then open that spreadsheet by ID.

Problem 1 changed line
function countResponses() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
  Logger.log(sheet.getLastRow() - 1);
}
Fixed 8 changed lines
function countResponses() {
  const form = FormApp.getActiveForm();
  if (!form) throw new Error('Run this from the script editor opened from the form.');
  if (form.getDestinationType() !== FormApp.DestinationType.SPREADSHEET) {
    throw new Error('Link this form to a spreadsheet on the Responses tab first.');
  }
  const spreadsheet = SpreadsheetApp.openById(form.getDestinationId());
  const sheet = spreadsheet.getSheetByName('Form Responses 1');
  if (!sheet) throw new Error('No tab named Form Responses 1 in ' + spreadsheet.getName() + '.');
  Logger.log(sheet.getLastRow() - 1);
}

How to fix it

  • Check where the script lives. In the Apps Script editor, open Overview: a bound project names its container file there. If it names a Form or a Doc, or nothing, getActiveSpreadsheet() returns null.
  • Copy the spreadsheet ID from its URL, the long text between /d/ and /edit, and pass it to SpreadsheetApp.openById.
  • In a script bound to a Google Form, open the linked response spreadsheet with FormApp.getActiveForm().getDestinationId().
  • Keep the getSheetByName check after opening the spreadsheet. A wrong tab name causes the related getRange error on the next line.

Things to check

  • openById asks for access to all your spreadsheets the first time it runs. The script can still only change what the code tells it to.
  • If the message names getRange or getDataRange instead, the spreadsheet opened and the tab lookup returned null. See Cannot read properties of null (reading 'getRange').
  • myFunction cannot know which spreadsheet the script is meant to open.

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.