[英]google.script.run.withSuccessHandler does not return value
it is getting me crazy, the code was working yesterday, but not anymore.这让我发疯,代码昨天还在工作,但现在不行了。 I tried to check all syntax again, but the issue still persists.我尝试再次检查所有语法,但问题仍然存在。 this server-side request from Google Sheets, shows value on server side ( Logger.log()
), but returns null
in client side.这个来自 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]);
}
}
this is my server side code as well:这也是我的服务器端代码:
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;
}
for recap, Logger.log(result)
returns the array I needed, but console.log(quickStat2)
returns null
.回顾一下, Logger.log(result)
返回我需要的数组,但console.log(quickStat2)
返回null
。
I bumped into this issue some time ago, and it also nearly drove me mad (oh, the joys of loosely-typed JavaScript!).前段时间我遇到了这个问题,它也几乎把我逼疯了(哦,松散类型 JavaScript 的乐趣!)。 The problem is that you are trying to return an unacceptable type of data to client-side.问题是您试图将不可接受的数据类型返回给客户端。 Functions called via google.script.run
have restrictions on what they can return (for example, you should avoid Date
instances).通过google.script.run
调用的函数对它们可以返回的google.script.run
有限制(例如,您应该避免Date
实例)。
Restricted types受限类型
Currently, you can't return (take a look at the documentation for detailed explanation of restrictions):目前,您无法返回(查看文档以了解限制的详细说明):
Date
instances; Date
实例;Function
;任何Function
;<form>
is permitted); DOM 元素(尽管<form>
是允许的);Solution解决方案
Changing ary[1]= new Date(r[0]);
更改ary[1]= new Date(r[0]);
to ary[1] = r[0];
到ary[1] = r[0];
should do the trick, simply move Date
parsing to the client.应该可以解决问题,只需将Date
解析移动到客户端即可。
For me it was related to Google's black-box serialization process.对我来说,它与谷歌的黑盒序列化过程有关。 If the value originates from a range.getValues()
call or other Google API it might not be plain objects/arrays/primitives, there might be complex underlying objects or unexpected Date
s that fail the native serialization.如果该值源自range.getValues()
调用或其他 Google API,则它可能不是普通对象/数组/基元,可能存在复杂的底层对象或未通过本机序列化的意外Date
。
Forcing a regular JS serialization avoids the issue.强制执行常规 JS 序列化可避免此问题。
return JSON.parse(JSON.stringify(obj));
full example:完整示例:
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.