snoop31
snoop31

Reputation: 91

"for loop" does not find rows of a given condition

The code for driver "1" summarize data, for driver "2" the code continue summarize data (data of 1 driver + data of 2 driver), but i need for each driver different results. What's the problem?

Thank you, for your time!

    function driverBort() {
      var srs = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('List1');
      var col = srs.getRange('C:M').getValues();
      var data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('IDs');
      var main = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Table');

      var type = data.getRange('A38').getValue();
      var tax =  data.getRange('D38').getValue();
      var drivers = main.getRange('A2:B10').getValues();

      var sum = 0;  


    for (var n = 0; n < col.length; n++) {
      for (var i = 0; i < drivers.length; i++) {
        if (col[n][0] == drivers[i][0]) {
          if (col[n][10] == type) {
            sum += col[n][7] * tax
            main.getRange(i+2,2).setValue(sum)
          }
        }
      }
    }
    }

Upvotes: 2

Views: 89

Answers (1)

dev
dev

Reputation: 3999

I haven't tried your code but I think the issues is that you're using .getValues for a singe cell value, hence it is returning a 3D array. You then trying to compare a singular value to an array with this line col[n][10] == type) . Try changing your type and tax declaration lines to:

var type = data.getRange('A38').getValue();
var tax =  data.getRange('D38').getValue();

Note: getValue without the s.

Upvotes: 3

Related Questions