google-sheetsgoogle-apps-script

Filter data by date with Google Apps Script is not working


Here is my sample data in column A(KPI ID), B(*Business Unit Name), C(Payment Terms) and D(Transaction Date) in my Google Sheet in sheet - "main", which is my first sheet:

KPI ID  *Business Unit Name Payment Terms   Transaction Date
b10001  MyCo    30 days net 1/13/2025
b10002  MyCo    30 days net 1/13/2025
b10003  MyCo    30 days net 1/13/2025
b10004  MyCo    30 days net 1/13/2025
b10005  MyCo    60 days net 1/22/2025
b10006  MyCo    60 days net 1/22/2025
b10007  MyCo    30 days net 1/22/2025
b10008  MyCo    60 days net 1/22/2025
b10009  MyCo    30 days net 1/22/2025
b10010  MyCo    30 days net 1/22/2025

I print out Transaction Date to ensure data type is Date.

8:42:49 AM  Notice  Execution started
8:42:50 AM  Info    Mon Jan 13 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Mon Jan 13 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Mon Jan 13 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Mon Jan 13 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Mon Jan 13 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Wed Jan 22 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Wed Jan 22 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Wed Jan 22 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Wed Jan 22 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Wed Jan 22 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Wed Jan 22 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Wed Jan 22 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Wed Jan 22 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Wed Jan 22 00:00:00 GMT+08:00 2025
8:42:50 AM  Info    Wed Jan 22 00:00:00 GMT+08:00 2025

I only want data on 13th Jan 2025, so I try following method to filter data:

  const target_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetById(0)

  const t_header = target_sheet.getRange(1, 1, 1, 81).getValues()
  let t_data = target_sheet.getRange(2, 1, target_sheet.getDataRange().getLastRow() - 1, 81).getValues()

  const t_end_date = new Date(2025, 0, 13)

  t_data.filter(row => row.slice(3, 4)[0] == t_end_date)
  .forEach(row => Logger.log(row))

Execution log doesn't print out anything. I tried the same method in another sample data and it worked. Please advise where goes wrong.


Solution

  • Filter with Date Object

    For starters I want to share that your code issue starts with the conditional statement. Objects cannot be compared to the primitive data type. What it tests is not if they have the same value but rather if they are the same object.

    Solution

    The solution is to convert your object to a primitive data type where comparative syntax works.

    I.E

    
    function myFunction() {
      const target_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetById(0)
    
      const t_header = target_sheet.getRange(1, 1, 1, 81).getValues()
      let t_data = target_sheet.getRange(2, 1, target_sheet.getDataRange().getLastRow() - 1, 81).getValues();
      const t_end_date = new Date(2025, 0, 13)
    
      var test = t_data.filter(row => row[4].toString() == t_end_date.toString());
      console.log(test)
      }
    

    With the example above I just changed the date objects into a string where it can be tested. What I have sighted is the most simple one to use there are other way make it happen feel free to check the references sited below.

    References:

    Comparing Date Objects