title: Google Apps Script - Still struggling with looping through rows
body: <p>I'm still struggling with understanding loops. </p>
  
  <p>I have a script that calculates the distance in kilometers between two addresses. At this point it only calculates the distance at row 12 and put the result in column C, row 12. </p>
  
  <pre><code>Start           End           Total km's      
  address one     address two   16,283           
  </code></pre>
  
  <p>Code (found on the web):</p>
  
  <pre><code>function GOOGLEMAPS() {
  
    var start_address = SpreadsheetApp.getActiveSheet().getRange('A12').getValue();
    var end_address = SpreadsheetApp.getActiveSheet().getRange('B12').getValue();
  
    var mapObj = Maps.newDirectionFinder();
    mapObj.setOrigin(start_address);
    mapObj.setDestination(end_address);
    var directions = mapObj.getDirections();
  
    var getTheLeg = directions["routes"][0]["legs"][0];
  
    var meters = getTheLeg["distance"]["value"];
  
    var keerduizend = meters / 1000;
  
    SpreadsheetApp.getActiveSheet().getRange('C12').setValue(keerduizend);
  
  }
  </code></pre>
  
  <p>The final result should be that it can calculate all distances that are put in the sheet: </p>
  
  <pre><code>A               B             C
  Start           End           Total km's      
  address one     address two   16,283    
  address two     address one   16,283    
  address three   address four  16,283    
  address five    address six   16,283    
  
                  Total        SUM(C12:C15)
  </code></pre>
  
  <p>It should not matter if there are 4 addresses or 40 and users should be able to add rows (between the last row with an address and the row with Total. As I'm still struggling with understanding loops I don't see how to achieve this. </p>
  
  <p>Can someone explain to me how to achieve this? </p>
  
  <p><strong>Edit</strong></p>
  
  <p>Thanks to Diego the first part of the script works: </p>
  
  <pre><code>function GOOGLEMAPS() {
  
    var sheet = SpreadsheetApp.getActiveSheet();
    var start_addresses = sheet.getRange('B12:B26').getValues(); // [["address one"], ["address two"], ["address three"], ["address five"]]
    var end_addresses = sheet.getRange('D12:D26').getValues(); // [["address two"], ["address one"], ["address four"], ["address six"]]
  
    var distances = []; // This will store all of the distances
  
      for (var i = 0; i &lt; start_addresses.length; i++) {
      var mapObj = Maps.newDirectionFinder();
      if (start_addresses[i][0] != "" &amp;&amp; end_addresses[i][0] != "") {
        mapObj.setOrigin(start_addresses[i][0]);
        mapObj.setDestination(end_addresses[i][0]);
        var directions = mapObj.getDirections();
  
        var getTheLeg = directions["routes"][0]["legs"][0];
  
        var meters = getTheLeg["distance"]["value"];
  
        var keerduizend = meters / 1000;
  
        distances.push([keerduizend]);
      }
    }
  
    sheet.getRange('E12:E26').setValues(distances);
  
  }
  
  </code></pre>
  
  <p>Still get's an error </p>
  
  <pre><code>The number of rows in the data does not match the number of rows in the range. The data has 3, but the range has 15. (line 30, file 'Code')
  </code></pre>
  
  <p>But I will try to figure this one out by myself first. </p>