HTML에 JavaScript 함수에 상태가 계속 표시되는 방법

Nov 30 2020

이것은 내가이 포럼에서 만든 이전 질문 의 연속입니다 .

else 조건을 추가하여 코드를 약간 변경했습니다. 내가 해결하려는 것은 페이지가로드 될 때 현재 상태를 표시하는 것입니다. 현재 코드는 버튼을 클릭 한 경우에만 상태를 표시합니다.

페이지가로드 될 때 현재 상태가 나타나고 버튼을 클릭하면 상태가 업데이트되도록하려면 어떻게해야합니까?

CODE.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;
  }
}
  }
}

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>

답변

2 VimalPatel Nov 30 2020 at 15:43

다음과 같이 페이지로드시이 함수를 호출 할 수 있습니다. 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>