如何将状态保持在JavaScript函数上显示为HTML

如何将状态保持在JavaScript函数上显示为HTML,javascript,html,google-apps-script,web-applications,Javascript,Html,Google Apps Script,Web Applications,这是我以前在这个论坛上所做的努力的延续 我通过添加else条件对代码进行了一些更改。我试图解决的是在页面加载时显示当前状态。当前代码仅在单击按钮时显示状态 如何获取页面加载时显示的当前状态以及单击按钮时更新的状态 代码.GS function doGet(e) { return HtmlService.createHtmlOutputFromFile('Index'); } function checkStatus(notify) { var employee = "Joh

这是我以前在这个论坛上所做的努力的延续

我通过添加else条件对代码进行了一些更改。我试图解决的是在页面加载时显示当前状态。当前代码仅在单击按钮时显示状态

如何获取页面加载时显示的当前状态以及单击按钮时更新的状态

代码.GS

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('Index');
}

function checkStatus(notify) { 
  var employee = "John Peter";
  var ss = SpreadsheetApp.getActiveSpreadsheet();        
  var mainSheet = ss.getSheetByName("MAIN");
  var data = mainSheet.getDataRange().getValues();
  
  for (var j = 0; j < data.length; j++){
    var row = data[j];
    var mainSheet2 = row[4];
    var mainSheet3 = row[0];
    var status = (mainSheet2 =="IN" && mainSheet3 == employee) ; 
    if (status == true){
      var notify = employee +" You Are In"
      return notify;
      }
    else{
     var status = (mainSheet2 =="OUT" && mainSheet3 == employee) ; 
    if (status == true){
      var notify2 = employee +" You Are Out"
      return notify2;
  }
}
  }
}
函数doGet(e){
返回HtmlService.createhtmloutpfromfile('Index');
}
功能检查状态(通知){
var employee=“约翰·彼得”;
var ss=SpreadsheetApp.getActiveSpreadsheet();
var mainSheet=ss.getSheetByName(“MAIN”);
var data=mainSheet.getDataRange().getValues();
对于(var j=0;j
Index.html

<body>
    <div>
      <button onclick="onStatus()">Check Status</button>
      <font color='Green' id="status" onbeforeprint="onStatus" ></font>
      </div>
      
    

    <script>
      function onStatus() {
        google.script.run.withSuccessHandler(updateStatus) // Send the backend result to updateStatus()
          .checkStatus(); // Call the backend function
      }

      function updateStatus(notify) {
        document.getElementById('status').innerHTML= notify;
      }
      
     
      
    </script>
  </body>

检查状态
函数onStatus(){
google.script.run.withSuccessHandler(updateStatus)//将后端结果发送到updateStatus()
.checkStatus();//调用后端函数
}
函数updateStatus(通知){
document.getElementById('status').innerHTML=notify;
}

您可以在页面加载时调用此函数,如下所示。在body标记结束之前,在末尾添加此脚本

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
    // The code in this function runs when the page is loaded.
    $(function() {
     google.script.run.withSuccessHandler(updateStatus)
              .checkStatus(); 
    });
</script>

//加载页面时,此函数中的代码将运行。
$(函数(){
google.script.run.withSuccessHandler(updateStatus)
.checkStatus();
});

您可以将函数包装在IIFE代码块中,该代码块将在页面加载时运行。(函数(){//您的函数代码})()@VimalPatel我不是编码员,我不知道IIFE代码块(我应该在代码中的何处添加花括号?