Java脚本结果未以HTML显示 [英] The Java Script Results not showing in HTML
问题描述
我写了GAS代码来检查员工是否加入(从Google工作表中提取数据).控制台日志给了我正确的答案,但是当我单击按钮时,答案没有出现在前端.您能帮我解决哪里出错了吗?
I wrote a GAS code to check if the employee is In or Not in (extracting data from Google sheets). The console log give me the right answer but When I click on the button the answer doesn't appear at the front end. Can you help me to troubleshoot on where I went wrong?
<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 provides a very good Client-to-Server Communication guide that I highly suggest you read to get a better understanding of how this works.
您不能在前端脚本中放置应用程序脚本代码(例如 SpreadsheetApp.getActiveSpreadsheet()
).该代码必须由后端中的apps脚本服务器运行,然后您可以使用 google.script.run
调用.
You cannot put apps script code (e.g. SpreadsheetApp.getActiveSpreadsheet()
) in your frontend scripts. That code has to be run by the apps script server in the backend and you'll then call it using a google.script.run
call.
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";
}
}
}
Index.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>
这篇关于Java脚本结果未以HTML显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!