Google 表格:类 google.script.run 不起作用

2023-12-25

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

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

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

我打算用它来执行 HTML 文件,以便我可以播放声音文件。到目前为止,我可以通过从侧面弹出一个侧边栏来完成此操作,如本线程中所述:Google 脚本:当特定单元格更改值时播放声音 https://stackoverflow.com/questions/40881605/google-script-play-sound-when-a-specific-cell-change-the-value.

然而,谷歌提供的这段代码不起作用。为什么?

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 您将调用服务器端 Apps 脚本函数。https://developers.google.com/apps-script/guides/html/reference/run https://developers.google.com/apps-script/guides/html/reference/run请仔细检查您是否正确执行了以下步骤:

  1. 请确保将代码的 html 部分放在一个单独的 HTML 文件中(通过文件 -> 新建 -> HTML 文件创建),其名称与您在 HtmlService.createHtmlOutputFromFile() 中调用的文件相对应 - 在您的案例索引.html
  2. 选择“doGet”作为要运行的函数。
  3. 将脚本部署为 Web 应用程序 - 这是使用 Apps 脚本 HTML 服务的要求。请在此处查找说明:https://developers.google.com/apps-script/guides/web https://developers.google.com/apps-script/guides/web
  4. 确保每次在代码中实施更改后,都将脚本部署为新的项目版本。这是更新更改所必需的。
  5. 打开更新版本后获得的当前 Web 应用程序 URL,以打开 html 输出。
  6. 在您的情况下,只会打开一个空的 HTML 文件来测试功能 - 在 HTML 正文中插入一些文本,以测试正确的功能。后者可以通过运行代码后查看日志来确认。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google 表格:类 google.script.run 不起作用 的相关文章

随机推荐