I have no idea what is wrong with the default font, but yes, changing the font of the default cell style surely works in this particular case. However, everything is lost if you don’t change the file format.
I tried a macro which transfers the imported csv data into a new document. This shows the following side effects:
- Although there is no formatting info in csv, Calc detects “special numbers” (percent, date, time, currency) and formats them in order to be human readable. With a simple macro, everything ends up as unformatted numbers, day numbers instead of dates, day fractions instead of times, fractions instead of percent.
- Calc applies optimal column widths when importing csv.
The following code tries to do the right things with any given template and with a given set of style names existing in that template.
- The macro assumes your csv data within the current region around cell A1 of the source document.
- Style names that do not exist in the target document are ignored.
- Optimal width is applied to each column.
EDIT: extended version with its own file picker, and you can specify any template. Use the Main routine as a template for your own specific csv imports.
Sub Main()
Const cImportOptions = "59,34,ANSI,1,,1031,false,true,true,false,false,0,false,false,true"
REM file picker options
Const cFilterLabel = "Text (csv)"
Const cPattern = "*.csv"
Const cInitPath = "/home/andreas/Downloads/"
REM template options
Const cTemplate = "MyDefault"
Const cTemplateRegion = "" 'optional
REM sample arguments for formatting, ignored if style names don't exist
sHeaderStyle = "ColumnLabel"
aColumnStyles = Array("MyDate", "MyTime", "", "", "CurrEuro")
REM ------ END OF CONFIG ------
Dim aProps(1) as new com.sun.star.beans.PropertyValue
aProps(0).Name = "FilterOptions"
aProps(0).Value = cImportOptions
aProps(1).Name = "FilterName"
aProps(1).Value = "Text - txt - csv (StarCalc)"
sFile = pickFile(sTitle:="Open CSV", sInit:=convertToURL(cInitPath), sFilterLabel:=cFilterLabel, sPattern:=cPattern)
if sFile = "" then exit sub
doc1 = StarDesktop.loadComponentFromURL(sFile, "_blank", 0, aProps())
doc2 = getOpenTemplate(cTemplate, "scalc", cTemplateRegion)
transfer2DefaultTemplate doc1, doc2, sHeaderStyle, aColumnStyles
doc1.close(false)
End Sub
Sub transfer2DefaultTemplate(oSourceDoc, oTargetDoc, Optional sHeaderStyle, Optional aColumnStyles())
If isMissing(sHeaderStyle) then sHeaderStyle = ""
If isMissing(aColumnStyles) then aColumnStyles = Array()
cell = oSourceDoc.Sheets(0).getCellByPosition(0,0)
src = cell.getSpreadSheet.createCursorByRange(cell)
src.collapseToCurrentRegion()
a() = src.getDataArray()
r = uBound(a())
c = uBound(a(0))
tgt = oTargetDoc.Sheets(0).getCellRangeByPosition(0, 0, c, r)
tgt.setDataArray(a())
addr = tgt.getRangeAddress()
styles = oTargetDoc.StyleFamilies.getByName("CellStyles")
if styles.hasByName(sHeaderStyle) then
tgt.getCellRangeByPosition(addr.StartColumn, addr.StartRow, addr.EndColumn, addr.StartRow).CellStyle = sHeaderStyle
addr.StartRow = addr.StartRow +1
endif
iub = uBound(aColumnStyles())
for i = 0 to addr.EndColumn
if i <= iub then
s = aColumnStyles(i)
if styles.hasByName(s) then
tgt.getCellRangeByPosition(i, addr.StartRow, i, addr.EndRow).CellStyle = aColumnStyles(i)
next
endif
tgt.Columns.getByIndex(i).OptimalWidth = True
next
End Sub
Function getOpenTemplate(sTemplateName$, Optional sComponentName$, Optional sTemplateRegionName$)
If isMissing(sComponentName) or sComponentName ="" then sComponentName = "swriter"
If isMissing(sTemplateRegionName) or sTemplateRegionName = "" then sTemplateRegionName = "My Templates"
Dim a(1) as new com.sun.star.beans.PropertyValue
a(0).Name = "TemplateName"
a(0).Value = sTemplateName
a(1).Name = "TemplateRegionName"
a(1).Value = sTemplateRegionName
REM the component name does not matter with unambigous template names
getOpenTemplate = StarDesktop.loadComponentFromURL("private:factory/"& sComponentName,"_blank",0,a())
End Function
Function pickFile(sTitle$, sInit$, sFilterLabel$, sPattern$) As String
REM return a single file URL or ""
REM dialog starts at office default directory if sInit = ""
Dim oPicker, x()
oPicker = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
oPicker.setTitle(sTitle)
oPicker.setDisplayDirectory(sInit)
oPicker.setMultiSelectionMode(False)
oPicker.appendFilter(sFilterLabel, sPattern)
if oPicker.execute() then
x() = oPicker.getFiles()
pickFile = x(0)
endif
End Function
You get the right import options (first line in main routine) by opening your csv manually and runing the following code. Copy the displayed string with surrounding double quotes to cFilterOptions in your own macro.
Sub showFilterOptions()
Dim args(),i%
args() = thisComponent.getArgs
for i = 0 to uBound(Args())
if args(i).Name = "FilterOptions" then inputbox args(i).Name,"",cStr(args(i).value)
next
End Sub