google-apps-script - 如何从 HTML 文件调用 google 脚本函数?

标签 google-apps-script

我已经通过 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/

相关文章:

javascript - curl 如何映射到 Google App Script URLFetchApp

json - 本地化 Google 插件

javascript - 如何在 Google Apps 脚本中引用单元格

google-apps-script - 谷歌应用程序脚本 : Unexpected error while getting the method or property openById on object SpreadsheetApp

mysql - 尝试使用 jdbc 读入数据库时​​出现 Google Apps 脚本错误

javascript - 仅按谷歌应用程序脚本中的列设置值

javascript - 使用谷歌应用程序脚本将服务器端数据表返回给客户端

google-apps-script - 谷歌应用脚​​本: Export all Google contacts as CSV

google-apps-script - 如果 A 列为空,隐藏 Google 电子表格中的行吗?

javascript - 范围错误: Maximum call stack size exceeded on my first google script function