AndrewT
AndrewT

Reputation: 498

GAS in Sheets submit function does not work as another user

I have made a very simple version that still shows the issue and can be viewed and tested by anyone.

My problem is the onclick events do not ever get called unless you are the owner even when the sheet is shared with the world.

Please follow the link below, to try out the issue

My Simple Example to Try

Here is the simple code:

//Global reused variables
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pageTxt;
var html;

function onLoad(){
  pageTxt = ''; 
  pageTxt += '<h2><b>Click on this button, it should pop up and say Hello World but it does not work when it is not run as the owner, why?</b></h2><br><br>';
  pageTxt += '<TR><TD><input onclick="formSubmit()" type="button" value="Submit" style="height:30px; width:100px" /></TD><TD> </TD>';
  pageTxt += '<script type="text/javascript"> document.getElementById("UniqueCode").focus(); function formSubmit() { google.script.run.EventHandler(document.forms[0]); } </script>';
  html = HtmlService.createHtmlOutput(pageTxt);
  html.setTitle("Simple Example of my issue");
  ss.show(html);
}


//RecieveUniqueCode
function EventHandler(form){
  Browser.msgBox("Hello World");
  onLoad();
}

I am hoping someone to work out why when clicking the submit button as another user it does nothing.

If I add another button to close the form this works:

<input type="button" value="Close" onclick="google.script.host.close()" />

It just can't run a custom function in the app script.

Update:

I tried manually running the function as the other user and it poped up and asked for authorization

enter image description here

I think I am close to understanding the issue, although as the owner I have given the user permission to my sheet, because the script interacts with sheets it seems like the user also has to give my script permission to play with the sheet, a bit strange really. So I guess I need to find a way to force it to ask the permission instead of doing nothing when it needs it.

I can also now confirm that accepting the authorization means the simple script now works. It does not give the option when autorun i.e. onload but does when you run a function manually from script editor. how can I do this at run-time? Any ideas??

Thanks in advance for your help.

Upvotes: 0

Views: 70

Answers (2)

AndrewT
AndrewT

Reputation: 498

Google referance

Authorization is required to perform that action.

This error indicates that the script is lacking the authorization needed to run. When a script is run in the Script Editor or from a custom menu item an authorization dialog is presented to the user. However, when a script is run as a service, embedded with a Google Sites page, or run from a trigger the dialog cannot be presented and this error is shown. To authorize the script, open the Script Editor and run any function. To avoid this error, remember to run the script once in the Script Editor after adding new services or capabilities to your script.

So unless you are happy that every user you send this too has to run it manually the first time from the script editor then this flaw will stop you from ever deploying an automated sheet to anyone. Very disappointed in google apps :(

Upvotes: 0

Gerneio
Gerneio

Reputation: 1340

I think you are heading in the right direction. If I open of my console in chrome and see what happens when the button is clicked an error pops up. See the screenshot.

Dev console error

Not sure yet why or how to resolve it. I'll keep investigating though.

Update: Opened up the script editor, and attempted to run one of the functions manually, the script then prompted for authorization. Once authorized, I went back to the sheet and the formSubmit() function worked as expected (no errors in console).

Upvotes: 1

Related Questions