I have trouble applying my custom formula across all columns using Array Formula. My custom formula is named getCountry where it could get the country based on an address. The address components e.g. street or city are also separated per column. When I apply the formula to a cell, it displays the correct country. When I enclosed it in ArrayFormula, it only displays the country of the first row.
I want to apply the ArrayFormula function because if there are new rows I want to apply the formula automatically. Is there a workaround for this if ArrayFormula will not work?
Here's the sample spreadsheet.
https://docs.google.com/spreadsheets/d/1W3s1a8mG2fkmFlrez9mQLqwtyIcl-gNym1xtvLPFN1Q/edit?usp=sharing
Here's the code for my custom function:
function getCountry(address) {
var response = Maps.newGeocoder().geocode(address);
for (var i = 0; i < response.results.length; i++) {
var result = response.results[i];
country = []
for (var comp of result.address_components) {
if (comp.types.indexOf("country") > -1) country.push(comp.long_name)
}
if(country.length > 1) return country.join()
return country[0]
}
}
Edit: I've managed to make it work on a smaller dataset. But when I apply it to a much bigger dataset it will prompt with an error "exceeded maximum execution time". How do I make it better so that it will not return with that error when applied on a bigger dataset? The formula I used is:
=transpose(split(getCountryList(transpose(query(transpose(filter(A2:D,not(isblank(A2:A)))),,rows(filter(A2:A,not(isblank(A2:A))))))),","))
and the custom functions that I used now are:
function getCountry(address){
var response = Maps.newGeocoder().geocode(address);
country = []
for (var i = 0; i < response.results.length; i++) {
var result = response.results[i];
for (var comp of result.address_components) {
if (comp.types.indexOf("country") > -1) country.push(comp.long_name)
}
}
return (country.length > 1)?country.join(): country[0];
}
function getCountryList(address_array) {
country_list = []
for (i in address_array){
var country = getCountry(address_array[i]);
country_list.push(country);
}
return country_list.join();
}