Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 903
Brianne Davis Avatar asked Dec 01 '22 09:12

Brianne Davis


1 Answers

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.

like image 133
res_cogitans Avatar answered Dec 04 '22 01:12

res_cogitans