Optional {Array | string} parameter/Argument in custom function

351 views
Skip to first unread message

Mhd

unread,
Apr 1, 2022, 7:33:19 AM4/1/22
to Google Apps Script Community
Hello, App script community

This function works fine but I need to improve it and make the fourth argument optional for the user.

Now It returns an error when one of the cells in the fourth argument array does not contain data

Can we make the 4th argument in this custom function in App Script, optional, and at the same time it can override the lack of data in the array?


/**
 * Calculate the distance between two
 * locations on Google Maps.
 * =GOOGLEMAPS_DISTANCE("NY 10005", "Hoboken NJ", "driving", value_or_array)
 *
 * @param {String} origin The address of starting point
 * @param {String} destination The address of destination
 * @param {String} mode The mode of travel (driving, walking, bicycling or transit)
 * @param {String|Array} waypoints To include as part of route
 * @customFunction
 */
const GOOGLEMAPS_DISTANCE = (origin, destination, mode, waypoints) => {
  const route = Maps.newDirectionFinder()
    .setOrigin(origin)
    .setDestination(destination)
    .setMode(mode);

  if(waypoints){
    Array.isArray(waypoints) ?
      waypoints.map(row => row.map(cell => route.addWaypoint(cell))) :
      route.addWaypoint(waypoints);
  }

  const { routes: [data] = [] } = route.getDirections();

  if (!data) {
    throw new Error('No route found!');
  }

  let distance = data.legs.reduce((p,v) => (p += v.distance.value, p), 0);
  return distance;
};



note:
This function gives me the path distance from the starting point to the ending point through way point/s "which I need to make optional".

In the data I'm working on, there are four different cases (a path without waypoints - a path with one waypoint - a path with two waypoints - a path with three waypoints).

Unfortunately, with each of these cases, I need to modify the fourth argument array in order to make my function work.

Edward Ulle

unread,
Apr 1, 2022, 10:25:55 AM4/1/22
to Google Apps Script Community
Parameters to functions are always optional.  You can call it 

let result =  GOOGLEMAPS_DISTANCE(origin1, destination1, mode1, waypoints1)  

Or  

let results = GOOGLEMAPS_DISTANCE(origin2, destination2, mode2)   

in which case inside the function wayponts = undefined.  

However optional parameter start from the end of the parameter list so there is no way to make destination optional but mode required.  In that case you have to include a null in the parameter list  

let result = GOOGLEMAPS_DISTANCE (origin3, null, mode3, waypoints3) 

Message has been deleted

Mhd

unread,
Apr 1, 2022, 4:15:44 PM4/1/22
to Google Apps Script Community
This function works correctly and without any problems, if the fourth argument is a single cell or text (regardless of whether the cell contains the point lat/long or is empty).

But when the fourth argument is an array "Range of cells like D3:H3
if one of the ranges of cells D3:H3  or more do not contain point/s lat/long) the function stops and returns an error "invalid argument address"!

I need to make the function can ignore the lack of data in the range of cells.

On Friday, 1 April 2022 at 22:58:04 UTC+3 Mhd wrote:
Would you tell me exactly where I should add this line, please!
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages