首页 > 解决方案 > “ScriptError:需要授权才能执行该操作。” 从库运行 google.script.run 时

问题描述

问候,

我发现了几个关于此错误的问题: "ScriptError: Authorisation is required to perform that action."但我找不到与我的问题有关的问题。

我想要做的是使用 google.script.run 从 .html 文件调用函数 .gs 文件,其中两个文件都在库中。参考这个答案这个答案这个错误报告,我在使用库的脚本中创建了“包装函数”,但仍然未能完成执行。

这是我所做的:

.html 在库中:

<html>

<head>
  <script>
    function onFailure(error) {
      console.log("ERROR: " + error);
    }

    function myfunc() {
      console.log("HERE");
      google.script.run.withFailureHandler(onFailure).callLibraryFunction('LibraryName.test', ['test123']);
    }
  </script>
</head>

<body>
  <button onclick="myfunc()">CLICK</button>
</body>

</html>

.gs 在库中

function test(x){
  Logger.log(x);
}

.gs 在使用库的脚本中:

function callLibraryFunction(func, args) {
  var arr = func.split(".");

  var libName = arr[0];
  var libFunc = arr[1];

  args = args || [];

  return this[libName][libFunc].apply(this, args);
}

控制台记录HERE但随后它记录ERROR: ScriptError: Authorisation is required to perform that action.而不是预期的输出,test123.

注意:HTML 用于表格中的自定义无模式对话框,不适用于 Web 应用程序。

我真的希望有人能在这方面帮助我。先感谢您。

标签: javascriptgoogle-apps-scriptgoogle-sheetsmodeless-dialog

解决方案


您需要授予对库“LibraryName”的访问权限作为您的 Gmail 帐户中的授权应用程序,就像您授予对调用脚本的访问权限一样。HtmlService.createHtmlOutputFromFile(...)我猜你已经在你的库中调用了这个方法。这需要更多的授权。您需要授予此权限。您可以做的是在具有HtmlService.createHtmlOutputFromFile(..). 从脚本编辑器运行它并出现授权要求窗口。继续授予访问权限...


推荐阅读