How to get the results of a formula for text based formulas?

How to get the results of a formula for text based formulas?

I am using the following function to get the cell value.

def get_val(cell: XCell) -> Any:
    t = cell.getType()
    if t == CellContentType.EMPTY:
        return None
    if t == CellContentType.VALUE:
        return convert_to_float(cell.getValue())
    if t in (CellContentType.TEXT, CellContentType.FORMULA):
        # https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1sheet_1_1FormulaResult.html
        ft = cell.FormulaResultType2
        if ft == 1:  # VALUE
            return cell.getValue()
        if ft == 2:  # STRING
            return cell.getFormula()
        if ft == 4:  # ERROR
            return f"(Error: {cell.getError()})"
    return None

Test SUM

A1 value 10
B1 value 20
C1 value =SUM(A1:B1)
get_val() for C1 returns 30

Test String

A2 value Hello
get_val() for A2 returns Hello

Test Formula String

A3 value Hello
B3 value World
C3 value =CONCATENATE(A3,B3)
get_val() for C1 returns (Error: 508)

How to Get Formula value?

Is there a way to get the actual formula results from a Calc cell when the result should be text?
How would I get the result of HelloWorld from =CONCATENATE(A3,B3)?

may I ask you: »returns this magic: “convert_to_float” better floats than “cell.Value”??

The convert_to_float was left over from the origin when I converted Java LibreOffice Programming by Andrew Davison. Thanks for pointing it out. I removed it.

cell.String

C3 value =CONCATENATE(A3,B3)
This is the result

cell.String
'Err:508'

Are you sure the comma is your argument separator?
You didn’t tell us your loacle / the respective setting.
The semicolon would be accepted by any locale.

Thank you guys.
It is looking like the semi colon separator is the fix.
I have a few more test to write but this is looking like the fix.


I see Formula and FormulaLocal have different seperators.

Everything seems to be working. Thanks again everyone.
Here is a link to my updated function.