mercredi 30 décembre 2015

Count how many times a conditional is true inside for loop with javascript

I'm banging my head against the wall with this, and I know it can't be that difficult. I have a for loop that runs over a range of cell values in a Google Apps script I'm trying to write, and each time it loops, it checks to see if a certain condition is true. If it's true, for the time being, it's logging the results to Logger.log().

I need the total number of results that show up in Logger.log() (in this case, it would be 10) and the best way I was able to think of doing that would be to count up by 1 each time that conditional is true in the for loop. It should be very simple to get a numerical value of how many results, but I'm inexperienced and have been searching for hours for anything that I think might be able to help me and I just can't figure it out. Here's the code I'm using.

var currentsheet = SpreadsheetApp.getActiveSheet();
var values = currentsheet.getRange("A28:E").getValues();

for (i=0; i<values.length; i++) {
if (values[i][3] === "Section 179 Depreciation") {
  var date = values[i][0];
  var amount = values[i][1];
  var desc = values[i][2];
  var expType = values[i][3];
  var notes = values[i][4];

 }
}

If someone could fill me in on what I'm missing, I would appreciate it very much!

Aucun commentaire:

Enregistrer un commentaire