onit
onit

Reputation: 2372

How to push the latest data entered based on conditions on Google AppScript?

Currently, the code below checks the if the data meets two criteria and then it's pushed into an array, but the need now is to get the latest data that meet those criteria. The date's index within the dataset is [19] (20th column).

How would I go about tweaking it?

for (var i = 0; i < values.length; i++) {
         if (values[i][0] == productCode && values[i][2] == productVersao) {
        data.push(values[i]);
    } 
  }

Thanks a lot in advance!

Upvotes: 0

Views: 38

Answers (1)

Cooper
Cooper

Reputation: 64062

function themostrecent(values) {
  values.sort(function(a,b){
    var A=new Date(a[19]).valueOf();
    var B=new Date(b[19]).valueOf();
    return B-A;
  });
  for (var i = 0; i < values.length; i++) {
    if (values[i][0] == productCode && values[i][2] == productVersao) {
      data.push(values[i]);
      break;
    } 
  }
  return data;
}

Upvotes: 2

Related Questions