Skip to content Skip to sidebar Skip to footer

Accept Arbitrary Number Of Arguments In Google Scripts Custom Function?

I'm trying to re-build COUNTIFS as a Google Scripts Custom Function and having trouble with one thing: how do I build a function that accepts an arbitrary number of arguments? If y

Solution 1:

You can reference the arguments object when the number of arguments passed to a function is variable.

Solution 2:

(Example to support AdamL's answer.)

The autocomplete feature for custom functions abuses jsdoc tags somewhat. The "parameter type" which is normally enclosed in braces, e.g. {String}, is used verbatim in the "Example" section of the function's help, while the parameter name is used in the quick-help.

You can take advantage of this to clarify functions with arbitrary parameters, as shown here.

screenshot

Code

/**
 * Calculate the driving distance along a route.
 *
 * @param {"london","manchester","liverpool"}
 *                   route  Comma separated ordered list of two or more map
 *                          waypoints to include in route. First point
 *                          is 'origin', last is 'destination'.
 *
 * @customfunction
 */functiondrivingDistance(route) {
  if (arguments.length < 2) thrownewError( "Must have at least 2 waypoints." )
  var origin = arguments[0];
  var destination = arguments[arguments.length-1];
  ...

Post a Comment for "Accept Arbitrary Number Of Arguments In Google Scripts Custom Function?"