Saurabh Garg
Saurabh Garg

Reputation: 201

Getting Cannot call method "getRange" of null error

var METADATA_SHEET_NAME = 'MetaData';

function readMetaData() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName(METADATA_SHEET_NAME);
  var range = sheet.getRange(1, 2);
  var fixVersion = range.getValue();
  Logger.log(fixVersion);
  return fixVersion;  
}

In my current google script, I already have Sheet with Name MetaData. I am storing a sheet name in variable METADATA_SHEET_NAME. if I pass sheet name directly in getSheetByName() method, it works perfectly but when I am passing variable name(in which I have stored sheet name) it is giving

TypeError: Cannot call method "getRange" of null.

Upvotes: 0

Views: 616

Answers (2)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17613

Check for typos as it's working for me. It's okay to assign the sheet name to a variable.

Here I have a sheet named 'Sheet1' which I assigned to METADATA_SHEET_NAME variable. Now I'm going to fetch value from row 1 col 2 as per your code.

[17-07-06 05:17:16:829 PDT] Response1

enter image description here

Upvotes: 1

Andy_Mac
Andy_Mac

Reputation: 1

Global variables in Google apps script aren't persistent. see top answer here for more info

Check out this blog post for loading global variables if you really need to

Upvotes: 0

Related Questions