Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick'n'dirty reversion of https://github.com/nuket/google-sheets-geocoding-macro/ for extracting country names from a list of free-text strings
function extractCountries() {
var sheet = SpreadsheetApp.getActiveSheet();
var cells = sheet.getActiveRange();
if (cells.getNumColumns() != 2) {
Logger.log("Must select the Location, Country.");
return;
}
var addressColumn = 1;
var addressRow;
var countryColumn = addressColumn + 1;
var geocoder = Maps.newGeocoder();
var location;
for (addressRow = 1; addressRow <= cells.getNumRows(); ++addressRow) {
address = cells.getCell(addressRow, addressColumn).getValue();
if (address && address != "") {
location = geocoder.geocode(address);
if (location.status == 'OK') {
var components = location["results"][0]["address_components"];
var country = components[components.length-1]["long_name"];
cells.getCell(addressRow, countryColumn).setValue(country);
}
}
}
};
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name: "Extract countries",
functionName: "extractCountries"
}];
sheet.addMenu("Macros", entries);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.