Find the closer item in the list from address

function closerItem() {
  
  var id = "1JA9utI4cYiwHcIWw1uTFE4UHJjyuoTs1LlmgVVgxnXY";
  var name = "Empreendimentos";
  
  var TRIX = SpreadsheetApp.openById(id).getSheetByName(name);
  
  var rows = TRIX.getRange("A2").getValue();

  var dados_raw = new Array(new Array());
  dados_raw = TRIX.getRange("C4:D68").getValues();

  var data = new Array(new Array());
  
  var lat1;
  var lon1;
  var lat2;
  var lon2;
  
  var i;
  var j;
  
  for(i = 0; i <= rows - 1; i++) {
    
    var row = 1;
    var minor = 0;
    
    for (j = 0; j <= rows - 1; j++) {
      
      Logger.log(
        dados_raw[i][0] + " - " +
        dados_raw[i][1] + " - " +
        dados_raw[j][0] + " - " +
        dados_raw[j][1]);
      
      lat1 = dados_raw[i][0];
      lon1 = dados_raw[i][1];
      lat2 = dados_raw[j][0];
      lon2 = dados_raw[j][1];
      
      var dist = distance(lat1, lon1, lat2, lon2);
      
      // data[cliente][outros]
      if(dist != 0) {
                
        if(minor == 0 || dist < minor) { 
          
          minor = dist;
          row = j;
        }
      } else {
       
        //
           
      }
    }
    
    TRIX.getRange("F" + (i + 4)).setValue(minor);
    TRIX.getRange("G" + (i + 4)).setValue(row + 4);
  }
}

Get Latitude and Longitude from Address


function latlong() {
  
  var id = "id";
  var name = "name_sheet";

  var TRIX = SpreadsheetApp.openById(id).getSheetByName(name);
  
  var num_rows = "A2";
  var column_lat = "C";
  var column_long = "D";
 
  
  var i = 0;
  var j = TRIX.getRange(num_rows).getValue();
  
  for (i ; i <= j; i++) {
    
    var value = TRIX.getRange("B" + (i + 4)).getValue();
    var response = Maps.newGeocoder().geocode(value + ", São Paulo, Brazil");
    var result = response.results[0];
    
    Logger.log('%s: %s, %s', result.formatted_address, result.geometry.location.lat, result.geometry.location.lng);
    // TRIX.getRange("C" + (i + 4)).setValue(result.formatted_address);
    TRIX.getRange(column_lat + (i + 4)).setValue(result.geometry.location.lat);
    TRIX.getRange(column_long + (i + 4)).setValue(result.geometry.location.lng);
        
  }  
}

Calculate distance of two latitude longitude points

function distance(lat1, lon1, lat2, lon2) {

  var R = 6371; // km
  
  var dLat = toRad(lat2 - lat1);
  var dLon = toRad(lon2 - lon1);
  
  var lat_1 = toRad(lat1);
  var lat_2 = toRad(lat2);
   
  var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat_1) * Math.cos(lat_2); 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c;
  
  return d;

}

function toRad(x) {
   return x * Math.PI / 180;
}