User Defined Functions

User defined functions added via PyXLL are identical in appearance to any other functions available in excel, the only difference is they are significantly easier and faster to write.

Here’s a simple example of a user defined worksheet function to get started with:

from pyxll import xl_func

@xl_func("string name: string")
def hello(name):
    """returns a familiar greeting"""
    return "Hello, %s" % name

The decorator xl_func tells PyXLL what arguments the function takes and (optionally) the return type so it can register it correctly in Excel. The argument text you see in Excel is taken from this signature, and the function help is taken from the function’s docstring.

And that’s it. There is no more code to write to get that function in Excel. There’s nothing to compile; you don’t even need to restart Excel to see it!

To see this new function in Excel, the PyXLL addin is loaded via the addins menu in Excel in the same way as any other addin.

The PyXLL addin automatically picks up any imported functions with the xl_func decorator. By default the functions are added to the PyXLL category in Excel, but that can be altered on a per function basis using the optional argument category to xl_func.

Here’s how this python function looks in Excel: