[英]Google Apps Script - Returning Undefined
What I want to do:我想做的事:
Please note that I can do it if the processing of data is in html, but I want to practice doing it in Code.gs since I want to fiddle with Google Sheets in the future, I just wanted to study it little by little.请注意,如果数据的处理是在html中,我可以做到,但我想在Code.gs中练习,因为我想在未来摆弄Google Sheets,我只是想一点一点地研究它。 But when I transferred the processing inside Code.gs, the returned data is "undefined".
但是当我在 Code.gs 中转移处理时,返回的数据是“未定义的”。 Your help is very much appreciated.
非常感激你的帮助。 Thank you very much!
非常感谢!
Present code:现在的代码:
Code.gs:代码.gs:
function doGet(e) {
return HtmlService
.createTemplateFromFile('Index').evaluate();
}
function getData(a,b){
var lastName = a;
var firstName = b;
var txtResult = "Hello " + lastName + ", " + firstName;
return txtResult;
}
Index.html:索引.html:
<!DOCTYPE html>
<html>
<body>
<div>
<label>Enter Last Name</label>
<input type="text" id="txtLastName"/></br>
<label>Enter First Name</label>
<input type="text" id="txtFirstName"/></br>
<button id="btnProcess">Process</button></br>
<input type="text" id="txtResult"/>
</div>
</body>
<script>
document.getElementById("btnProcess").addEventListener("click", processData);
function processData() {
var lastName = document.getElementById("txtLastName").value
var firstName = document.getElementById("txtFirstName").value
document.getElementById("txtResult").value = google.script.run.getData(lastName,firstName);
}
</script>
</html>
You are missing the withSuccessHandler and withFailureHandler (optional).您缺少withSuccessHandler和withFailureHandler (可选)。
<script>
document.getElementById("btnProcess").addEventListener("click", processData);
function processData() {
var lastName = document.getElementById("txtLastName").value
var firstName = document.getElementById("txtFirstName").value
google.script.run.withFailureHandler(errorFunction).withSuccessHandler(successFunction).getData(lastName,firstName);
}
function errorFunction(errorMsg) {
console.log("Something went wrong");
}
function successFunction(txtResult) {
document.getElementById("txtResult").value = txtResult;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.