Skip to content

Instantly share code, notes, and snippets.

@jadeallencook
Last active March 8, 2018 19:23
Show Gist options
  • Save jadeallencook/838e993dfb1894fd0d5aecdaa93eb9cf to your computer and use it in GitHub Desktop.
Save jadeallencook/838e993dfb1894fd0d5aecdaa93eb9cf to your computer and use it in GitHub Desktop.
Google Script that returns zipcode for address.
function getZip(address) {
if (address == '') {
return 'ERROR: No address was provided';
} else {
var location = Maps.newGeocoder().geocoder.geocode(address);
if (location.status == 'OK') return extractFromAddress(location.results[0].address_components, 'postal_code');
else return 'ERROR: Something went wrong with Geocoder';
}
}
function extractFromAddress(components, type) {
for (var x = 0; x < components.length; x++) {
for (var y = 0; y < components[x].types.length; y++) {
if (components[x].types[y] == type) return components[x].long_name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment