Container-bound Script getting permission errors trying to run functions with google.script.run from sidebar
Asked Answered
N

3

5

I try to implement a sidebar on my spreadsheet to get user input for my scripts to use. I haven't been able to get it to successfully call any server side functions. I put together a simple script from the google documentation and several stackoverflow questions that I read through, but I keep getting an error. It is able to print to the console, but it errors out trying to call the logText() function with google.script.run.

Script File:

function onOpen() {
  SpreadsheetApp.getUi()
      .createMenu('Extra Functions')
      .addItem('Show sidebar', 'showSidebar')
      .addToUi();
}
function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('Test')
    .setTitle('Testing')
    .setWidth(300);
  SpreadsheetApp.getUi()
    .showSidebar(html);
}
function logInput(text) {
  Logger.log(text);
}

HTML File (Test.html):

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
    function onFailure(error) {
      var div = document.getElementById('output');
      div.innerHTML = "ERROR: " + error.name + ": " + error.message;
    }
    function logText(){
      var txt = document.getElementById("txt_input").value;
      console.log(txt);
      google.script.run.withFailureHandler(onFailure).logInput(txt);
    }
    </script>
  </head>
  <body>
    <label for="txt_input">Input Text:</label>
    <input type="text" id="txt_input"><br>
    <button onclick='logText()'>Send Name</button><br>
    <div id="output"></div>
  </body>
</html>

I've tried running it both on the new Apps Script V8 and Apps Script Legacy, and I get a slightly different error on each.

Apps Script Legacy

ERROR: ScriptError: You do not have access to perform that action. Please ask the owner of this item to grant access to you.

Apps Script V8

ERROR: ScriptError: We're sorry, a server error occurred while reading from storage. Error code PERMISSION_DENIED.

I've been doing research on Authorization but as far as I can tell, it has all the permissions it needs as a Container-Bound Script (https://developers.google.com/apps-script/guides/bound). It has the /auth/script.container.ui OAuth Scope which should allow it to "Display and run third-party web content in prompts and sidebars inside Google applications", as well as the /auth/spreadsheets Scope. I am also the owner of the spreadsheet and the script project.

Since it is not functioning as a Web App it does not need to be deployed, and does not need a doGet() function. https://developers.google.com/apps-script/guides/html#serve_html_as_a_google_docs_sheets_slides_or_forms_user_interface

Nelia answered 27/2, 2020 at 22:53 Comment(9)
It works for me.Operator
I had some similar problems and what I did was that every time I was told that I don't have permission to do something I'd take that scope and put it in my manifest file like they use to do and so now my manifest file has this added to it: Go to next commentOperator
"oauthScopes": ["googleapis.com/auth/userinfo.email", "googleapis.com/auth/script.external_request", "googleapis.com/auth/spreadsheets", "googleapis.com/auth/script.container.ui", "googleapis.com/auth/calendar", "googleapis.com/auth/gmail.send", "googleapis.com/auth/script.send_mail", "googleapis.com/auth/drive", ],Operator
I know it seems silly but hey if silly works I'm all for it. BTW I offered this solution to someone yesterday and it didn't work for them. But I'm running V8 and your code is working for me just as it is.Operator
I tried including the full list of scopes you gave in the Manifest. The first time I ran my script it made me confirm I was giving it all the permissions listed, but it is still giving me the same errors :/Nelia
Include your publish configuration.Beg
If you are talking about deployment, from what I understand in the documentation it doesn't need to be "published" since it is a Container-Bound Script and not acting as a Web AppNelia
Try the following just in case: log out of your google accounnt and log in with the account you are using for building this. Let me know if that helped/worked.Nonmetallic
Hi. Did anyone solve this? It's running fine with just a single-user signed in (Incognito) but dies when run in a multiple user session.Souffle
T
15

I've had the same issue, for me the problem was having a user being logged in with multiple google accounts. It might apply to your case as well. The user tried logging-in with just one account and the permission issue was gone.

It's the same problem that might occur when installing a custom addon. Hope it helps.

Trews answered 11/3, 2020 at 10:11 Comment(3)
Related AuthMode gets confused w/ multiple logged in usersMukluk
Thanks. I tried running my form/script in incognito mode with only one google account signed in, and now my scripts work as expected.Ida
Thank you forever!! Spent too many hours on this problem and the solution was so simple...Loci
P
5

Seems like a bug in the new engine. I have a similar problem. A function from the script is invoked from the HTML. The new engine fails. I disabled the V8 engine and it worked so it seems to be something internal to Google.

Phelan answered 3/3, 2020 at 23:30 Comment(0)
L
3

I experienced exactly the same problem and solved it disableling the new V8 engine.

Lexie answered 5/3, 2020 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.