Stand Alone Script getSheetByName is not a function

3,140 views
Skip to first unread message

Greg Borchard

unread,
Jun 18, 2020, 3:00:07 AM6/18/20
to Google Apps Script Community

I have a standalone script that I eventually want to publish as an add-on.  I have a button in my sidebar that calls my test function.  The test function is in my server-side file.  When I test my script, I click Run > Test as Add-on in the IDE and I refer to Stackdriver logging for my errors.

I am experiencing difficulty with the SpreadsheetApp service in standalone scripts (I've used this numerous times in standalone scripts with no issues).

If I run this script:
function test() {
  const ss = SpreadsheetApp.getActiveSpreadsheet;
  const sht = ss.getSheetByName('Sheet1');
  const cell = sht.getRange('B6');

  cell.setValue = 1;
}

I get the error:
TypeError: ss.getSheetByName is not a function at test(Code:20:18)

and if I modify the script to be :
function test() {
  const ss = SpreadsheetApp.getActiveSpreadsheet.getSheetByName('Sheet1');
  const cell = ss.getRange('B6');

  cell.setValue = 1;
}


then I get the error:
TypeError: SpreadsheetApp.getActiveSpreadsheet.getSheetByName is not a function at test(Code:19:50)

What am I doing incorrectly?  Why am I getting the error "getSheetByName is not a function" when I'm not trying to run it as a function?

Martin Molloy

unread,
Jun 18, 2020, 3:04:45 AM6/18/20
to google-apps-sc...@googlegroups.com
you need to write it as

ss = SpreadsheetApp.getActiveSpreadsheet();

note the brackets at the end

Martin



--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/164261d6-9052-462a-86b9-e6c5dd364259o%40googlegroups.com.

CBM Services

unread,
Jun 18, 2020, 12:38:00 PM6/18/20
to google-apps-sc...@googlegroups.com
Also not sure you should be using Const as the declaration of an object that you are changing.

Would suggest you use var in this case.

From: Martin Molloy
Sent: ‎2020-‎06-‎18 12:04 AM
To: google-apps-sc...@googlegroups.com
Subject: Re: [Apps-Script] Stand Alone Script getSheetByName is not a function

Greg Borchard

unread,
Jun 18, 2020, 6:24:26 PM6/18/20
to google-apps-sc...@googlegroups.com
Thank you for your feedback.

I have added the brackets at the end as Martin suggested, and that has stopped the error.  I have also changed the const to let.  I still had a problem where the function executed but I had no errors showing.  Then realised I should have had:
cell.setValue(1);  and NOT cell.setValue = 1;

problem solved!

Thanks again for your help

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/XQeoth2JF28/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/5eeb9864.1c69fb81.78367.b94c%40mx.google.com.

adam....@igbis.edu.my

unread,
Jun 22, 2020, 7:01:04 AM6/22/20
to Google Apps Script Community
Regarding "const" / "let" / "var":

Using const is okay as long as you don't reassign the variable. It is not the same as a "constant" in other programming languages.

const obj = {value: 'value'};
obj.value = 'different value';   // okay
obj = {value: 'different value'};  // not okay, because it's reassigning the variable
Reply all
Reply to author
Forward
0 new messages