Last active
March 8, 2018 19:23
-
-
Save jadeallencook/838e993dfb1894fd0d5aecdaa93eb9cf to your computer and use it in GitHub Desktop.
Google Script that returns zipcode for address.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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