自动更新 Google Apps 脚本网络应用程序

问题描述 投票:0回答:1

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

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

代码.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
或类似内容?

javascript google-apps-script web-applications
1个回答
0
投票

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

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.