Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compare dates with a spreadsheet using Google Apps Script?

I have a spreadsheet that I want to conditionally format according to dates. If a date is 7 days away, I want the cell to turn red, if a date is 14 days away, I want it to turn green, etc.

The first step to doing this, I figured, would be to create a simple conditional format script, where if a date is today, it would be highlighted. But my code doesn't work :(

function formatting() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); // get the sheet
  var columnF = sheet.getRange(2, 6, sheet.getLastRow()-1, 1); // get all the rows
  var fValues = columnF.getValues(); // get the values
  var today = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy"); // get today's date
  for (var i = 0; i < fValues.length; i++) { // repeat loop
    if (fValues[i][0] == today) { // if the date in the cell is today's date...
      sheet.getRange(i + 2, 6, 1, 1).setBackgroundColor('green'); // ...turn that cell green
    }
  }
}

What am I doing wrong?

Thanks!

like image 667
user1786546 Avatar asked Jan 16 '13 02:01

user1786546


2 Answers

You should not convert your date to a string, you can parse the date in days, month and years like in Srik's answer, or you can also compare dates using their native values like below, it becomes easier to calculate other dates... I created a few variables to simplify the math :

day = the number of milliseconds in a day

today = today's date at 00:00 (in days)

function formatting() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); // get the sheet
  var columnF = sheet.getRange(2, 6, sheet.getLastRow()-1, 1).setBackgroundColor('white'); // get all the rows and clear colors
  var fValues = columnF.getValues(); // get the values
  var day = 24*3600*1000
  var today = parseInt((new Date().setHours(0,0,0,0))/day); // get date today
  for (var i = 0; i < fValues.length; i++) { // repeat loop
    var dataday = parseInt(fValues[i][0].getTime()/day)
    Logger.log(dataday+' = '+today)
    if (dataday == today-7) {sheet.getRange(i + 2, 6, 1, 1).setBackgroundColor('red')}
    else if (dataday == today-14){sheet.getRange(i + 2, 6, 1, 1).setBackgroundColor('green')}
    else if (dataday == today-21){sheet.getRange(i + 2, 6, 1, 1).setBackgroundColor('blue')}
  }
}

Test sheet available here

like image 186
Serge insas Avatar answered Oct 06 '22 23:10

Serge insas


You cannot compare Date object directly. You need to compare by date, month and year. Try something like

function formatting() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); // get the sheet
  var columnF = sheet.getRange(2, 6, sheet.getLastRow()-1, 1); // get all the rows
  var fValues = columnF.getValues(); // get the values
  var today = new Date(); 
  for (var i = 0; i < fValues.length; i++) { // repeat loop
    var fDate = new Date(fValues[i][0]);
    if (fDate.getDate() == today.getDate() && 
        fDate.getMonth() == today.getMonth() && 
        fDate.getFullYear() == today.getFullYear()) { // if the date in the cell is today's date...
      sheet.getRange(i + 2, 6, 1, 1).setBackgroundColor('green'); // ...turn that cell green
    }
  }
}
like image 35
Srik Avatar answered Oct 07 '22 00:10

Srik