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.
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.
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 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
I'm attempting to use a script in a google sheet I've used in the past, and suddenly while trying to make a new sheet with this code, I'm getting a new error that is seemingly impossible to bypass.
"Sign in with Google temporarily disabled for this app
This app has not been verified yet by Google in order to use Google Sign In."
Normally, or at least every time I have used this script in the past, there has been an "advanced" and "continue anyway" option.
"This app isn't verified yet
"Advanced > Continue to Untitled Project (Unsafe)"
This is the code.gs script [gitlab link], how can I bypass this saftey check, and continue using this script as needed?
And for reference, these are the instructions for the whole project on git lab,
Unfortunately, there's no direct solution for the issue you are encountering.
According to the issue from Issue Tracker, the error you are encountering may be due to this cause:
an external user runs the script with sensitive scopes
A possible solution:
run the script from Incognito Mode and remove the project's permissions from myaccount.google.com/permissions
Ultimately, if this still doesn't solve your issue you can star the issue on Issue Tracker here.
I was trying to publish a new version of an Add-on that was already created, but when I tried to publish it, I can see the menus, sub-menus and I can execute the Add-On Picture 1. The problem is that the other users -who before were able to execute the Add-On- now they don't see the sub-menus and obviously they can't execute the Add-On Picture 2.
I Googled a little bit and I was investigating about this, so I found that there is a method called AuthMode.NONE and AuthMode.LIMITED, I know the differences between them but I don't know how Google Scripts classify the Add-Ons and how to force change the AuthMode to LIMITED, or maybe you can give me another solution that can help.
BTW: I already checked if the Add-On is activated for my document and yes it is, I mean that i have the option: Add-Ons --> Manage Add-Ons --> [my addon] --> Manage --> Use in this document checked.
PD: I tried uninstalling and installing the Add-On, publishing and re-publishing new versions, I tried a lot of things but no solutions.
Thanks in advance!
This read on Authorization modes may help:
If an add-on is installed for a user but not enabled in the current
document, onOpen(e) runs in AuthMode.NONE; if the add-on is enabled in
the current document, onOpen(e) runs in AuthMode.LIMITED. If the
add-on is both installed and enabled, the enabled state takes
precedence, since LIMITED allows access to more Apps Script services.
Note that only published add-ons can be in AuthMode.NONE; scripts that
are bound to a document, including add-ons in development, always run
onOpen(e) in AuthMode.LIMITED. You can, however, test a script as an
add-on to verify that an add-on under development will behave as
intended in either AuthMode.
Check this SO thread for additional reference.