ผลลัพธ์ Java Script ไม่แสดงใน HTML
Nov 30 2020
ฉันเขียนรหัส GAS เพื่อตรวจสอบว่าพนักงานอยู่ในหรือไม่อยู่ (ดึงข้อมูลจาก Google ชีต) บันทึกของคอนโซลให้คำตอบที่ถูกต้องแก่ฉัน แต่เมื่อฉันคลิกที่ปุ่มคำตอบจะไม่ปรากฏที่ส่วนหน้า คุณช่วยฉันแก้ปัญหาที่ฉันผิดพลาดได้ไหม
<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>
คำตอบ
4 Diego Nov 30 2020 at 10:49
Google มีคู่มือการสื่อสารระหว่างไคลเอ็นต์กับเซิร์ฟเวอร์ที่ดีมากซึ่งเราขอแนะนำให้คุณอ่านเพื่อทำความเข้าใจวิธีการทำงานนี้ให้ดียิ่งขึ้น
คุณไม่สามารถใส่รหัสสคริปต์ของแอพ (เช่นSpreadsheetApp.getActiveSpreadsheet()) ในสคริปต์ส่วนหน้าของคุณ โค้ดนั้นจะต้องถูกเรียกใช้โดยเซิร์ฟเวอร์สคริปต์ของแอพในแบ็กเอนด์จากนั้นคุณจะเรียกมันโดยใช้การgoogle.script.runโทร
Code.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";
}
}
}
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>