Using Python functions as UDF()

Using the built-in UDF() (user-defined-) function you can call any Python modules/functions in formulas in GS-Calc.
This can help you to significantly increase GS-Calc functionality thanks to the large number of various Python modules and libraries including, for example, science, finance, graphics.

From GS-Calc you can pass to Python functions any type of parameters: (double precision) numbers, text strings, ranges as numeric arrays/matrices, automatically saved blocks of CSV data from ranges.

GS-Calc can accept from Python functions any type of data it supports in formulas: (double precision) numbers, text strings, numeric arrays/matrices, 2d arrays of any size and with any cell contents passed to GS-Calc as CSV data blocks to parse and finally images/graphics (that can overflow the formula cells).

The UDF() function:

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

Python modules are plain text files with the "*.py" extension. Below is an example of such a file containing seven simple functions.

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)

In GS-Calc the above functions can be use as follows:

Related Topics

Installing Python and integrating it with GS-Calc