I trying to publish Google Sheets editor add-on to the market. It is being rejected for listing as the addon menu items are not being added to Sheets addon menu. This is the image I got from the review team:
Menu items are being added within onOpen()
trigger function, and in development environment its working fine.
The first time publishing was rejected as I was not aware for the onOpen
ScriptApp.AuthMode restrictions and the necessity to implement onInstall()
just to call onOpen()
, as described
The following is the corresponding error log entry filtered by "Exception: You do not have permission" in JSON format
{ "insertId": "-vw46i0f1p6gav", "jsonPayload": { "message": "Exception: You do not have permission to perform that action.\n at addMenu(src/macros:27:36)\n at onOpen(src/macros:16:3)", "context": { "reportLocation": { "lineNumber": 27, "functionName": "addMenu", "filePath": "src/macros" } }, "serviceContext": { "service": "AKfycbx5HHHMsaYBlD39Pf3UmyM0JhkFUWIpxU0Iw3Jp-XYCBw" } }, "resource": { "type": "app_script_function", "labels": { "invocation_type": "simple trigger", "function_name": "onOpen", "project_id": "gothic-space-325413" } }, "timestamp": "2021-11-24T20:49:50.684Z", "severity": "ERROR", "labels": { "script.googleapis.com/deployment_id": "AKfycbx5HHHMsaYBlD39Pf3UmyM0JhkFUWIpxU0Iw3Jp-XYCBw", "script.googleapis.com/process_id": "EAEA1GOytih2wJPcuJNmCqsYzsqHKmik2d0KexhRdxq9Bi-T9frqnuGYZ_0XT1d0rvTIF6w5AWR5km2z2arNDWNHrShY88Z1ZvRKKib5hX46-e1oqdrpz0T7Fxc7xNnOrWXglwuTsdOQp0CbTO-4KgutgMRotiOjwArel5QOYPKYkhD8Jb-oJ6mYPa__KPLAug-Li3IzIbR_g5y9YffLMHhY-Chd_SlfcaxuB607i30zSFjBdmtYnQreN5KXKUzfXJ60SHHNtLPZZVeaozQDB6roqP82bgzZliyU7X12Y", "script.googleapis.com/project_key": "MVmLkhkHw02MpGp6YjTYdgIJQT15o7EEE", "script.googleapis.com/user_key": "APrZfepJTnqDE2xPOOZyy7vfu7vWpovS3THrJ8F1CIHw5sc/710lPeQ940fg7V6II4bRKBAqBvr2" }, "logName": "projects/gothic-space-325413/logs/script.googleapis.com/console_logs", "receiveTimestamp": "2021-11-24T20:49:51.726293367Z" }
questions:
It is right to assume
onInstall()
is not running, according to cloud error and logging? If yes - what can be the reason?I thought maybe, this is something with what the review team did, or maybe did not do. For example: First time they installed the addon, so it is being added to the main menu (i.e. Formula Analyzer), but the sub items are missing because of the missing
onInstall()
. Second review they just open the document again, so Formula Analyzer is still there, but theonOpen
fails (again) because they did not re-install the addon. Just an assumption - I do not know of course if it is a valid scenario, or what they are actually doing..More importantly maybe, this is a scenario I was considering before, how to test-imitate market installation in dev environment, without add on is published yet. How can one test the onInstall trigger? I couldn't still find the way. This is why after the first rejection and the fix, I published for review again, without actually testing it. I tried asking the review team, if it is possible and how, but they ignore. And here we are again.
How to solve this?
CodePudding user response:
Issue:
If the add-on is not enabled for current document and it's just installed, onOpen
trigger does not have access to that document.
Explanation:
Before using an add-on for the first time in a document, it is not enabled for that document. In those cases, a simple onOpen
does not have access to that document, and methods related to that document, like SpreadsheetApp.getActive() or Spreadsheet.addMenu, fail accordingly.
Since an add-on can be installed without calling onOpen
, when you later call onOpen
, it may not run with AuthMode.FULL
(the authorization mode for onInstall
).
Therefore, an add-on should be designed so that the onOpen
trigger can run with AuthMode.NONE
(that is, in cases when the add-on is installed for the user, but not enabled for the document), and the review team is right in attempting to execute this without onInstall
. And because of this, methods that access the current document should not be used in that function.
That's the reason why methods like Spreadsheet.addMenu are not used in the documentation code snippets for editor add-ons, and Ui.createAddonMenu() is used instead, since that method is not accessing the document, and so can be used in onOpen
.
Code sample:
function onOpen() {
var menu = SpreadsheetApp.getUi().createAddonMenu();
menu.addItem('Trace precedents', 'tracePrecedents');
menu.addItem('Trace dependents', 'traceDependents');
menu.addToUi();
}
Note:
You'll notice that this method doesn't allow you to name the menu. That's because, since this is designed for add-ons, the menu name will match the add-on name.