Using Python Functions as UDF()

1. Overview

The built‑in UDF() function allows GS-Calc formulas to call Python modules and functions. This greatly expands functionality through Python’s scientific, financial, and graphics libraries.

You can pass numbers, text strings, ranges (as numeric arrays/matrices), or automatically saved CSV blocks to Python.

Python functions may return numbers, text, arrays, CSV blocks (parsed or unparsed), or images (binary data).

2. UDF() Syntax

UDF(module, function, type, parameter1, parameter2, ...)

Additional flags for type:

If neither flag is used, ranges are passed as numeric arrays (text ignored).

3. Example Python Module

e:\example.py:

def function1():
   return 10

def function2(a, b):
  return a + b

def function3(text):
   return 'A new text with ' + text + ' inserted in the middle'

def function4():
   csvdata = 'abc,def,ghi\n123,456,789'
   return csvdata

def function5(csvdata):
   return csvdata

def function6(csvFilePath):
   f = open(csvFilePath, 'rb')
   file_content = f.read()
   f.close()
   return file_content

def function7(imageFilePath):
   f = open(imageFilePath, 'rb')
   file_content = f.read()
   f.close()
   return bytearray(file_content)

4. Usage Examples in GS-Calc

Related Topics

Installing Python and integrating it with GS-Calc