自动更新Google Apps脚本webapp [英] Automatically update Google Apps Script webapp

查看:69
本文介绍了自动更新Google Apps脚本webapp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Google web app displaying an HTML dashboard with data pulled in via AJAX. The Spreadsheet is populated using a Google Form and I'd like the data (mock election results) to be updated every 10 seconds or so as votes come in so teachers can see live results.

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

Here's the relevant portion of the app:

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 或类似的文件?

What I can't figure it out is what actually pulls the data. Is the the success handler polling the server? Or is it the client-side script? Where should I include Utilities.sleep or similar?

推荐答案

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

It seems like this line is getting the data from the spreadsheet:

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

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

You could try wrapping this line in setInterval so that it is called every ten seconds:

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

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

There also may be a Google Apps Script equivalent for setInterval.

这篇关于自动更新Google Apps脚本webapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆