0

I'm trying to make a function in order to find & match the date of day in a array. But for a weird reason, if two dates are matching, my function never find the match. So..., if I change my date by an other string (ex : "foo" match with "foo"), it's work.

Here my code, and then the log return. Have you an idea why my script can't match two equal dates ?

Thank you !

function hideBeforeToday(values, value)
{
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName(roadmapSS))
   var range = sheet.getRange(4, 11, 1, sheet.getLastColumn())
   var values = range.getValues();
   Logger.log(values);
   var value = moment().set({hour:0,minute:0,second:0,millisecond:0}).utc().toDate();
   Logger.log("date", value);

   for(var i = 0; i < values.length; i++) {
     if(values[i].indexOf(value) > -1) {
        Logger.log("data found");
     } 
   }  
}

[18-01-11 13:49:33:065 CET] Logger.log([arrayValues, [[[Wed Jan 10 00:00:00 GMT+01:00 2018, Thu Jan 11 00:00:00 GMT+01:00 2018]]]]) [0 secondes]
[18-01-11 13:49:33:067 CET] Logger.log([dateOfDay, [Thu Jan 11 00:00:00 GMT+01:00 2018]]) [0 secondes]
[18-01-11 13:49:33:070 CET] Script exécuté [durée totale d'exécution : 0,535 secondes]
christophebazin
  • 200
  • 2
  • 4
  • 14

1 Answers1

1

Look at this example:

var date1 = new Date("Thu Jan 11 00:00:00 GMT+01:00 2018");
var dates = [date1];


console.log(date1); // prints 2018-01-10T23:00:00.000Z
console.log(dates.indexOf(date1)); // prints 0

While this:

var date1 = new Date("Thu Jan 11 00:00:00 GMT+01:00 2018");
var dates = [new Date("Thu Jan 11 00:00:00 GMT+01:00 2018")];


console.log(date1); // prints 2018-01-10T23:00:00.000Z
console.log(dates.indexOf(date1)); // prints -1

I don't know what data you have in the spreadsheet, if it is a string or a Date object, but try converting both to the same type before comparing. Check their types and perhaps use the toISOString() function?

bla
  • 301
  • 2
  • 11