Last active
March 26, 2021 11:42
-
-
Save mroswell/6b5983c4a2b080feeb30 to your computer and use it in GitHub Desktop.
getZip() for google sheet
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
/** | |
* Looks up an address' probable ZIP code using the Google | |
* geocoder. | |
* | |
* @param {string} address An address string (May consist of multiple cells, such as street, city, and state). | |
* @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; | |
} | |
}; | |
/** | |
* Looks up an address' probable County using the Google | |
* geocoder. | |
* | |
* @param {string} address An address string (May consist of multiple cells, such as street, city, and state). | |
* @return The County of first matched address. | |
* @customfunction | |
*/ | |
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 an address' probable City using the Google geocoder. | |
* | |
* @param {string} address An address string (May consist of multiple cells, such as street, state, and zip). | |
* @return The City 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
Looks like custom functions in the current version of Google Sheets require a JsDoc tag to be picked up by autocomplete. Here's an example of a JsDoc comment that should precede the function.