[英]Compare dates in Google Sheets using Apps Script
我有一个Google Apps电子表格,其中一列包含日期时间。 通过将几张纸合并在一起来构造该纸。 在某些行上,该值为日期(即typeof sheet.getRange(i,2).getValue()==“ object”以及用于日期的方法)。 其他时间,该单元格是数字(即typeof sheet.getRange(i,2).getValue()==“ number”),但是单元格格式显示的单元格好像是日期。
例如,对于2015年5月16日(星期六)上午9:30的日期
如果typeof firstDate ==“ number”,则值为42140.395833333336
如果typeof firstDate ==“ object”,则“ valueOf”为1431783000000
此功能的目的是在每天更改时添加边框,以便每天出现在单独的单元格组上。 当前,此功能会在数据类型在日期和数字之间变化的任何地方添加其他边框。
/**
* A special function that runs when the spreadsheet is open, used to add a
* custom menu to the spreadsheet.
*/
function onOpen() {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Add Borders', functionName: 'Add_Borders'}
];
spreadsheet.addMenu('Scheduling', menuItems);
}
/**
* Retrieves all the rows in the active spreadsheet that contain data and logs the
* values for each row.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function Add_Borders() {
var sheet = SpreadsheetApp.getActiveSheet();
var numRows = sheet.getLastRow();
var cols = sheet.getLastColumn();
var firstDate=new Date();
var secondDate = new Date();
//Logger.log(sheet.getParent().getName() + " ! " + sheet.getName());
for (var i = 2; i <= numRows - 1; i++) {
firstDate = sheet.getRange(i,2).getValue();
Logger.log("row " + i + " Date type is " + typeof firstDate)
if (typeof firstDate == "number"){
Logger.Log ("row " + i + " number value = " + firstDate)
}
else {
Logger.Log ("row " + i + " date value = " + firstDate.valueOf())
Logger.Log ("row " + i + " numeric value = " + firstDate)
}
firstDate = (typeof firstDate == "number") ? new Date(firstDate * 86400) : firstDate
secondDate = sheet.getRange(i+1,2).getValue();
secondDate = (typeof secondDate == "number") ? new Date(secondDate * 86400) : secondDate
// Logger.log("row " + i + " first date = " + firstDate + " firstDate.getDate() = " + firstDate.getDate() + "; firstDate.getMonth() = " + firstDate.getMonth())
// Logger.log("row " + i + " second Date = " + secondDate + " secondDate.getDate() = " + secondDate.getDate() + "; secondDate.getMonth() = " + secondDate.getMonth())
if (firstDate.getDate() != secondDate.getDate() || firstDate.getMonth() != secondDate.getMonth()){
sheet.getRange(i, 1, 1, cols).setBorder(null, null, true, null, null, null);
}
else {
sheet.getRange(i, 1, 1, cols).setBorder(null, null, false, null, null, null);
}
}
};
经过一番试验后,我终于了解了以下定义:对于数字,值是自1899年12月31日以来在当地时区的天数; 对于日期,.getTime()返回自1970年1月1日UTC以来的毫秒数。
因此,以下函数强制了该值:
function FixDate(dte){
// If dte is a number then it contains a date in the form of the number of days since
// 00:00 on 12/31/1899 in the current time zone
if (typeof dte == "number"){
return dte
}
// If dte is a date object then the getTime property contains the date as the number of
// milliseconds since 00:00 on January 1, 1970 in GMT. This value is converted to be
// compatible with the other format
return (dte.getTime() / 86400000) + 25569 - dte.getTimezoneOffset() / (60 * 24)
}
然后使用Math.floor(FixDate())查找自1899年12月31日以来的日期。 我希望找到某种自动日期强制例程,例如工作表用来将数字格式化为日期的例程,但我找不到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.