2016-10-31 102 views
0

我有一個Google網絡應用程序,顯示一個HTML儀表板,並通過AJAX提取數據。電子表格使用Google表單填充,我希望數據(模擬選舉結果)每10秒鐘更新一次,以便投票進入,以便教師可以看到實時結果。自動更新Google Apps腳本網絡應用程序

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

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或類似的東西?

回答

0

好像這條線是從電子表格獲取數據:

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

你可以嘗試包裝中的setInterval此行,它每隔十秒稱爲:

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

還有可能是與setInterval等效的Google Apps腳本。

+0

'setTimeout'在10秒後運行一次,但是這導致了'setInterval'這很好。謝謝。 – Brian

+0

哈哈,就我而言。我更新了使用setInterval的答案。 –

相關問題