【发布时间】:2021-12-09 20:10:28
【问题描述】:
我正在尝试复制一个简单的 Web 应用教程视频,但无法让 google.script.run.function 在我的 code.gs 中创建日志。这是我的代码:
代码.gs
function doGet(){
return HtmlService.createHtmlOutputFromFile("clock");
}
function userClicked(){
Logger.log("Log clicked");
console.log("Console click");
}
clock.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1>test</h1>
<div id="replace">___</div>
<button id="submit">Submit</button>
<script>
document.getElementById("submit").addEventListener("click",doSubmit);
function doSubmit(){
google.script.run.userClicked();
document.getElementById("replace").innerHTML = "clicked";
}
</script>
</body>
</html>
当我在编辑器中运行 userClicked 时,执行日志显示正常。此外,当在 web 应用程序中单击按钮时,“单击”文本显示得很好。此外,doGet 和 userClicked 都出现在我的 Executions 中。问题是从 webapp 运行时日志不会显示在我的执行日志中。我找到了几个类似的线程,但它似乎永远不会得到解决。
更新:我也尝试添加 withSuccessHandler 并且结果是相同的 - 函数都在执行中运行良好,但没有日志显示在执行日志中(如果我在旧版本中测试,“日志”也是如此) .新的 html 是这样的:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1>test</h1>
<div id="replace">___</div>
<button id="submit">Submit</button>
<script>
document.getElementById("submit").addEventListener("click",doSubmit);
function onSuccess(){
document.getElementById("replace").innerHTML = "success";
}
function doSubmit(){
google.script.run.withSuccessHandler(onSuccess).userClicked();
}
</script>
</body>
</html>
【问题讨论】:
-
你需要使用 SuccessHandler
-
2-3 分钟后查看
executions(不是code editor) -
@TheMaster doGet 和 userClicked 几乎立即出现在 Executions 中。问题是“Log clicked”文本没有显示在编辑器日志中,我不知道为什么。
-
@Cooper 我刚刚更新以添加 withSuccessHandler;结果相同