0
votes

While following the tutorial here: https://developers.google.com/apps-script/articles/building-sites-app-part1

I have the sample code copied in and modified etc. When I try to run it I get the error:

TypeError: Cannot call method "getLastRow" of null. (line 327)

Error.

Here are lines 326, 327 from that page:

326) var sheet = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName("TimeBooking"); 327) var lastRow = sheet.getLastRow();

If I edit the string for .getSheetByName to make it wrong on purpose, then I get an error on line 326 telling me it can't find the sheet. Good, it should fail that test. If I make sure the line 326 is correct, it runs silently with no errors, but line 327 puts out the null error.

If the sheet object is invalid, why doesn't line 326 warn me about it?

Note I get the same null error whether I run this code directly in the apps editor, or whether I publish the URL and run it from my sites page as a google Apps widget..

Any ideas? I'm trying to learn the basics, and got stumped on my first "copy and paste" of an example (lol!)

2

2 Answers

1
votes

Alright, I figured it out - my edits broke the example. I got lost in the semantics, and indeed renaming the spreadsheet was the problem.

I confused the name of the whole spreadsheet with the name of the individual sheet in said spreadsheet.

The name of the whole spreadsheet is irrelevant, since you use the Spreadsheet ID to open it (which is a key in the URL of the spreadsheet).

The name of the first "sheet" in the spreadsheet IS relevant, and must match the ascii names inside the source code.

I unfortunately named my Spreadsheet to TimeBookingExample, and didn't notice that the first sheet in the spreadsheet was named TimeBooking.

Thus the getSheetByName() function needs the name of the first sheet in the spreadsheet, not the name of the whole spreadsheet document.

326) var sheet = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName("TimeBooking");

Why doesn't line 326 give an error when it failed? That would have made me find my bug instantly..

0
votes

when an error message says something about getColumnWidth you shouls look for a line of code that uses getColumnWidth() (use command F or control F to help you) and see to which object this statement applies.

Also : have you checked the sheet name ? you mention a sheet name in your question ("TimeBookingExample") but the code you show talks about another sheet name ("TimeBooking")... I'm a bit confused.

EDIT : first part of this answer doesn't make sense anymore since the question has been edited