Jak sprawić, by Status nadal pojawiał się w funkcji JavaScript w HTML
Nov 30 2020
To jest kontynuacja poprzedniego pytania, które zadałem na tym forum.
Dokonałem pewnych zmian w kodzie, dodając warunek else. To, co staram się wypracować, to pokazać aktualny stan podczas ładowania strony. aktualny kod pokazuje status tylko po kliknięciu przycisku.
Jak mogę wyświetlić aktualny status, gdy strona się ładuje, a status jest aktualizowany po kliknięciu przycisku.
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>
Odpowiedzi
2 VimalPatel Nov 30 2020 at 15:43
Możesz wywołać tę funkcję podczas ładowania strony, jak poniżej. Dodaj ten skrypt na końcu, tuż przed końcem tagu 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>