it’s not very difficult.
First, create a module in a standard library named (for example) AutoOpen
Create subroutine like this
Sub openAndTrimCSV(Optional sFileName As String)
Dim sUrl As String, oDoc As Variant
If IsMissing(sFileName) Then Exit Sub
sUrl = convertToURL(sFileName)
Dim OpenProp(1) as New com.sun.star.beans.PropertyValue
OpenProp(0).name="FilterName"
OpenProp(1).name="FilterOptions"
OpenProp(0).value="Text - txt - csv (StarCalc)"
OpenProp(1).value="59,34,76,1,,1033,false,false,true,false,true"
If Not FileExists(sUrl) Then Exit Sub
oDoc = stardesktop.LoadComponentFromURL(sUrl, "_blank",0, OpenProp())
Rem If your version of LibreOffice can't remove trailing spaces then unrem this part
'Dim oSheet As Variant, oCursor As Variant, aData As Variant, i&, j&
' oSheet = oDoc.getSheets().getByIndex(0)
' oCursor = oSheet.createCursor()
' oCursor.gotoEndOfUsedArea(True)
' oData = oCursor.getDataArray()
' For i = LBound(oData) To UBound(oData)
' For j = LBound(oData(0)) To UBound(oData(0))
' oData(i)(j) = RTrim(oData(i)(j))
' Or not to touch the numbers
' If VarType(oData(i)(j)) = V_STRING Then oData(i)(j) = RTrim(oData(i)(j))
' Next j
' Next i
' oCursor.setDataArray(oData)
End Sub
UPDATED RTrim() cannot be applied to all cells, for text cells only!
Save it and close LibreOffice.
Now you can type in the command line
[Full path to LibreOffice\program folder]\scalc.exe macro:///Standard.AutoOpen.openAndTrimCSV([Full path and filename of your CSV-file])
Update You can use this macro to create a code snippet
Sub getMeCode
Dim oDoc As Variant
Dim aArgs As Variant
Dim sResult As String
Dim sTemp As String
oDoc = ThisComponent
aArgs = oDoc.getArgs()
sTemp = getValArg(aArgs, "URL")
sResult = "Dim sUrl As String, oDoc As Variant" + Chr(10) + "sUrl = convertToURL(""" + convertFromURL(sTemp) +""")" + Chr(10)
sResult = sResult + "Dim OpenProp(1) as New com.sun.star.beans.PropertyValue" + Chr(10)
sResult = sResult + "OpenProp(0).name=""FilterName""" + Chr(10)
sResult = sResult + "OpenProp(1).name=""FilterOptions""" + Chr(10)
sTemp = getValArg(aArgs, "FilterName")
sResult = sResult + "OpenProp(0).value=""" + sTemp + """" + Chr(10)
sTemp = getValArg(aArgs, "FilterOptions")
sResult = sResult + "OpenProp(1).value=""" + sTemp + """" + Chr(10)
sResult = sResult + "If Not FileExists(sUrl) Then Exit Sub" + Chr(10)
sResult = sResult + "oDoc = stardesktop.LoadComponentFromURL(sUrl, ""_blank"",0, OpenProp())"
MsgBox(sResult,0,"Select, Press Ctrl+C and Paste to macro")
End Sub
Function getValArg(aArg As Variant, sNameArg As String) As Variant
Dim i As Long
Dim tmp As Variant
getValArg = ""
For i = LBound(aArg) To UBound(aArg)
tmp = aArg(i)
If UCase(tmp.Name) = UCase(sNameArg) Then
getValArg = tmp.Value
Exit Function
EndIf
Next i
End Function
Just open your .csv file, set such parameters so that the data is imported as you want. Run the procedure getMeCode