I have a function that is calculating the distance between two coordinates as follows:
function distance(origin, destination) {
//Utilities.sleep(Math.random() * 60000);
destination = destination + "";
if (destination.split(",").length == 2) {
var directions = Maps.newDirectionFinder()
.setOrigin(origin)
.setDestination(destination)
.getDirections();
if (directions.routes.length > 0) {
return directions.routes[0].legs[0].distance.value / 1000;
}
return "-";
}
return 0;
}
It is used in the spreadsheet as follows:
=distance("83.342353,23.23353", V2)
The function runs fine but hits the rate limit for url calls since my spreadsheet has more than 200 rows. The error message is:
Service invoked too many times in a short time: urlfetch protected host rateMax. Try Utilities.sleep(1000) between calls. (line 5)
The fix suggested is to put a Utitlies.sleep()
in the code. Where exactly should I put this?