Skip to content

Instantly share code, notes, and snippets.

@bencooper222
Last active April 4, 2017 22:14
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 bencooper222/9aaf856847801581d6ab6b4a6eb8f103 to your computer and use it in GitHub Desktop.
Save bencooper222/9aaf856847801581d6ab6b4a6eb8f103 to your computer and use it in GitHub Desktop.
Google Sheets Geocoding Custom Function
// returns latlng from Google API
function GEOCODE(input){
var data = JSON.parse(queryGoogle(input));
var location = data.results[0].geometry.location;
return location.lat + ", " + location.lng;
}
function queryGoogle(address){
var options =
{
"method" : "GET",
"followRedirects" : true,
"muteHttpExceptions": true
};
// wouldn't it be great if we had a real getRequest method in Google Apps Script
// you'll have to get your own API key from https://developers.google.com/maps/documentation/geocoding/start
var queryString = "?address="+address+"&key="+"YOUR API KEY";
var request = UrlFetchApp.fetch("https://maps.googleapis.com/maps/api/geocode/json"+queryString, options)
return request.getContentText();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment