Basic is dead, clumsy, difficult. You dont find recent literature on Basic.
Pyton is a professional and modern programming lanuguage (yes, it is easier than Basic).
Stuff like this
should be wrapped in a spreadsheet add-in (a package that adds functions to Calc's function wizard). Nevertheless it can be done with pure scripting. The attached spreadsheet document calls a Basic function PYFUNCTION which itself calls a specified Python function in Python module SheetFunctions.py with up to 6 positional arguments.
Save the Python code (embedded Writer object) unmodified as plain text file under
<user_profile>/Scripts/python/SheetFunctions.py
Right-click>Save Copy As...
Automatic file name extension = OFF (or rename SheetFunctions.txt to SheetFunctions.py later)
File Type: Text(*.txt)
Browse to the the Scripts folder and create a subdirectory “python” if necessary and save as “SheetFunctions.py”
Hit Ctrl+Shift+F9 to recalculate the above formulas
Don't forget to turn on the automatic file name extension when you save the next document.
Move the Basic module of the attached spreadsheet to your global Basic container.
menu:Tools>Macros>Organize>Basic... [Organizer...], Ctrl+Drag&Drop copies a module from one library to another library.
From your own Basic code you can call my Basic function PYFUNCTION which will call the right Python code.
PYFUNCTION takes the name of a Python function and up to 6 discrete arguments to be passed to this function (no array arguments yet).
Code: Select all
REM if you put my Basic to a lib other than "Standard":
GlobalScope.BasicLibraries.loadLibrary("LibraryName")
REM let's use a Writer selection of text:
s = ThisComponent.CurrentSelection(0).getString()
md5hash = PYFUNCTION("md5sum", s)
--------- OFF TOPIC ---------
If the code was wrapped in a Calc add-in, the function call would require the installation of an extension package and then:
Code: Select all
REM pass a text selection in Writer to a spreadsheet function:
srv = createUnoService("com.sun.star.sheet.FunctionAccess")
s = ThisComponent.CurrentSelection(0).getString()
md5hash = srv.callFunction("org.villeroy.PyFunctions.getMD5SUM", s)
The function would appear as a normal spreadsheet function MD5SUM among the other spreadsheet functions.