最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 (). 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.

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

Share Improve this question edited Aug 19, 2020 at 12:27 Marios 27.4k8 gold badges38 silver badges58 bronze badges asked Feb 27, 2020 at 22:53 Brianne DavisBrianne Davis 512 bronze badges 9
  • It works for me. – Cooper Commented Feb 27, 2020 at 23:01
  • 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 comment – Cooper Commented Feb 27, 2020 at 23:04
  • "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", ], – Cooper Commented Feb 27, 2020 at 23:06
  • 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 Commented Feb 27, 2020 at 23:06
  • 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 Commented Feb 28, 2020 at 0:25
 |  Show 4 more comments

3 Answers 3

Reset to default 16

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.

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.

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论