![](/img/trans.png)
[英]Can google.script.run.withSuccessHandler return area?
[英]google.script.run.withSuccessHandler does not return value
这让我发疯,代码昨天还在工作,但现在不行了。 我尝试再次检查所有语法,但问题仍然存在。 这个来自 Google 表格的服务器端请求在服务器端显示值( Logger.log()
),但在客户端返回null
。
function supervisorLine(lineData) {
if (lineData == 'Name Value is not VALID!') {
console.log("Supervisor Name Issue!");
} else {
document.getElementById('Team').value = lineData[7];
document.getElementById('Shift').value = lineData[12];
document.getElementById('action').classList.remove("disabled");
console.log("team " + lineData[7] + " shift " + lineData[12]);
////////////////////////////////// need to be Moved somewhere after password check!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
google.script.run.withSuccessHandler(function(quickStat2) {
console.log(quickStat2)
}).loginRecords(lineData[7], lineData[12]);
}
}
这也是我的服务器端代码:
function loginRecords(team, shift) {
var sh = ss.getSheetByName("Attn Rec");
var result = [];
var matchRec = sh.getRange("a2:l" + sh.getLastRow()).getValues().filter(function(a) {
return a[1] === shift && a[4].valueOf() == team.valueOf()
});
uniqeLogin = removeDups(matchRec.map(function(a) {
return a[9]
}));
// Logger.log(uniqeLogin);
uniqeLogin.forEach(function(a) {
var ary = [team, 0, shift, "", 0, 0, 0, 0, 0];
matchRec.forEach(function(r) {
if (r[9] === a) {
ary[1] = new Date(r[0]);
ary[3] = r[8];
switch (r[3].toString().toLowerCase()) {
case "off-site work":
case "hr action":
ary[8]++;
break;
case "present":
case "late transfer":
case "transfer":
ary[4]++;
break;
case "no show":
ary[5]++;
break;
case "Sick":
case "vacation":
ary[7]++;
break;
case "late":
case "approved delay start":
ary[6]++;
break;
}
}
});
result.push(ary);
});
Logger.log(result);
return result;
}
回顾一下, Logger.log(result)
返回我需要的数组,但console.log(quickStat2)
返回null
。
前段时间我遇到了这个问题,它也几乎把我逼疯了(哦,松散类型 JavaScript 的乐趣!)。 问题是您试图将不可接受的数据类型返回给客户端。 通过google.script.run
调用的函数对它们可以返回的google.script.run
有限制(例如,您应该避免Date
实例)。
受限类型
目前,您无法返回(查看文档以了解限制的详细说明):
Date
实例;Function
;<form>
是允许的);解决方案
更改ary[1]= new Date(r[0]);
到ary[1] = r[0];
应该可以解决问题,只需将Date
解析移动到客户端即可。
对我来说,它与谷歌的黑盒序列化过程有关。 如果该值源自range.getValues()
调用或其他 Google API,则它可能不是普通对象/数组/基元,可能存在复杂的底层对象或未通过本机序列化的意外Date
。
强制执行常规 JS 序列化可避免此问题。
return JSON.parse(JSON.stringify(obj));
完整示例:
function getFirstCellInfo() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DB");
const values = sheet.getRange(1, 1).getValues();
const row = values[0];
const cellInfo = {
value: row[0],
valueType: typeof row[0]
};
const serializedCellInfo = JSON.parse(JSON.stringify(cellInfo));
// serializedCellInfo is guaranteed to be a plain JS object
return serializedCellInfo;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.