Remote execution of basic code using LibreOffice headless

This macro, which is invoked from LibreOffice started in headless mode through xScript.invoke(…), works in the UI but does not execute when invoked through code (using location = document). The calculations on this Sheet are reflected in another sheet using a formula similar to (=$‘Sheet2’.A1). I’m using LibreOffice 6.4 and Java. The only difference is: works in IDE but not when invoked through code. What do I need to do for this to work when invoked through code?

Option VBASupport 1
Option Compatible

Sub Test
    Dim i As Double	
    With Sheets("Sheet2")
	Range("A1024").Select
	Selection.End(xlUp).Select
	Range("A1").Select
	Range(Selection, Selection.End(xlToRight)).Select
	Range(Selection, Selection.End(xlDown)).Select
	Selection.ClearContents	    
        For i = 1 To .Cells(6, 4)
            Calculate
            .Cells(14 + i, 3) = i
            .Cells(14 + i, 4) = .Cells(9, 4).Value
            .Cells(14 + i, 5) = .Cells(10, 4).Value        
        Next        
    End With
End Sub

XScriptProviderSupplier xsps = UnoRuntime.queryInterface(XScriptProviderSupplier.class, xSpreadsheetDocument); // I've used XComponent as well but both fail
XScriptProvider xsp = xsps.getScriptProvider();
XScript xs = xsp.getScript(...); // elided for brevity
short[][] i = ...;
Object[][] o = ...;
Object result = xs.invoke(...);

Edited for clarity by @Ratslinger - Use preformatted text tool