Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'getSheetByName' of null

function sortResponses() {
  var Sheets = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Fall 01")
  sheet.sort(3, false);
}

I have a Sheet called Fall 01 in my google Sheets, where I explicítly had to give the script access to, but it won't open, what am I missing?

like image 366
Mr Watski Avatar asked Oct 17 '25 06:10

Mr Watski


1 Answers

Explanation/Issue:

The issue is that you have a standalone script which therefore is not bound to a google spreadsheet and as a result SpreadsheetApp.getActiveSpreadsheet() returns null.

Solutions:

There are two ways you can follow:

Solution 1:

Use SpreadsheetApp.openById(id) or SpreadsheetApp.openByUrl(url):

var Sheets = SpreadsheetApp.openById("Put your Spreadsheet ID").getSheetByName("Fall 01");

or

var Sheets = SpreadsheetApp.openByUrl("Put your Spreadsheet URL").getSheetByName("Fall 01");

Solution 2:

Go to the spreadsheet file that you want to work with and click on Tools => Script editor on the top menu of the spreadsheet file and put your current code there.

Please Note

If this is your only code, sheet is not defined, therefore you will get another error down the road. You most likely want to replace sheet with Sheets or the other way around. Be careful with this.

like image 93
soMarios Avatar answered Oct 19 '25 03:10

soMarios



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!