I have a Google Apps spreadsheet where one of the columns contains a date-time. This sheet was constructed by merging several sheets together. On some rows this value is a Date (i.e. typeof sheet.getRange(i,2).getValue() == "object" with the methods for a date). Other time this cell is a number (i.e. typeof sheet.getRange(i,2).getValue() == "number") but the cell formatting has the cell displayed as if it is a date.
For example, for a date of Saturday, May 16, 2015 at 9:30 AM
If typeof firstDate == "number" then the value is 42140.395833333336
if typeof firstDate == "object" then the "valueOf" is 1431783000000
The purpose of this function is to add a border whenever the day changes so that each day appears on a separate group of cells. Currently this function is adding additional borders wherever the type of data changes between date and numeric.
/**
* 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);
}
}
};
firstDate
, you are setting it to a date type. This may not have anything to do with your problem, but you can leave it undefinedvar firstDate;
Don't make an assignment. That allows for the first assignment to dictate what type the variable will be. – Alan WellsDate()
function. – Alan Wells