【问题标题】:Automatically update Google Apps Script webapp自动更新 Google Apps 脚本 webapp
【发布时间】:2016-10-31 14:14:05
【问题描述】:

我有一个显示 HTML 仪表板的 Google 网络应用程序,其中包含通过 AJAX 提取的数据。电子表格是使用 Google 表单填充的,我希望数据(模拟选举结果)每 10 秒左右更新一次,以便教师可以看到实时结果。

这是应用程序的相关部分:

code.gs

function getData() {
  var sheet = ss.getSheets()[3];
  var data = sheet.getDataRange().getValues();
  Logger.log(data);
  return JSON.stringify(data);
}

function doGet() {
  return HtmlService
    .createTemplateFromFile("index")
    .evaluate();
}

HTML 模板

      function popularVote(data) {
        var getDivs = document.getElementsByClassName("percent");
        var data = JSON.parse(data);

        for(var j=0;j<getDivs.length;j++) {
          getDivs[j].textContent = ((data[j][1]/data[j][2]) * 100).toFixed(1) + "%";
        }
      }
      google.script.run.withSuccessHandler(popularVote).getData();

      <div class="card" id="popular">
          <div class="cand" id="cand1">
            <h2>Candidate:</h2><span class="percent">Loading...</span>
          </div>
          <div class="cand" id="cand2">
            <h2>Candidate:</h2><span class="percent">Loading...</span>
          </div>
          <div class="cand" id="cand3">
            <h2>Candidate:</h2><span class="percent">Loading...</span>
          </div>
        </div>

我无法弄清楚实际提取数据的是什么。成功处理程序是否轮询服务器?还是客户端脚本?我应该在哪里添加Utilities.sleep 或类似名称?

【问题讨论】:

  • 嘿顺便说一句,您是如何设法在 google sciprts 中缩进您的代码的?当我在编辑 HTML 文件时尝试使用选项卡时,它不会工作

标签: javascript google-apps-script


【解决方案1】:

似乎这一行正在从电子表格中获取数据:

google.script.run.withSuccessHandler(popularVote).getData();

您可以尝试将此行包装在 setInterval 中,以便每十秒调用一次:

setInterval(function(){ 
 google.script.run.withSuccessHandler(popularVote).getData(); 
 }, 10000);

也可能有与 setInterval 等效的 Google Apps 脚本。

【讨论】:

  • setTimeout 在 10 秒后运行了一次,但这让我找到了 setInterval,效果很好。谢谢。
  • 哈哈,对我而言。我更新了答案,改为使用 setInterval。
  • setInterval 效果很好,但是没有办法像实时一样更新html页面吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多