为什么日期长度返回 Null?
我有一个数组变量,用于获取电子表格的值
我想检查某些单元格是否为空,因此我查看数组每个元素的长度
但令我惊讶的是,如果元素是日期,则长度返回“无效的”。它不应该返回日期的完整长度吗? (例如,Wed Dec 01 03:00:00 GMT-05:00 2021 应返回 34)
有人可以向我解释这种行为吗?
这是我使用的代码:
在我的表格中,单元格 I1 是日期,单元格 J1 为空此
var sheet = SpreadsheetApp.getActive().getSheetByName('Example');
var tab = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
Logger.log(tab[0][8]);
Logger.log(tab[0][8].length);
Logger.log(tab[0][9]);
Logger.log(tab[0][9].length);
返回:
1:28:09 PM Notice Execution started
1:28:15 PM Info Wed Dec 01 03:00:00 GMT-05:00 2021
1:28:17 PM Info null
1:28:18 PM Info
1:28:19 PM Info 0.0
I have an array variable that get the values of a spreadsheet
I want to check if some of the cells are empty, so I look the length of each element of the array
But to my surprise, if the element is a date, the length returns "null". Shouldn't it return the full length of the date ? (e.g. Wed Dec 01 03:00:00 GMT-05:00 2021 should return 34)
Can someone explain this behaviour to me ?
Here is the code I use:
In my table the cell I1 is a date and the cell J1 is empty
var sheet = SpreadsheetApp.getActive().getSheetByName('Example');
var tab = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
Logger.log(tab[0][8]);
Logger.log(tab[0][8].length);
Logger.log(tab[0][9]);
Logger.log(tab[0][9].length);
This return:
1:28:09 PM Notice Execution started
1:28:15 PM Info Wed Dec 01 03:00:00 GMT-05:00 2021
1:28:17 PM Info null
1:28:18 PM Info
1:28:19 PM Info 0.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据注释,我们可以编辑代码并使用两种替代方案来转换字符串并避免 null。
Based on the comments we can edit the code and use both alternatives to convert the string and avoid the null.
只有 Google Apps 脚本产品工程师才能回答原因,但要基于 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date 长度不是 Date 对象的属性。
当使用 Logger.log 或 console.log 将任何对象打印到 IDE 执行日志时,它们会尝试根据设置的规则将该对象转换为字符串但 Google Apps 脚本文档没有提供传递非字符串时将显示的内容的详细信息。正如您在底部看到的,每个方法都有不同的结果,Logger.log 显示与使用 Date.prototype.toString() 相同的结果,而 console.log 打印日期时间,包括 IATA 时区名称。
JSON.stringify
和Utilities.formatDate
是将 Date 对象格式化为字符串的方法,两者都适用于旧的和新的运行时。使用默认运行时 (V8) 时,您可能还可能使用其他 Date 属性,例如 Date.prototype.toDateString 等。请记住,
Date.prototype.toLocaleString
将返回用户计算机上基于服务器的结果。使用 Google 表格时,请记住,它处理日期值的方式与 JavaScript 不同。使用 getValue / getValues 时,Google Apps 脚本会根据电子表格时区将电子表格日期转换为 JavaScript Date 对象。另一方面, getDisplayValue / getDisplyVALues 以字符串形式返回单元格显示值。您可以使用数字格式选项来设置日期格式,但有某些限制,例如无法显示时区,因为电子表格日期不直接包含时区。
参考文献
Only Google Apps Script product engineers could answer why, but based on the https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date length is not a property of the Date object.
When using
Logger.log
orconsole.log
to print any object to the IDE executions log, they will try to convert that object into a string according to the rules set to do that but Google Apps Script documentation doesn't provide details of what will be displayed when non string is passed. As you can see in the bottom each method have different results, Logger.log shows the same result as useDate.prototype.toString()
whileconsole.log
prints the date time including the IATA timezone name.JSON.stringify
andUtilities.formatDate
are methods for formatting Date object as strings, both work with the old and the new runtime. When using the default runtime (V8) you might also might other Date properties likeDate.prototype.toDateString
among others.Please bear in mind that
Date.prototype.toLocaleString
will return the a result based on the server, on the user computer.When using Google Sheets, please bear in mind that it has a different way to handle dates values than JavaScript. When using getValue / getValues Google Apps Script converts spreadsheet dates into a JavaScript Date objects based on the spreadsheet timezone. By the other hand getDisplayValue / getDisplyVAlues returns cell displayed values as string. You might use the number formatting options to format the date with certain limitations like not being able to display the timezone as the spreadsheet dates don't included it directly.
References