Anant
Anant

Reputation: 805

Menu item doesn't appear after publishing google addon

My google sheets script is working well, but once it was published, the menu items are no longer there. So, instead of displaying the 'start' button in the addon (which works fine when it is run as a script) it only displays help for my addon. Does anyone have any ideas why this may be the case?

//Runs when the addon is installed 
function onInstall(e) {
  onOpen(e);
}

//Creates menu entry in google ui when opened 

function onOpen(e) {
  SpreadsheetApp.getUi().createAddonMenu()
      .addItem('Start', 'showSidebar')
      .addToUi();
}

Upvotes: 3

Views: 1360

Answers (2)

Michele Pisani
Michele Pisani

Reputation: 14179

This can happen when you use the Spreadsheet instance globally (i.e. outside of functions):

var ss = SpreadsheetApp.getActiveSpreadsheet(); // or other variable name

Locally it works but when the script is published as an add-on it generates problems.

The solution is to repeat that declaration in all functions where the instance is used or passed it fro other functions (and remove it from the global scope).

If you check the error in Error reporting in Google Cloud you may find this error detail:

Exception: You do not have permission to perform that action. at [unknown function]

Upvotes: 0

Wicket
Wicket

Reputation: 38219

Instead of

SpreadsheetApp.getUi().createAddonMenu()
    .addItem('Start', 'showSidebar')
    .addToUi();

try

var ui = SpreadsheetApp.getUi();
var menu = ui.createAddonMenu();
menu
    .addItem('Start', 'showSidebar')
    .addToUi();

The above because changes made about how authorizations scope are being handled on add-ons could make that chained statements like the one used on the question code doesn't work as expected and because the examples on https://developers.google.com/apps-script/guides/menus use more than one statement to create menus.

Upvotes: 1

Related Questions