getUi is not a function?

309 views
Skip to first unread message

SSD_Matt

unread,
Oct 22, 2023, 12:52:05 PM10/22/23
to Google Apps Script Community
Hey everyone, I'm hacking my way through a small project but can't even seem to get a simple button and pop-up window script to function.

Here is my script:

function myFunction() {
  var ui = SpreadsheetApp.getActiveSpreadsheet().getUi();
  ui.alert("Clicked!");
}

I've assigned this to a button in Google Sheets and when I click the button I get this message:

TypeError: SpreadsheetApp.getActiveSpreadsheet(...).getUi is not a function

I've been asking Bard for help but it's largely going to nowhere. Also, I was provided this script by Bard with instructions for use in Google Sheets which is confusing to say the least.

Any chance someone here could offer some wisdom?  Thank you!



Andrew Apell

unread,
Oct 22, 2023, 3:25:10 PM10/22/23
to Google Apps Script Community
Write it like this:

function myFunction() {
  var ui = SpreadsheetApp.getUi();
  ui.alert("Clicked!");

SSD_Matt

unread,
Oct 22, 2023, 6:46:38 PM10/22/23
to Google Apps Script Community
It works now.  Thank you!  On to the next feature...

SSD_Matt

unread,
Oct 22, 2023, 7:03:45 PM10/22/23
to Google Apps Script Community
Okay, now I'm trying to randomly pick an item from a drop down menu...here is what Bard suggests:

function randomDropdownSelection() {
  // Get the range of cells that contains the dropdown menu.
  var dropdownRange = SpreadsheetApp.getActiveSpreadsheet().getRange("B4");

  // Get the list of items in the dropdown menu.
  var dropdownItems = dropdownRange.getDataValidation().getCriteriaList();

  // Generate a random number between 1 and the number of items in the dropdown menu.
  var randomIndex = Math.floor(Math.random() * dropdownItems.length) + 1;

  // Select the item at the random index in the dropdown menu.
  dropdownRange.setValue(dropdownItems[randomIndex]);
}



Here is the error message I keep getting:

TypeError: dropdownRange.getDataValidation(...).getCriteriaList is not a function


Any suggestions?  I feel like Bard isn't referencing current (Java?) syntax...

Andrew Apell

unread,
Oct 23, 2023, 1:03:58 AM10/23/23
to Google Apps Script Community
Use ChatGPT-4 or Bing AI not Bard. Bard, at this point, is a skilled liar. It will make up stuff, like methods, that do not exist.
If you use Bing, make sure it is configured like this:

img.png

If not, it will probably tell a few lies as well.
Reply all
Reply to author
Forward
0 new messages