Search code examples
google-sheetsgoogle-apps-script

Comparing Dates between 2 Sheets, Not Evaluating as true when conditions are met


function trackProgress() {
  var out = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
  out.getRange("dataRange").sort({ column: 1, ascending: true });

  var progress = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Progress");
  progress.getRange("Plog").sort({ column: 1, ascending: true });
  var dRows = out.getLastRow();
  var pRows = progress.getLastRow();
  var pdate = progress.getRange("Pdate").getValues();
  var pCRpro = progress.getRange("CRpro").getValues();
  var pMpro = progress.getRange("Mpro").getValues();
  var outUser = out.getRange("UID").getValues();
  var outWeight = out.getRange("Rweight").getValues();
  var outDate = out.getRange("Date").getValues();
  var data;

  //[["Date", "CR", "CR Proj", "M", "M Proj"]]


  for (var i = 0; i <= dRows; i++) {
    for (var g = 0; g <= pRows; g++) {
Logger.log(outDate[i]+" "+pdate[g])

      if (outDate[i] == pdate[g]) {
        Logger.log("date match")
        if (outUser[i] == "Munchkin") {
          data = data.concat([[outDate[i], "", pCRpro[i], outWeight[i], pMpro[i]]]);
        }
        if (outUser[i] == "CR") {
          data = data.concat([[outDate[i], outWeight[i], pCRpro[i], pMpro[i]]]);
        }
      }
  
    }

    }
  

  Logger.log(data)


}

Ex Log

As shown in the Ex log there are several instances where pDate and outDate are equal, any help would be appreciated.

when the two dates match I want to concat the row of info a new array


Solution

  •  var pdate = progress.getRange("Pdate").getValues();
     var outDate = out.getRange("Date").getValues();
         if (outDate[i] == pdate[g]) {
    

    There are two main issues. Firstly, pdate and outDate are 2D arrays and should not be compared the way shown in the code.

    When you reference pdate[g], you get a subarray of values. In the event the named range Pdate has three columns, the subarray will look like [1, 2, 3]. If there is just one column, it will look like [1]. To compare a value within the subarray to another value, you need to reference a column within the array, as in pdate[g][0]. Assuming that both named ranges have one column, you can compare the values like this:

        if (outDate[i][0] == pdate[g][0]) { // note: this will never be true when the arrays contain objects such as Dates
    

    Secondly, the logs you shared suggest that the two named ranges contain dates. In JavaScript, dates are handled as Date objects. Objects are different from primitives and cannot be compared the way shown in the code.

    To find if two Date objects refer to the same moment in time, use Date.getTime(), like this:

        if (value1.getTime && value2.getTime && value1.getTime() === value2.getTime()) {
    

    But in the Google Sheets context, if you are only comparing dates for equality, the easiest way to work with them would be to retrieve them as text strings in the format shown in the spreadsheet. Further, you can use Array.flat() to get 1D arrays, like this:

     const pdate = progress.getRange("Pdate").getDisplayValues().flat();
     const outDate = out.getRange("Date").getDisplayValues().flat();
       if (outDate[i] === pdate[g]) {
    

    Note that this requires that the two named ranges use the same date format.