Skip to content

Instantly share code, notes, and snippets.

@hiroto3432
Created December 29, 2017 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroto3432/969b7b4372222d2445669d285839069d to your computer and use it in GitHub Desktop.
Save hiroto3432/969b7b4372222d2445669d285839069d to your computer and use it in GitHub Desktop.
function myFunction() {
var APIkey = "APIkey";
var id = "SheetId";
var spreadSheet = SpreadsheetApp.openById(id);
var sheet = spreadSheet.getSheetByName("SheetName");
var row = 3;
var cell = 4;
var search;
while(1){
search = sheet.getRange(row,cell).getValue();
if(search != ''){
var data;
data = getData(search , APIkey);
Logger.log(data);
sheet.getRange(row,cell+1).setValue(data['add']);
sheet.getRange(row,cell+2).setValue(data['lat']);
sheet.getRange(row,cell+3).setValue(data['lot']);
row++;
}
else{
break;
}
}
}
function getData(str ,key){
var data = {add: null ,lat: -1 ,lot: -1};
var url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + str + "&key=" + key;
var response = UrlFetchApp.fetch(url);
var json = JSON.parse(response.getContentText());
Logger.log(json);
try{
var result = json.results[0];
var address = result.formatted_address;
data['add'] = address;
var location = result.geometry.location;
var lat = location.lat;
var lot = location.lng;
data['lat'] = lat;
data['lot'] = lot;
}
catch(e){
Logger.log(e.toString());
}
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment