【发布时间】:2021-03-23 17:13:36
【问题描述】:
我正在使用 GoogleSheets HTMLService。我从我的 Html 页面的脚本中调用 google.script.run。但它总是去FailureHandler。它有什么问题?请看下面的代码。当我运行它时,它总是显示警报失败。此外,记录器没有显示任何错误。它也没有在 hello() 函数中显示控制台日志“Inside Hello”。我们是否还需要做一些浏览器设置(我使用的是 chrome - 允许使用 javascript)。
[更新] 将 Logger.log 替换为 console.log 后,我将其视为传输错误。
modeDialog.gs
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile("test");
html.setWidth(90).setHeight(1);
var ui = SpreadsheetApp.getUi().showModalDialog(html, "Opening ..." );
}
function hello() {
console.log("Inside Hello");
return "hello";
}
test.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function onSuccess(str) {
window.alert("executed");
}
function onFailure(error) {
window.alert("failed");
Logger.log(error);
}
google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).hello();
</script>
</head>
<body>
<div id="failureContent"></div>
Hello world
</body>
</html>
【问题讨论】:
-
Logger在客户端不可用。请改用console。在开发工具> 浏览器控制台中查看错误日志 -
在您的情况下,我认为
google.script.run可能会出现类似Failed to load resource: the server responded with a status of 500的错误。在我的环境中,我也使用google.script.run确认了这种情况。在这种情况下,当我创建新的电子表格并将脚本复制并粘贴到脚本编辑器并再次运行时,问题就解决了。那么在你的环境中,当你这样做的时候,你会得到什么结果呢?在当前阶段,我无法在问题跟踪器中找到此问题。所以我不确定这是否是您问题的直接解决方案。对此我深表歉意。 -
尝试将其放入
window.onload -
@shanti 我不确定传输错误是否重要。是什么让您认为
hello没有运行?你检查过服务器日志吗?查看> 处决? -
好的,所以我尝试了 FireFox,它会转到成功处理程序并显示警报“已执行”。 chrome 出了点问题……
标签: google-apps-script google-sheets