0

Apologies for the beginner question.

I have an app script that creates a user popup selection menu when my Google Sheet is edited. The selection menu was created in HTML. This works exactly as intended when I edit the spreadsheet from my own account. However, when a shared user does the same thing, withFailureHandler gets executed.

Here's my Code.gs:

function doGet(e){
  
  var html = HtmlService.createHtmlOutputFromFile('Index')
  
      .setWidth(250)
      .setHeight(200);
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .showModalDialog(html, 'Animal');  
}
      
function displayMessage(selection){
  
SpreadsheetApp.getUi().alert("Your selection was: "+selection);

}

And here's the Index.html:

 <form>
 Select animal:
 <br><br>
 <div class="custom-select">
 <select id="myList" onchange="selectionMade()">
   <option>Selection Option</option>
   <option>Cat</option>
   <option>Dog</option>  
   <option>Horse</option>
 
 </select>

<script>

function selectionMade(selection) {
 
  var mylist = document.getElementById("myList");
  var selection = mylist.options[mylist.selectedIndex].text;
  google.script.run
     .withSuccessHandler(function(data,element){window.alert("executed");})
     .withFailureHandler(function(msg,element){window.alert("failed"); })
     .displayMessage(selection);
}

</script>

I'm looking for the script to return the selection value to the spreadsheet for shared users the same one is does when I use it from my account.

Thanks in advance.

Update: I am getting the following error: "ScriptError: Authorization is required to perform that action." when trying to run it while signed in to a Google account other than the one that created the script.

4
  • is it the full code? I get the "failed" alert running it as you have it there. maybe because the submit button is not there, or you're calling the selectionMade() without an argument Commented Feb 18, 2022 at 17:16
  • Did you check the console to see what is the error it thrown? Commented Feb 18, 2022 at 18:07
  • @liquidkat I edited my post to include the following error: ScriptError: Authorization is required to perform that action. Commented Feb 18, 2022 at 19:27
  • @DavidSalomon Yes, that's the full code. It's just a super stripped down version to demonstrate the problem I'm having. I'm getting an authorization error when running as another user but it works completely fine from the account that created the script. Commented Feb 18, 2022 at 19:31

1 Answer 1

1

okay , you go to your Code.gs:, select function displayMessage

function displayMessage(selection){
 selection =1;
 SpreadsheetApp.getUi().alert("Your selection was: "+selection);

} 

run this function and go to Authorization. after Authorized, remove selection =1 from your code. That should works

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, that worked! So every user would then need to authenticate themselves, it looks like. I suppose I could create a button in the spreadsheet and assign it a script that asks for authorization. Thanks again.
if this script set run as you , then it requires first time auth, then it should be good. if this script set as run as user(someone using this script) then they have to auth for the first time use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.