ui.alert("You clicked: "+cell+"!! \n Ya big legend!!!");
Line 1, sets the function, button(). Our first variable, ui, gets the User Interface class (getUi()). The second variable, cell, then calls the SpreadsheetApp service again all for the value of the currently selected cell.
Now that our variables are set, we call the ui class and send an alert on our screen. Inside the alert (Line 5) we add some text plus our cell value.
When the function is run, you will need to accept permissions for the first time. Then, you will see that whatever cell you clicked, the function will read that cell value and display it as an alert on your screen.
With the function complete, we’ll move over to create the button.
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.