Skip to content

Instantly share code, notes, and snippets.

@gunnaraasen
Forked from mroswell/gist:6b5983c4a2b080feeb30
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save gunnaraasen/9d70f60e768de7a0f86a to your computer and use it in GitHub Desktop.

Select an option

Save gunnaraasen/9d70f60e768de7a0f86a to your computer and use it in GitHub Desktop.
/**
* Looks up an address' probable ZIP code using the Google
* geocoder.
*
* @param {string} address An address string.
* @return The ZIP of first matched address.
* @customfunction
*/
function getZip(address) {
if (address == '') {
Logger.log("Must provide an address");
return;
}
var geocoder = Maps.newGeocoder();
var location;
Utilities.sleep(Math.random() * 4000);
location = geocoder.geocode(address);
// Only change cells if geocoder seems to have gotten a
// valid response.
if (location.status == 'OK') {
zip = extractFromAdress(location["results"][0].address_components, "postal_code");
return zip;
}
};
function getCounty(address) {
if (address == '') {
Logger.log("Must provide an address");
return;
}
var geocoder = Maps.newGeocoder();
Utilities.sleep(Math.random() * 4000);
var location;
location = geocoder.geocode(address);
// Only change cells if geocoder seems to have gotten a
// valid response.
if (location.status == 'OK') {
county = extractFromAdress(location["results"][0].address_components, "administrative_area_level_2");
return county;
}
};
/**
* Looks up and address' probable county.
*
* @param {string} address An address string.
* @return The county of first matched address.
* @customfunction
*/
function getCity(address) {
if (address == '') {
Logger.log("Must provide an address");
return;
}
var geocoder = Maps.newGeocoder();
Utilities.sleep(Math.random() * 4000);
var location;
var city;
location = geocoder.geocode(address);
// Only change cells if geocoder seems to have gotten a
// valid response.
if (location.status == 'OK') {
city = extractFromAdress(location["results"][0].address_components, "locality");
return city;
}
};
function extractFromAdress(components, type){
for (var i=0; i<components.length; i++)
for (var j=0; j<components[i].types.length; j++)
if (components[i].types[j]==type) return components[i].long_name;
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment