In Appsheet, Can I change my appscript (which runs in the "C" column in my data spreadsheet)---into an app formula?

Viewed 88

I'm new to appsheet and to appscript, thanks for any consideration you can give.

The problem I am having is my appscript is running too many times in a 24 hour period:

Error Exception: Service invoked too many times for one day: route. (line 18).

I want to avoid the error altogether but I don't know what solution might be best. the script needs to calculate "miles" between two addresses. I am using an appsheet app to fill the addresses, and all users enter about 4-5 entries daily, so I dont think it is the quota that is the problem---the app needs to have the values viewable, but the spreadsheet is calculating the entries everytime. the formula to run the address script is in the spreadsheet itself. Also, is it possible solution to run these address requests from the app and not on the data spreadsheet.

here is spreadsheet formula:

=(GOOGLEMAPS(A4,B4, "miles"))

here is the appscript:

/**
* Get Distance between 2 different addresses.
* @param start_address Address as string Ex. "300 N LaSalles St, Chicago, IL"
* @param end_address Address as string Ex. "900 N LaSalles St, Chicago, IL"
* @param return_type Return type as string Ex. "miles" or "kilometers" or "minutes" or "hours"
* @customfunction
*/
 
function GOOGLEMAPS(start_address,end_address,return_type) {
 
  // https://www.chicagocomputerclasses.com/
  // Nov 2017
  // improvements needed
  
  var mapObj = Maps.newDirectionFinder();
  mapObj.setOrigin(start_address);
  mapObj.setDestination(end_address);
  var directions = mapObj.getDirections();
  
  var getTheLeg = directions["routes"][0]["legs"][0];
  
  var meters = getTheLeg["distance"]["value"];
  
  switch(return_type){
    case "miles":
      return meters * 0.000621371;
      break;
    case "minutes":
        // get duration in seconds
        var duration = getTheLeg["duration"]["value"];
        //convert to minutes and return
        return duration / 60;
      break;
    case "hours":
        // get duration in seconds
        var duration = getTheLeg["duration"]["value"];
        //convert to hours and return
        return duration / 60 / 60;
      break;      
    case "kilometers":
      return meters / 1000;
      break;
    default:
      return "0";
   }
  
}
0 Answers
Related