Search code examples
google-apps-scriptgoogle-sheetstriggers

Exception: Cannot call SpreadsheetApp.getUi() from this context. (line 2, file "Code")


I'm getting this error when I try to run my code to had a custom menu. My code:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Menu')
      .addItem('Candles','callCandles')
      .addToUi();
}

Solution

  • You are not supposed to manually execute a trigger function. This particular one is activated when you open the spreadsheet.

    You can see the menu Candles created on top of the menu bar. You just need to refresh the spreadsheet page and you will see it. Please read carefully the official documentation:

    https://developers.google.com/apps-script/guides/triggers