Created
December 13, 2016 01:11
-
-
Save matiasgarciaisaia/5074fad41090da79a21acd8fa1480e35 to your computer and use it in GitHub Desktop.
Quick'n'dirty reversion of https://github.com/nuket/google-sheets-geocoding-macro/ for extracting country names from a list of free-text strings
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 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