4
votes

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

3
It works for me.Cooper
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 commentCooper
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.Cooper
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 :/Brianne Davis

3 Answers

11
votes

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.

4
votes

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.

2
votes

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