当我似乎无法使用 GAS 将它们转换为通用格式时如何比较时间戳?
How to compare timeStamp whe i can't seem to bring them to a common format using GAS?
因此,在提交表单时,我得到了一个时间戳,如下所示:
"values":["2/18/2022 14:11:25"]
然后我需要将它与电子表格中的进行比较,以便我可以为相邻的列设置一个数字。
然后,我正在使用下面的代码,但我在```Utilities.formatDate()````
上遇到错误
代码:
function onSubmit(e) {
Logger.log("%s", JSON.stringify(e));
const timeStamp = e.values[0]
const formRespSheet = e.source.getSheetByName('Form Responses 1')
var maxNumber = Math.max.apply(null, formRespSheet.getRange(2, 14, formRespSheet.getLastRow(), 1).getValues());
maxNumber = maxNumber + 1
Utilities.sleep(1000);//Tried it
const allTimeStamps = formRespSheet.getRange(2, 1, formRespSheet.getLastRow(), 1).getValues();
for (let a = 0; a < allTimeStamps.length; a++) {
let sheetTimeStamp = allTimeStamps[a]
sheetTimeStamp = Utilities.formatDate(sheetTimeStamp, Session.getTimeZone(), "MM/dd/yyyy HH:mm:ss")
if (sheetTimeStamp.valueOf() == timeStamp.valueOf()) {
const row = a + 1
formRespSheet.getRange(row, 14).setValue(maxNumber)
}
}
}
错误说:
The parameters (number[],String,String) don't match the method signature for Utilities.formatDate.
感谢您的帮助。
sheetTimeStamp = Utilities.formatDate(sheetTimeStamp, Session.getTimeZone(), "MM/dd/yyyy HH:mm:ss")
变为:
sheetTimeStamp = Utilities.formatDate(new Date(sheetTimeStamp), Session.getTimeZone(), "MM/dd/yyyy HH:mm:ss")
它应该有效
Google Spreadsheet 有一个 Date 对象,非常适合比较。我总是,并且不明白为什么每个人都不,将一个或多个单元格的数字格式设置为日期。这样我就可以比较日期、减去日期等。现在,我知道时间间隔,但一个简单的函数可以缓解这个问题,我有一个电子表格,其中有两个具有相同日期但时间不同的日期。所以我可以比较它们是否是同一天。
function test() {
try {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var range = sheet.getRange("B1:B2");
var values = range.getValues();
function removeTime( value ) {
var temp = value[0];
value[0] = new Date(temp.getFullYear(),temp.getMonth(),temp.getDate());
};
console.log( new Date(values[0][0]) );
console.log( new Date(values[1][0]) );
console.log(values[0][0].valueOf() === values[1][0].valueOf());
values.forEach( removeTime );
console.log( new Date(values[0][0]) );
console.log( new Date(values[1][0]) );
console.log(values[0][0].valueOf() === values[1][0].valueOf());
}
catch(err) {
console.log(err);
}
}
9:15:47 AM Notice Execution started
9:15:47 AM Info Fri Feb 18 2022 11:53:45 GMT-0500 (Eastern Standard Time)
9:15:47 AM Info Fri Feb 18 2022 11:54:16 GMT-0500 (Eastern Standard Time)
9:15:47 AM Info false
9:15:47 AM Info Fri Feb 18 2022 00:00:00 GMT-0500 (Eastern Standard Time)
9:15:47 AM Info Fri Feb 18 2022 00:00:00 GMT-0500 (Eastern Standard Time)
9:15:47 AM Info true
9:15:48 AM Notice Execution completed
因此,在提交表单时,我得到了一个时间戳,如下所示:
"values":["2/18/2022 14:11:25"]
然后我需要将它与电子表格中的进行比较,以便我可以为相邻的列设置一个数字。 然后,我正在使用下面的代码,但我在```Utilities.formatDate()````
上遇到错误代码:
function onSubmit(e) {
Logger.log("%s", JSON.stringify(e));
const timeStamp = e.values[0]
const formRespSheet = e.source.getSheetByName('Form Responses 1')
var maxNumber = Math.max.apply(null, formRespSheet.getRange(2, 14, formRespSheet.getLastRow(), 1).getValues());
maxNumber = maxNumber + 1
Utilities.sleep(1000);//Tried it
const allTimeStamps = formRespSheet.getRange(2, 1, formRespSheet.getLastRow(), 1).getValues();
for (let a = 0; a < allTimeStamps.length; a++) {
let sheetTimeStamp = allTimeStamps[a]
sheetTimeStamp = Utilities.formatDate(sheetTimeStamp, Session.getTimeZone(), "MM/dd/yyyy HH:mm:ss")
if (sheetTimeStamp.valueOf() == timeStamp.valueOf()) {
const row = a + 1
formRespSheet.getRange(row, 14).setValue(maxNumber)
}
}
}
错误说:
The parameters (number[],String,String) don't match the method signature for Utilities.formatDate.
感谢您的帮助。
sheetTimeStamp = Utilities.formatDate(sheetTimeStamp, Session.getTimeZone(), "MM/dd/yyyy HH:mm:ss")
变为:
sheetTimeStamp = Utilities.formatDate(new Date(sheetTimeStamp), Session.getTimeZone(), "MM/dd/yyyy HH:mm:ss")
它应该有效
Google Spreadsheet 有一个 Date 对象,非常适合比较。我总是,并且不明白为什么每个人都不,将一个或多个单元格的数字格式设置为日期。这样我就可以比较日期、减去日期等。现在,我知道时间间隔,但一个简单的函数可以缓解这个问题,我有一个电子表格,其中有两个具有相同日期但时间不同的日期。所以我可以比较它们是否是同一天。
function test() {
try {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var range = sheet.getRange("B1:B2");
var values = range.getValues();
function removeTime( value ) {
var temp = value[0];
value[0] = new Date(temp.getFullYear(),temp.getMonth(),temp.getDate());
};
console.log( new Date(values[0][0]) );
console.log( new Date(values[1][0]) );
console.log(values[0][0].valueOf() === values[1][0].valueOf());
values.forEach( removeTime );
console.log( new Date(values[0][0]) );
console.log( new Date(values[1][0]) );
console.log(values[0][0].valueOf() === values[1][0].valueOf());
}
catch(err) {
console.log(err);
}
}
9:15:47 AM Notice Execution started
9:15:47 AM Info Fri Feb 18 2022 11:53:45 GMT-0500 (Eastern Standard Time)
9:15:47 AM Info Fri Feb 18 2022 11:54:16 GMT-0500 (Eastern Standard Time)
9:15:47 AM Info false
9:15:47 AM Info Fri Feb 18 2022 00:00:00 GMT-0500 (Eastern Standard Time)
9:15:47 AM Info Fri Feb 18 2022 00:00:00 GMT-0500 (Eastern Standard Time)
9:15:47 AM Info true
9:15:48 AM Notice Execution completed