Alex Rivera | Logout

Optional arguments in custom Google Apps Script functions

Asked 2012-06-01T02:45:44.160
29

I want to write a custom function which has some mandatory arguments but can also accept a few optional arguments. How can I do this?

Edit
Report

1 Answer

29

Custom functions don't have a concept of required and optional fields, but you can emulate that behavior using logic like this:

function foo(arg1, opt_arg2) {
  if (arg1 == null) {
    throw 'arg1 required';
  }
  return 'foo';
}

It's convention to use the prefix "opt_" for optional parameters, but it's not required.

answered 2012-06-04T14:12:42.840

Your Answer