These decorators, functions and classes are used to expose Python functions to Excel as worksheet functions (UDFs).
Please see Worksheet Functions for complete details on writing Excel worksheet functions in Python.
See also Type Conversion and Real Time Data.
xl_func
([signature][, category="PyXLL"][, help_topic=""][, thread_safe=False][, macro=False][, allow_abort][, volatile=False][, disable_function_wizard_calc=None][, disable_replace_calc=None][, name][, auto_resize=False][, hidden=False][, transpose=False][, recalc_on_open][, formatter=None][, nan_value][, posinf_value][, neginf_value][, none_value])xl_func is decorator used to expose python functions to Excel. Functions exposed in this way can be called from formulas in an Excel worksheet and appear in the Excel function wizard.
Parameters: |
|
---|
Example usage:
from pyxll import xl_func
@xl_func
def hello(name):
"""return a familiar greeting"""
return "Hello, %s" % name
# Python 3 using type annotations
@xl_func
def hello2(name: str) -> str:
"""return a familiar greeting"""
return "Hello, %s" % name
# Or a signature may be provided as string
@xl_func("int n: int", category="Math", thread_safe=True)
def fibonacci(n):
"""naive iterative implementation of fibonacci"""
a, b = 0, 1
for i in xrange(n):
a, b = b, a + b
return a
See Worksheet Functions for more details about using the xl_func decorator, and Array Functions for more details about array functions.
XLAsyncHandle
XLAsyncHandle instances are passed to Asynchronous Functions as the async_handle argument.
They are passed to xlAsyncReturn
to return the result from an asynchronous function.
set_value
(value)Set the value on the handle and return it to Excel.
Equivalent to xlAsyncReturn
.
@Since PyXLL 4.2.0
Example usage:
from pyxll import xl_func
import threading
import sys
@xl_func("async_handle h, int x")
def async_func(h, x):
def thread_func(h, x):
try:
result = do_calculation(x)
h.set_value(result)
except:
result.set_error(*sys.exc_info())
thread = threading.Thread(target=thread_func, args=(h, x))
thread.start()
New in PyXLL 4.2
For Python 3.5.1 and later, asynchronous UDFs can be simplified by simply using the async keyword on the function declaration and dropping the async_handle argument.
Async functions written in this way run in an asyncio event loop on a background thread.