Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 从该库中的html调用库函数_Google Apps Script_Web Applications_Google Docs - Fatal编程技术网

Google apps script 从该库中的html调用库函数

Google apps script 从该库中的html调用库函数,google-apps-script,web-applications,google-docs,Google Apps Script,Web Applications,Google Docs,为了与同事们分享我正在开发的AppsScript代码,我创建了一个独立的项目,用作文档模板中的库 此库包含: 服务器端代码的.gs文件 客户端侧边栏的.html文件 在侧边栏中,我有一个按钮,可以触发对库中带有参数的函数的调用 javascript调用如下所示: google.script.run .withFailureHandler( function(msg, element) { sh

为了与同事们分享我正在开发的AppsScript代码,我创建了一个独立的项目,用作文档模板中的库

此库包含:

  • 服务器端代码的.gs文件
  • 客户端侧边栏的.html文件
在侧边栏中,我有一个按钮,可以触发对库中带有参数的函数的调用

javascript调用如下所示:

        google.script.run
            .withFailureHandler(
              function(msg, element) {
                showError(msg, $('#button-bar'));
              })
            .test();
我在其他页面上读到库代码没有公开,因此测试函数实际上在我文档的AppsScript代码中,并调用等效的库函数。 正如这里所建议的那样:

文档AppsScript中的代码:

function test()
{
  myLibrary.test();
}
myLibrary库中的代码:

function test()
{
  DocumentApp.getUi().alert('test');
}
问题是javascript的失败处理程序返回一个ScriptError,指出我需要获得执行此操作的授权

你知道我做错了什么吗


PS:我知道我可以制作一个附加组件,但这不是我公司内部可以轻松做到的:)

是的,当您使用库中的html代码(侧边栏)时,它将调用绑定到文档的脚本中的
test()
函数,而不是库中的函数

您需要向用户请求权限才能提示UI元素,您可以使用自定义菜单[1]来完成此操作

此外,用户至少需要对库具有读取权限[2]。我添加了
withSuccessHandler
函数[3],以在侧栏中显示客户端的响应。以下代码适用于我:

绑定到文档的脚本:

function onOpen() {
  DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show sidebar', 'showSidebar')
      .addToUi();
}

function showSidebar() {
  myLibrary.showSidebarLibrary();
}

//
function test() {
  myLibrary.test();
}
库:

代码.gs

function showSidebarLibrary() {
  var html = HtmlService.createHtmlOutputFromFile('Page')
      .setTitle('My custom sidebar')
      .setWidth(300);
  DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .showSidebar(html);
}

function test() {
  DocumentApp.getUi().alert('test');
  return "Success";
}
Page.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Hello, world!<br>
    <input type="button" value="Alert" onclick="alert()" /><br>
    <input type="button" value="Close" onclick="google.script.host.close()" />
    <p id="msg">Replace message with response<p>
    </body>
    <script>
        function alert() {
            google.script.run.withFailureHandler(handler).withSuccessHandler(handler).test();
        }
        function handler(msg) {
            document.getElementById("msg").innerHTML = msg;
        }
  </script>
</html>

你好,世界

将消息替换为响应 函数警报(){ google.script.run.withFailureHandler(handler).withSuccessHandler(handler.test(); } 函数处理程序(msg){ document.getElementById(“msg”).innerHTML=msg; }

[1]

[2]


[3]

是的,当您使用库中的html代码(侧栏)时,它将调用绑定到文档的脚本中的
test()
函数,而不是库中的函数

您需要向用户请求权限才能提示UI元素,您可以使用自定义菜单[1]来完成此操作

此外,用户至少需要对库具有读取权限[2]。我添加了
withSuccessHandler
函数[3],以在侧栏中显示客户端的响应。以下代码适用于我:

绑定到文档的脚本:

function onOpen() {
  DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show sidebar', 'showSidebar')
      .addToUi();
}

function showSidebar() {
  myLibrary.showSidebarLibrary();
}

//
function test() {
  myLibrary.test();
}
库:

代码.gs

function showSidebarLibrary() {
  var html = HtmlService.createHtmlOutputFromFile('Page')
      .setTitle('My custom sidebar')
      .setWidth(300);
  DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .showSidebar(html);
}

function test() {
  DocumentApp.getUi().alert('test');
  return "Success";
}
Page.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Hello, world!<br>
    <input type="button" value="Alert" onclick="alert()" /><br>
    <input type="button" value="Close" onclick="google.script.host.close()" />
    <p id="msg">Replace message with response<p>
    </body>
    <script>
        function alert() {
            google.script.run.withFailureHandler(handler).withSuccessHandler(handler).test();
        }
        function handler(msg) {
            document.getElementById("msg").innerHTML = msg;
        }
  </script>
</html>

你好,世界

将消息替换为响应 函数警报(){ google.script.run.withFailureHandler(handler).withSuccessHandler(handler.test(); } 函数处理程序(msg){ document.getElementById(“msg”).innerHTML=msg; }

[1]

[2]


[3]

这正是我所做的,所以我尝试了你的代码,它也做了同样的事情。警报不知道,我收到一个脚本错误,您必须具有执行此操作的权限。我不是在标准的谷歌用户帐户上做这件事,而是在公司帐户上做这件事,是否有一些相关的限制?他们是否有权访问库脚本?正如我所解释的,我测试了代码,它成功地工作了,与我的G套件域中的一个用户共享了库。我是测试者,我使用了库和文档,所以我可以同时访问它们。它工作了吗?如果有帮助,请考虑接受正确的答案。如果您使用与我相同的代码,并且库版本在两个脚本中都进行了更新,那么它应该可以工作。以防万一,以下是步骤:1)在文档上,单击“显示侧栏”菜单,该菜单将请求所需的权限并打开侧栏。2) 在侧边栏上,单击
Alert
按钮。3) 在警报对话框中单击“确定”。4) 侧边栏元素中的文本将更改为
success
。这正是我所做的,因此我尝试了您的代码,它也会这样做。警报不知道,我收到一个脚本错误,您必须具有执行此操作的权限。我不是在标准的谷歌用户帐户上做这件事,而是在公司帐户上做这件事,是否有一些相关的限制?他们是否有权访问库脚本?正如我所解释的,我测试了代码,它成功地工作了,与我的G套件域中的一个用户共享了库。我是测试者,我使用了库和文档,所以我可以同时访问它们。它工作了吗?如果有帮助,请考虑接受正确的答案。如果您使用与我相同的代码,并且库版本在两个脚本中都进行了更新,那么它应该可以工作。以防万一,以下是步骤:1)在文档上,单击“显示侧栏”菜单,该菜单将请求所需的权限并打开侧栏。2) 在侧边栏上,单击
Alert
按钮。3) 在警报对话框中单击“确定”。4) 侧边栏元素中的文本将更改为
success