Here is the Google Script:
function reverse_geocode(lat,lng) {
Utilities.sleep(1500);
var response = Maps.newGeocoder().reverseGeocode(lat,lng);
for (var i = 0; i < response.results.length; i++) {
var result = response.results[i];
Logger.log('%s: %s, %s', result.formatted_address, result.geometry.location.lat,
result.geometry.location.lng);
return result.formatted_address;
}
}
I'm trying to take coordinates from columns A and B and have them convert to an address after they appear in the sheet. I'd like to use an array here so the rows won't be used until they actually contain data. I followed this tutorial for the script: https://blogs.oregonstate.edu/deanna/2020/08/01/reverse-geocode-google-sheet/
Any ideas? Hope you can help.
Edit: the formula should be something more along the lines of:
=ArrayFormula(reverse_geocode(A1:A,B1:B))
I have altered your script to be able to accept and use an array.
/**
* Returns addresses from given coordinates
*
* @param {array} input The range containing the data.
* @return The coordinates as addresses.
* @customfunction
*/
function reverse_geocode(range) {
var arr = [];
for(var i of range) {
let lat = i[0];
let lng = i[1];
var response = Maps.newGeocoder().reverseGeocode(lat,lng);
var result = response.results[0];
Logger.log('%s: %s, %s', result.formatted_address, result.geometry.location.lat,
result.geometry.location.lng);
arr.push(result.formatted_address);
}
return arr;
}
This script will read and process each row within the given range individually, insert the results into an array, and lastly output the results.
If you have difficulty with this, have any questions, or would like this script altered in any way, please let me know
Hope this helps!
The script above is not the original, please see edit history for the first version.
Below is the response I get when using the (new) above script:
This script successfully returns the closest addresses for a given range.