Java脚本结果未显示在HTML中



我写了一个GAS代码来检查员工是否In(从谷歌工作表中提取数据(。控制台日志给了我正确的答案,但当我点击按钮时,答案不会出现在前端。你能帮我解决哪里出了问题吗?

<div>
<script>
function onStatus(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;

}
}
document.getElementById('status').innerHTML= notify;       
}

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

Google提供了一个非常好的客户端到服务器通信指南,我强烈建议您阅读该指南,以更好地了解其工作原理。

您不能将应用程序脚本代码(例如SpreadsheetApp.getActiveSpreadsheet()(放在前端脚本中。该代码必须由后端的应用程序脚本服务器运行,然后您将使用google.script.run调用来调用它。

代码.gs

function doGet(e) {
return HtmlService.createHtmlOutputFromFile('Index');
}
function checkStatus() { 
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){
return employee + " You Are In";
}
}
}

索引.html

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div>
<button onclick="onStatus()">Check Status</button>
<font color='Green' id="status" ></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>
</html>

最新更新