Macro overlays formula in cell

When a cell is selected and it contains a formula, Calc by default shows the formula in the formula bar and the calculation result in the cell.

But when I code =MyMacro() in a cell and MyMacro() saves a result in the cell with thisComponent.currentController.selection.setValue(result) the formula is overwritten with the result and the call to MyMacro() is lost. The result displays in both the selected cell and in the formula bar.

What did I do wrong or fail to do?

I think, maybe you do not need to save the result in the cell, if the macro function MyMacro() return a value, that’s all, like any other function.

Function sample in LibreOffice help:

Function VOL(a, b, c)

VOL = abc

End Function

SOLVED - thank you