[英]Spreadsheet App converting Date()
Problem: Spreadsheet isn't recognizing an input date as a Date(), so I can't get the .getTime() from it in Apps Script. 问题:电子表格无法将输入日期识别为Date(),因此我无法在Apps脚本中从中获取.getTime()。
Question: Can you see why this is? 问题:您知道为什么吗?
Results: Here is a part of the script I'm running: 结果:这是我正在运行的脚本的一部分:
dates.map(function(inp) {
if (inp != undefined && inp !== ""){
inp = new Date(inp.getTime() - (date.getTimezoneOffset()*60000);
return inp
}
});
where dates
refers to a range: dates = sheet.getRange(2,1, lastR, 1).getValues();
其中
dates
是指范围: dates = sheet.getRange(2,1, lastR, 1).getValues();
Currently this range has a manually inputed value of "2017-05-20" which Spreadsheet automagtically recognizes as the date "Sat May 20 2017 00:00:00 GMT+0200 (CEST)" 当前,此范围的手动输入值为“ 2017-05-20”,电子表格会自动将该日期识别为“ 2017年5月20日星期六00:00:00 GMT + 0200(CEST)”
The error reads as follows: 错误内容如下:
TypeError: Cannot find function getTime in object Sat May 20 2017 00:00:00 GMT+0200 (CEST).
TypeError:在对象Sat May 20 2017 00:00:00 GMT + 0200(CEST)中找不到函数getTime。 (line 86, file "Code") Details Dismiss
(第86行,文件“代码”)详细信息关闭
I have tried a million ways throughout my script to get Spreadsheet to stop autoconverting the input values as dates, so that I can make then at least timestamps or something. 我在整个脚本中尝试了100万种方法,以使Spreadsheet停止将输入值自动转换为日期,以便至少可以创建时间戳或类似内容。 Does any one know why it is doing this?
有谁知道为什么要这么做吗?
Even if you call getValues()
on a one-column range it will still be an array of arrays. 即使您在一个
getValues()
范围内调用getValues()
,它仍将是一个数组数组。
What's confusing here is that GAS tries to be helpful and displays the only value in the one-element array. 令人困惑的是,GAS会尝试提供帮助,并在单元素数组中显示唯一的值。
Extracting the first element from the row should fix this issue. 从行中提取第一个元素应该可以解决此问题。
dates.map(function(row) {
var inp = row[0];
if (inp != undefined && inp !== ""){
inp = new Date(inp.getTime() - (date.getTimezoneOffset()*60000);
return inp
}
});
If you insist on doing everything via Array.prototype.map(), the following code might work. 如果您坚持通过Array.prototype.map()完成所有操作,则以下代码可能会起作用。 I ignored your date manipulation logic, adding 5 days to the date in the cell instead.
我忽略了您的日期操作逻辑,而是在单元格中的日期上增加了5天。 For the values to update, you must assign whatever Array.prototype.map() returns to a variable.
为了更新值,您必须将Array.prototype.map()返回的值分配给变量。 Also, 2 separate checks for empty string and 'undefined' are redundant as JavaScript actually considers them both to be 'false' (Unfortunately, this applies to 0 as well, so be careful)
另外,对空字符串和“ undefined”进行2次单独检查是多余的,因为JavaScript实际上认为它们都为“ false”(不幸的是,这同样适用于0,所以要小心)
function getDates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, 1);
var values = range.getValues();
var newValues = values.map(function(row) {
var newRow = row.map(function(value){
if (value){
value = new Date(value.getTime() + 3600000*24*5) // adding 5 days
return value;
}
});
return newRow;
});
range.setValues(newValues);
}
I ended up taking Robin's recommendation, and did the following, which worked: 我最终接受了罗宾的建议,并做了以下工作,该工作有效:
rows.map(function(r) {
r.day = new Date(r.day.getTime() - (date.getTimezoneOffset()*60000));
r.day = r.day.toISOString().split('T')[0];
return r
})
where date was globally defined as date = new Date()
, "rows" the rows which have data in them, and "day" is the column name (of the first row [0] I had defined earlier in the script) for the column of dates I was trying to change. 其中,date全局定义为
date = new Date()
,“行”其中包含数据的行,“ day”是(我在脚本前面定义的第一行[0]的)列名我试图更改的日期列。
Thanks again! 再次感谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.