Filter array by date with google app script

后端 未结 3 1880
南笙
南笙 2021-01-22 03:08

i\'ve a google spreadsheet that is a 2 columns table that has on column A dates (i\'m sure that are dates, and all the date functions works fine on all the column) and on column

3条回答
  •  清歌不尽
    2021-01-22 03:47

    I've created a similar spreadsheet (first column with text, second with dates) and the following script:

    function myFunction() {
      var sheet =  SpreadsheetApp.openById("…");
      var ss    = sheet.getSheetByName("testing");
      var range = ss.getRange(1,1,ss.getLastRow(),ss.getLastColumn());
      var data  = range.getValues();
    
      var filtered = data.filter(function (row) {
        return row[1].getFullYear() === 2016;
      });
    
      Logger.log(filtered);
    }
    

    And it works as long as the second column is of Date type (Format→Number→Date). I get the error you've described if I change the type to Text. If there are no errors in your code (like .getFullYear instead of .getFullYear() as it's a function, not a property), I think the data (not the variable, but the spreadsheet's data) is not of Date type.

提交回复
热议问题