Google 张:Class google.script.run 不工作

Google sheets: Class google.script.run not working

我的问题很简单。我在网上搜索的所有可能的解决方案都没有解决我的问题。

Google 的 Class google.script.run (https://developers.google.com/apps-script/guides/html/reference/run#withSuccessHandler) 开发者网站展示了 myFunction(...) 方法(任何服务器端函数)。

我已经复制了他们的确切代码和html代码并推断出doSomething()函数没有执行。没有任何记录。

我打算用它来执行一个 HTML 文件,这样我就可以播放一个声音文件。到目前为止,我可以通过从侧面弹出一个侧边栏来做到这一点,如该线程中所讨论的:.

但是,Google 提供的这段代码不起作用。为什么?

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index');
}

function doSomething() {
  Logger.log('I was called!');
}

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      google.script.run.doSomething();
    </script>
  </head>
  <body>
  </body>
</html>

通过使用 google.script.run,您可以调用 server-side Apps 脚本函数。 https://developers.google.com/apps-script/guides/html/reference/run 请double-check您按照以下步骤正确操作:

  1. 请确保将代码的 html 部分放在单独的 HTML 文件中(通过文件->新建->HTML 文件创建)与您在 HtmlService.createHtmlOutputFromFile() 中调用的名称相对应的名称 - 在您的情况下 Index.html
  2. Select “doGet”作为函数运行.
  3. 将脚本部署为 Web 应用程序 - 这是使用 Apps 脚本 HTML 服务的要求。请在此处找到说明:https://developers.google.com/apps-script/guides/web
  4. 确保每次在代码中实施更改后,将脚本部署为新项目版本。这是更新更改所必需的。
  5. 打开您在更新版本后获得的当前 Web 应用 URL,以打开您的 html 输出。
  6. 在您的情况下,只会打开一个空的 HTML 文件,以测试功能 - 在您的 HTML 正文中插入一些文本,以测试正确的功能。后者可以在 运行 代码后通过查看日志来确认。