Searching for the codes of NumberFormat, which could be set by Basic to get a right formatted cell, I won’t get any complete answer here. Have seen Basic Macro - Date NumberFormat , but there isn’t any example.
I have created a Writer-template and started the following code in a document, which is saved in same path as the template:
SUB HardcodedFormatCode
DIM oDB AS OBJECT
DIM oNewDoc AS OBJECT
DIM oTables AS OBJECT
DIM oTable AS OBJECT
DIM oRows AS OBJECT
DIM stDir AS STRING
DIM i AS INTEGER
DIM k AS INTEGER
DIM inCols AS INTEGER
oDB = ThisComponent
stDir = Left(oDB.Location,Len(oDB.Location)-Len(oDB.Title))
stDir = stDir & "HardFormatCodes.ott"
DIM args(0) AS NEW com.sun.star.beans.PropertyValue
args(0).Name = "AsTemplate"
args(0).Value = True
oNewDoc = StarDesktop.loadComponentFromURL(stDir,"_blank",0,args)
oTables = oNewDoc.getTextTables
oTable = oTables.getByName("Printout")
i = 1
FOR k = 1 TO 120
oTable.getCellByPosition(0,i).setValue(k)
oTable.getCellByPosition(1,i).setValue(44992.25) '2023-03-07 06:00:00
oTable.getCellByPosition(1,i).NumberFormat = k
oRows = oTable.getRows()
oRows.insertByIndex(oRows.getCount(),1)
i = i + 1
NEXT
oRows.removeByIndex(oRows.getCount()-1,1) ' Last Row has to be deleted
END SUB
This will write all hard coded number formats (Starting with number 1, ending with number 120) into a Writer table.
This won’t show the special format which you get when opening dialog “Format Number”. So there is missing, for example, in German list code for currency together with €
. Contains only DM
, which has been gone since 2002.
I don’t know why, but when getting a NumberFormat together with locale, it will show the user defined formats also:
FUNCTION CellSetNumberFormat(stNumberFormat AS STRING, oDoc AS OBJECT) AS LONG
DIM aLocale AS NEW com.sun.star.lang.Locale
DIM oNumberFormats AS OBJECT
DIM loFormatKey AS LONG
oNumberFormats = oDoc.getNumberFormats()
loFormatKey = oNumberFormats.queryKey(stNumberFormat, aLocale, FALSE)
IF loFormatKey = -1 THEN loFormatKey = oNumberFormats.addNew(stNumberFormat, aLocale)
CellSetNumberFormat = loFormatKey
End Function
With his function I could get the right number for getting my special currency symbol. I only looked at the code in dialog “Number Format”:
CellSetNumberFormat("#.##0,00 [$€-407];[ROT]-#.##0,00 [$€-407]", oNewDoc)
Hope this will help others to find the right number for NumberFormat. I’m using this for getting data from a Base file into a Writer document.
HardFormatCodes.ott (12.8 KB)