1

I have an array that I would like to filter by using only dates AFTER a certain period (the 4th column in the array). The startDate variable is the date within a cell that I would like to filter the array by. I also want to filter out the blank cells in the 4th column. I've tried a few different things, but the function is returning the entire array with JUST the blank columns filtered. Here's the latest Code I've tried.

function invPayable(){
   var overview = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Overview");

   var paymentLog = SpreadsheetApp.openById("1KkR4jE1c00WuQpPrexW8f9BIq5vxl0fWaUGxbeYERsE").getSheetByName("Payment Log");
   var paymentArr = paymentLog.getRange(3, 1,paymentLog.getLastRow(),paymentLog.getLastColumn()).getValues();
   var startDate = new Date(overview.getRange(3, 1).getValue());

   var pay1 = paymentArr.map(function(r){return [r[0],r[1],r[2],r[3]]})
      .filter(function(item){
      if(item[3]!=""){return true}
      if(item[3]>=startDate){return true}
      else{return false}
   });

 //execute
 overview.getRange(2, 18,pay1.length,4).setValues(pay1);
 overview.getRange(6,1).setValue(startDate);
}//Added by editor

1 Answer 1

3

Issue:

if(item[3]!=""){return true}
if(item[3]>=startDate){return true}

Once the first if condition is satisfied(i.e., column 4 is not empty), the function ends and returns true. The second if condition is never checked. Therefore,

the function is returning the entire array with JUST the blank columns filtered.

Solution:

Check both conditions before ending the function.

Snippet:

if(item[3]!="" && item[3]>=startDate ){return true}
Sign up to request clarification or add additional context in comments.

1 Comment

Hello! this worked. Can't believe I missed this. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.