我已经通过 google apps 脚本中的 Html Services api 创建了一个网页。在脚本中,我有一个 google script(.gs) 文件和两个 html 文件(.html)。该脚本发布在网络上。为了在网络上显示 html 文件,我在 .gs 文件中使用了以下函数:
function doGet() { //Published on web
return HtmlService.createTemplateFromFile('<htmlFile_1>').evaluate();
}
function doProcess() {
return HtmlService.createTemplateFromFile('<htmlFile_2>').evaluate();
}
doGet() 正在网络上返回此 Html 文件。现在我想通过替换这个文件来显示另一个 Html 文件。因此,我在 htmlFile_1 中使用了以下内容:
//htmlFile_1.html
<html>
<head>
<script>
function loadMainHtmlPage(){
google.script.run.doProcess(); //calling another function in .gs file
setTimeout(function(){hello()},4000);
}
function hello(){
alert("hiii");
document.getElementById("loading").style.display="none";}
</script>
</head>
<body onload="loadMainHtmlPage();">
<div id="loading" style="display:block;">
<img src="http://commondatastorage.googleapis.com/kickoff/loading.gif"/>
</div>
</body>
</html>
这个 htmlFile_1 没有调用 doProcess(),它将返回 htmlFile_2。' 有什么建议来实现这个吗?
最佳答案
您需要在这行代码中包含一个 onSuccess (以及可选的 onFailure)处理程序
google.script.run.doProcess();
见下文
Server code
function getSuperHero() {
return {name: "SuperGeek", catch_phrase: "Don't worry ma'am, I come from the Internet" };
}
Client code
<script>
function onSuccess(hero) {
alert (hero.catch_phrase);
}
google.script.run.withSuccessHandler(onSuccess).getSuperHero();
</script>
关于google-apps-script - 如何从 HTML 文件调用 google 脚本函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13928649/