Skip to content

Instantly share code, notes, and snippets.

@bencooper222
Last active Apr 4, 2017
Embed
What would you like to do?
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