Solved:Why can't call the customized function in Calc?

java -version
openjdk version "21.0.10" 2026-01-20
OpenJDK Runtime Environment (build 21.0.10+7-Debian-1deb13u1)
OpenJDK 64-Bit Server VM (build 21.0.10+7-Debian-1deb13u1, mixed mode, sharing)

My python code:

cat    ~/.config/libreoffice/4/user/Scripts/python/finnhub_tools.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*- 

import urllib.request
import json

API_KEY = "xxxxxxxxxxxxxxxxxx"
def GetStock(symbol):
    try:
        url = f"https://finnhub.io/api/v1/quote?symbol={symbol}&token={API_KEY}"
        req = urllib.request.Request(url)
        with urllib.request.urlopen(req, timeout=5) as response:
            data = json.loads(response.read().decode())
            return data.get("c", "Error")
    except Exception as e:
        return f"Error: {str(e[:30])}"

g_exportedScripts = (GetStock,)

It works fine.


I can get APPL’s price.
Write GetStock function in Basic:

Function GetStock(symbol As String)
    On Error GoTo ErrorHandler

    Dim mspf As Object
    Dim scriptProvider As Object
    Dim script As Object
    Dim url As String
    mspf = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
    scriptProvider = mspf.createScriptProvider("")
    url = "vnd.sun.star.script:finnhub_tools.py$GetStock?language=Python&location=user"
    script = scriptProvider.getScript(url)
    GetStock = script.invoke(Array(symbol), Array(), Array())
    Exit Function

ErrorHandler:
    GetStock = "Error: " & Err.Description
End Function

Can’t call the python script !


How can fix it?

its an UDF ( which needs an argument )
call it from Calc: =GETSTOCK("AAPL")
or test from Basic-IDE:

sub main() 
    msgbox GetStock( "AAPL" )
end sub

Solved!
sudo apt install libreoffice-script-provider-python

1 Like