User Defined Functions

User defined functions, or UDF’s, are Python functions that can be called directly from Excel. UDF’s are turned off by default and can be enabled from the settings menu.

Overview

To import Python functions, define them in a file named “functions.py” and import that file into DataNitro. Any function defined in the file will then be available for use in Excel. It’s not necessary to run the script.

Imported functions cannot make calls to other Excel objects (so they cannot use Cell or CellRange objects, for example). To use values in other cells, pass them as arguments to the function.

e.g: To compute the sum of cells A1 and A2, write the following in functions.py:

def my_sum(x, y):
    return x + y

You can then put =my_sum(A1, A2) into a cell.

The following function will fail:

def my_sum_a1_a2():
    return Cell(A1).value + Cell(A2).value

Input and Output

Function Input

Args, kwargs, and keyword arguments are not supported.

Function Output

The udf will print the return value in the cell it is called. If an error occurs, it will print the error to the cell.

Notes

UDF’s imported into any workbook will be available in all workbooks. All UDF’s run in the same process, so any shared variables will be available from all workbooks as well.

A UDF will time out if it doesn’t return a result quickly. (It will return “Python timeout”.) This is to prevent functions from freezing Excel indefinitely in the case of errors or network problems. You can change the amount of time alloted per function in the DataNitro settings. The default is 3 seconds.