-
-
Save gunnaraasen/9d70f60e768de7a0f86a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * 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