问题描述
我的问题很简单.我在网上搜索的所有可能的解决方案都没有解决我的问题.
My problem is simple. All the possible solutions I searched for online did not address my question.
针对google.script.run类的Google开发者网站( https://developers.google.com/apps-script/guides/html/reference/run#withSuccessHandler )展示了方法myFunction(...)
(任何服务器端函数).
Google's developer website for Class google.script.run (https://developers.google.com/apps-script/guides/html/reference/run#withSuccessHandler) showcased the method myFunction(...)
(any server-side function).
我已经复制了它们的确切代码和html代码,并推断出函数doSomething()
无法执行.什么都没有记录.
I have copied their exact code and html code and deduced that the function doSomething()
does not execute. Nothing gets logged.
我打算用它来执行HTML文件,以便可以播放声音文件.到目前为止,我可以通过从侧面弹出侧边栏来做到这一点,如该线程中所述:.
I intend to use this to execute an HTML file so that I could play a sound file. I could do this so far with a sidebar popping up from the side, as discussed in this thread: Google Script: Play Sound when a specific cell change the Value.
但是,Google提供的此代码无效.为什么?
However, this code provided by Google does not work. Why?
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 请仔细检查您是否按照以下步骤正确进行操作:
By using google.script.run you are calling a server-side Apps Script function.https://developers.google.com/apps-script/guides/html/reference/runPlease double-check that you follow the following steps to do it correctly:
- 请确保将代码的html部分放在一个单独的HTML文件(通过File-> New-> HTML文件创建)中,其名称与您在HtmlService.createHtmlOutputFromFile()中调用的名称相对应-您的情况是Index.html
- 选择"doGet"作为要运行的功能.
- 将脚本部署为Web应用程序-这是使用Apps Script HTML服务的要求.请在此处找到说明: https://developers.google.com/apps-script/指南/网络
- 确保每次在代码中实施更改后,都将脚本部署为新的项目版本.这是更新更改所必需的.
- 打开更新版本后获得的当前Web应用程序URL,以打开html输出.
- 在您的情况下,将仅打开一个空HTML文件以测试功能-在HTML正文中插入一些文本以测试正确的功能.可以通过在运行代码后查看日志来确认后者.
- Please make sure that you put the html part of the code in a separate HTML file (which you create through File->New->HTML file) with the name corresponding to the one you are calling in HtmlService.createHtmlOutputFromFile() - in your case Index.html
- Select "doGet" as the function to be run.
- Deploy the script as a web app - this is the requirement for using Apps Script HTML service. Please find the instructions here: https://developers.google.com/apps-script/guides/web
- Make sure that every time after you implement changes in your code, you deploy the script as a NEW project version. This is necessary to update the changes.
- Open the current web app URL you obtain after updating your version, to open your html output.
- In your case only an empty HTML file will be opened, to test functionality - insert some text in your HTML body, to test the correct functionality. The latter can be confirmed by viewing the Logs after running the code.
这篇关于Google表格:google.script.run类不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!