Author Topic: Excel functions with optional arguments  (Read 1358 times)

dwcawlfield

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Excel functions with optional arguments
« on: February 13, 2014, 07:14:40 PM »
With Excel VBA we can define UDFs with optional args, and we can do the same in python, but I can't find a way to create one with the xl_func decorator.  Of course, I'm suggesting that an optional argument is something with a default value.  Is there something undocumented, or is it just not possible yet.

David

Tony

  • Administrator
  • Hero Member
  • *****
  • Posts: 584
  • Karma: +14/-1
    • View Profile
Re: Excel functions with optional arguments
« Reply #1 on: February 13, 2014, 07:24:56 PM »
Hi,

in some sense all arguments are optional in excel. If you miss arguments when calling the function Excel will call the underlying function implementation with its own default values, which depend on the argument type. There is no way when registering a function to say what the default value should be. This is a limitation of the Excel XLL SDK.

If you use the 'var' type a missing value will be passed as None, which you can check for and substitute in your default. This isn't as convenient as python's keyword argument defaults but it should be workable.

Best regards,
Tony