I have the following code to convert a series of post codes to long lat by selecting the cells, in this example, A2:C3
It works well but want I want it to do is get the range into an add a long and lat entry to the array, then dump the array back to the sheet on the assumption it would be quicker.
I have tried cells.split but was still only able to print one line back at a time.
Example data, UK post codes with empty cells for long and lat
| A | B | C |
1 | Postcode | Long | Lat |
2 | SW1 1AA | | |
3 | EC1V 9BP | | |
Current app script
function getGeocodingRegion() {
return PropertiesService.getDocumentProperties().getProperty('GEOCODING_REGION') || 'uk';
}
function addressToPosition() {
var sheet = SpreadsheetApp.getActiveSheet();
var cells = sheet.getActiveRange();
var addressColumn = 1;
var addressRow;
var latColumn = addressColumn + 1;
var lngColumn = addressColumn + 2;
var geocoder = Maps.newGeocoder().setRegion(getGeocodingRegion());
var location;
for (addressRow = 1; addressRow <= cells.getNumRows(); ++addressRow) {
var address = cells.getCell(addressRow, addressColumn).getValue();
// Geocode the address and plug the lat, lng pair into the
// 2nd and 3rd elements of the current range row.
location = geocoder.geocode(address);
// Only change cells if geocoder seems to have gotten a
// valid response.
if (location.status == 'OK') {
lat = location["results"][0]["geometry"]["location"]["lat"];
lng = location["results"][0]["geometry"]["location"]["lng"];
cells.getCell(addressRow, latColumn).setValue(lat);
cells.getCell(addressRow, lngColumn).setValue(lng);
}
}
};