菜鸟,过去一周我一直遇到这个问题,我不明白为什么 javascript 不会看到 code.gs 的返回值,因为我有相同的代码,但不同的函数按预期工作。
是否有原因导致 Logger.log 中的日志值下方的 code.gs 函数意味着它有数据要返回,但 javascript 不会获取任何值并在警报中给我一个“null”?
代码.gs
function getInvoicesForID(studentAltID) {
var invoicesForID = [];
//get general invoices sheet and values
var sInvoices = ss.getSheetByName("Invoices");
var dataInvoices = sInvoices.getDataRange().getValues();
//get balance info for id
for(var i = 0; i < dataInvoices.length; i++){
if(dataInvoices[i][4]==studentAltID){
invoicesForID.push([dataInvoices[i][0],dataInvoices[i][1],dataInvoices[i][2],dataInvoices[i][3]]);
break;
}
}
Logger.log("invoicesForID = " + invoicesForID);
return invoicesForID;
}
javascript.html
document.getElementById("btnsearchInvPayBalforStudentID").addEventListener("click",searchInvPayBalforStudentID);
//function to look for Payments, Invoices and Balance
function searchInvPayBalforStudentID()
{
try{
//get the id
var stID = document.getElementById("txtStudentID").value;
google.script.run.withSuccessHandler(getInvoices)
.getInvoicesForID(stID);
}catch(e){
alert(e);
}
}
function getInvoices(stIDInvData) {
try{
alert(stIDInvData);
}catch(e){
alert(e);
}
}
当代码执行并检查日志时,我确实看到来自 gs 函数的数据,如下所示,这是传递的 StudentAltID 的预期数据的数据
[19-07-04 22:12:13:491 EDT]发票ForID = 2019 年 1 月 31 日星期四 00:00:00 GMT-0500 (EST),34073,Matricula 2019,298854
我错过了什么?
提前谢谢您:)
更新:
我包含了事件处理程序(单击时的按钮),我检查了语法错误和括号错配,但找不到问题
这是该项目的链接,其中包含我在帖子中清理的更多项目,希望它可能有所帮助
谢谢
最佳答案
(...光束照射进来,天使在背景中齐声唱着哈利路亚...)
我在这里找到了答案: Unable to return Array [Google apps script]来自@VishnarTadeleratha
事实证明,我无法在数组中传递日期,而该数组正是返回的数组中的项目之一: google 说:“参数:...如果您尝试传递除表单之外的日期、函数、DOM 元素或其他禁止类型,包括对象或数组内的禁止类型,请求将失败...来源:https://developers.google.com/apps-script/guides/html/reference/run
一旦我从返回数组中删除该项目,javascript 就会得到与 null 不同的值..万岁。
如果我仍然需要在数组中传递日期(我确实这么做了),@VishnarTadeleratha 还建议将该变量转换为字符串,瞧……我的长屁股问题现在已经解决了……终于我可以移动了在.. 干杯
关于javascript - google.script.run 执行但返回 "null"而 code.gs 函数确实有/日志值要返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56895811/