Google Apps Script – AuthMode
I mean, when I run it normally, it works just fine!
This is an interesting problem I came across recently when reviewing one of my Google add-ons.
The issue was that when I ran my Google Apps Script add-on code normally – in my case, connected to a Google Sheet – the add-on would appear just fine in my add-on menu. However, when I tested the script as an add-on ( run >>> Test as add-on…), as installed for current user but not enabled, my app would not appear in the add-on menu.
If I were to publish this app in this condition (and I may have…eep!), the user would be able to see my add-on in the add-on menu but not be able to access the link to run the app’s function without going into Add-ons >>> Manage add-ons… then click on Manage >>> Use this add-on. The user would have to do this each time they want to apply my add-on to a new Google doc for the first time. Not a good user experience.
The reason is that the first time the app is run in a new document, the Authorization Mode is set to none:
When the user opens the document for the first time, the
onOpen(e) trigger function is loaded along with any global variables that are in the script.
This is what Google Apps Script has to say about this:
So the problem was that one of my Global Variables did not abide by the scope of AuthMode.NONE.
How about we look at some example code to see how to identify the problem and work towards a solution.