簡體   English   中英

自動更新Google Apps腳本webapp

[英]Automatically update Google Apps Script webapp

我有一個Google Web應用程序顯示一個HTML儀表板,並通過AJAX提取數據。 電子表格是使用Google表單填充的,我希望每10秒鍾左右更新一次數據(模擬選舉結果),以便投票通過,以便老師可以看到實時結果。

這是應用程序的相關部分:

代碼

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.script.run.withSuccessHandler(popularVote).getData();

您可以嘗試將此行包裝在setInterval中,以便每十秒鍾調用一次:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM