I recently created and published a Google-Docs add-on, but have run into an issue when trying to download and use it with another account.
The Issue:
When I first install it in a document, the menu items show up and everything is working fine.
However, when I open up a new document, and try to use the menu, all the items have disappeared and I'm left with "Help." (see screenshot)
The only way to fix this issue is to click Add-ons --> Manage Add-ons --> click use in document, and then reload the page. (screenshots)
However, I would like to set the add-on to be used in every document by default, so that users won't have to go through this process and reload every time they create a new document. (I am also open to any other solution that allows the menu items to be shown)
Minimal Reproducible Example:
This is the code I use to create the menu upon installing or opening.
function onInstall(e) {
onOpen(e);
}
function onOpen(e) {
DocumentApp.getUi().createAddonMenu()
.addItem('Function 1', 'functionOne')
.addSeparator()
.addItem('Function 2','functionTwo')
.addItem('Function 3', 'functionThree')
.addToUi();
}
The logs show the following error:
You do not have permission to call getUserProperties.
Some relevant info: whenever the script runs, it checks whether the user has saved any settings (which would be in user properties), and if there are no settings, it sets them to default. Is it possible that I cannot access user properties from new documents?
Let me know if I should provide any other information, such as the name of the add-on
You should review Editor add-on authorization. Briefly, add-ons run on limited authorization mode when they are not enabled for the active document (form, spreadsheet, presentation). To check the authorization mode use authMode property of the open event object before calling any method that requires full authorization mode to run.
Related
Can I test DocumentProperties in Google Apps Scripts? (read this first)
Google Apps Script - AuthMode - no menus are visible after installation
add-on with LIMITED auth cannot open sidebar
How I can start a HtmlService directly on file start (onOpen)?
Trouble understanding authmodes
Related
I'm new to Google Apps Script, so forgive me if I use the wrong terminology. I'm trying to build a Google Apps Add-On but I'm having trouble piecing things together. Ideally I'd like to add a right-click menu to all documents when someone installs my Add-On. Similarly to how LucidChart adds a top level menu to all docs.
https://developers.google.com/apps-script/reference/base/ui#createAddonMenu() says:
If the script is running as an add-on, the sub-menu name matches the add-on's name in the web store; if the script is bound to the document directly, the sub-menu name matches the script's name.
Should this work in an Add On:
function onOpen() {
DocumentApp.getUi().createAddonMenu().addItem("Test", "test").addToUi()
}
function test() {
DocumentApp.getUi.alert("Yeah!")
}
and if so, what am I supposed to do after doing this to trigger onOpen on all the documents I open in my account? I mean, at the moment, just to test this add on (that is, to start developing it). I'm not sure what to do to run it.
You should create a unbound/standalone script when creating a addon. But it'll become bound to the document, when a user installs the addon on that document. See Authorization modes.
I'm piecing together an answer. There are currently two editors for Google Scripts, the current one and the legacy one. I still don't know if it's possible to test an add-on with the current one, but it seems possible in the legacy one.
To switch from the current one to the legacy one, there's a link on the top right:
Once in the old editor there's "Run" menu:
that has a "Test as add-on" function:
That opens a dialog that allows you to open another document with the add-on installed, enabled, or both:
Before finding this out, I enabled showing the script manifest, added the "addOn" entry, and deployed as a Test Add On in the new editor. I'm not sure if that had an effect.
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();
}
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
When running the onOpen trigger or any function calling Class Ui from the Google Apps Script editor, it will fail if the script was opened from a link, from https://script.google.com, etc. but not from the container even if the container was opened later. It might also happen when somehow the document editor UI and the Google Apps Script Editor lost the connection between the active document, form, slide or spreadsheet and the script.
Usually this is fixed by closing the script and open it again from its container. As of September 2022, this is done from the Extensions menu.
Very rarely it might be needed to restart the web browser, and even more rarely it will be required to restart the computer.
Based on Installed and Enabled states of Editor add-on, I have written code for each states on Google Sheets add-on.
The code is working fine when I test it using the script editor with INSTALLATION CONFIG set to Installed and Enabled. But when I test the published addon after installing, the state always remains disabled for the document.
To enable the add-on, I have to manually go to Add-ons >> Manage add-ons >> Click on the three dots of my installed add-on >> Click on "Use in this document". Please find below screenshot:
Now I need to know whether programmatically it's possible to enable the add-on for the document or not. I tried but couldn't find a way to perform it programmatically.
Any help or suggestions would be helpful.
Edit
I have found that Data connector for Salesforce is already doing this. After clicking "Click to enable the add-on", "Use in this document" gets ticked. So there must be some way to programmatically enable the addon. I have scoured each Google App Script article but I haven't found any piece of code to achieve this.
Thanks.
There's no way to programmatically enable an add-on for a spreadsheet.
As you can see in the reference you shared, enabling an add-on for a document can be done the following ways:
Getting an add-on from the store while using that document, or
Using a previously installed add-on in that document
You have to take into account that an add-on can be enabled for a document, not for a user or for a domain, so having the add-on enabled for all documents by default kind of breaks the idea behind enabling and the related Authorization modes.
Enabling an add-on via onOpen:
If your add-on contains an onOpen trigger that adds an add-on menu with an item with which you can call an add-on function, clicking this item will effectively enable the add-on for your document. See, for example, The complete lifecycle.
File a feature request:
In any case, you can try filing a feature request in this Issue Tracker's component if you think this functionality could be useful.
Reference:
Installed versus enabled
I would like to read the Active spreadsheet and add menu items based on that. Below is the code in OnOpen(e) trigger:
function onOpen(e) {
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
//add menus based on values of sheet
}
I am getting below exception:
[Exception: You do not have permission to perform that action.]
I have gone through the documentation and found that the AuthMode is None in OnOpen trigger which does not allow access to any services that require authorization.
Please suggest if there is any other way to accomplish my task.
Thanks.
It's not possible to access the spreadsheet unless the owner or editor of the spreadsheet uses the add-on in that spreadsheet. As written in the official document,
However, because an editor add-on automatically runs its onOpen(e) function to add menu items when a document opens, the behavior above adds some complexity to Apps Script's authorization rules. After all, users wouldn't be comfortable with an add-on accessing personal data every time they open a document.
The add-on needs to be enabled for the document for the addon to get access to the document. When it's enabled, onOpen() runs in AuthMode.LIMITED, where you get access to the bound document. This "enabled" state is caused by
Getting an add-on from the store while using that document, or
Using a previously installed add-on in that document
In all other cases, add on runs in AuthMode.NONE. In this mode, You are only able to add menu items without access to any data including the bound spreadsheet. Add Menu items to request access to such data. Then, after getting AuthMode.FULL, the rest of the workflow can be done.
References:
The complete lifecycle
I'm working on an AddOn based on an Apps Script library which stores several keystrings in the user cache. In testing the AddOn in AuthMode.NONE, the menu addition fails because the cache is trying to load when the document opens.
To work around this, I've tried incorporating several of the cache calls right into my code. This hasn't worked either.
Is there a way to programmatically add a library or prevent certain .gs files from loading in an AddOn until the user has enabled it?
UPDATE
Tanaike has a helpful Manifest library which allows you to change which libraries the app has access to. But, it still leads to authorization limitations. Is there a way to have the user interactively change the authorization mode?
I'm pretty sure this is a bug in Apps Script addon development flow.
I have released an addon, that follows the code from Apps Script documentation:
function onOpen(e) {
var menu = SpreadsheetApp.getUi().createAddonMenu();
if (e && e.authMode == ScriptApp.AuthMode.NONE) {
// Add a normal menu item (works in all authorization modes).
menu.addItem('Enable Addon', 'startWorkflow');
}
menu.addToUi();
}
The addon works and when installed from Gsuite Marketplace, the Enable Addon menu correctly appears in AuthMode.None.
However, when tested from inside script editor in AuthMode.None, it fails to add menus correctly with a console info:
Script Google Apps: Exception: You don't have permission for this action.
I think Google devs should handle it and fix the bug, and it's not meant to be solved by some manifest libraries or something else.