 | OO-Snippets: create number formatsCommons| Keywords | Number, Format, Style, NumberFormat, GetNumberFormats, FormatString, QueryKey, QueryKeys |
|---|
| Language | OOBasic |
|---|
| Application | Office |
|---|
| Authors |
Andrew Pitonyak
(initial)
Laurent Godard
(initial)
Michael HŮnnig
Tom Schindl
|
|---|
| Supported Versions | |
|---|
| Supported OS | |
|---|
| Question |
How can I find and create number formats?
|
|---|
| Answer |
If you want a particular number format, then you can see if you have it and create it if you do not. For more information on valid formats, see the help contents on topic "number formats; formats". They can be very complex. The following macro creates a number format if it does not exist. The appropriate number format is returned.
If you want to see all of your existing number formats, try something like the following, which is a macro that I modified from Laurent Godard.
The following macro enumerates the current number format styles.
The style numbers (keys) and their text representation are inserted into the current document.
The disadvantage to this version is that it enumerates the styles based on the locale.
The original version enumerated the key from 0 to 1000, ignoring errors.
This will find all formats regardless of locale, but I consider this macro a slightly cleaner solution. If you want to see all of your existing number formats, try something like the following, which is a macro that I modified from Laurent Godard.
The following macro enumerates the current number format styles.
The style numbers (keys) and their text representation are inserted into the current document.
The disadvantage to this version is that it enumerates the styles based on the locale.
The original version enumerated the key from 0 to 1000, ignoring errors.
This will find all formats regardless of locale, but I consider this macro a slightly cleaner solution.
|
|---|
Function FindCreateNumberFormatStyle (_
sFormat As String, Optional doc, Optional locale)
Dim oDocument As Object
Dim aLocale as new com.sun.star.lang.Locale
Dim oFormats As Object
oDocument = IIf(IsMissing(doc), ThisComponent, doc)
oFormats = oDocument.getNumberFormats()
If ( Not IsMissing(locale)) Then
aLocale = locale
End If
formatNum = oFormats.queryKey (sFormat, aLocale, TRUE)
MsgBox "Current Format number is" & formatNum
If (formatNum = -1) Then
formatNum = oFormats.addNew(sFormat, aLocale)
If (formatNum = -1) Then formatNum = 0
MsgBox "new Format number is " & formatNum
End If
FindCreateNumberFormatStyle = formatNum
End Function
Function FindCreateNumberFormatStyle (_
sFormat As String, Optional doc, Optional locale)
Dim oDocument As Object
Dim aLocale as new com.sun.star.lang.Locale
Dim oFormats As Object
oDocument = IIf(IsMissing(doc), ThisComponent, doc)
oFormats = oDocument.getNumberFormats()
If ( Not IsMissing(locale)) Then
aLocale = locale
End If
formatNum = oFormats.queryKey (sFormat, aLocale, TRUE)
MsgBox "Current Format number is" & formatNum
If (formatNum = -1) Then
formatNum = oFormats.addNew(sFormat, aLocale)
If (formatNum = -1) Then formatNum = 0
MsgBox "new Format number is " & formatNum
End If
FindCreateNumberFormatStyle = formatNum
End Function
Sub enumFormats()
Dim vText
Dim vFormats, vFormat
Dim vTextCursor, vViewCursor
Dim iMax As Integer, i As Integer
Dim s$
Dim PrevChaine$, Chaine$
Dim aLocale as new com.sun.star.lang.Locale
vFormats = ThisComponent.getNumberFormats()
RunSimpleObjectBrowser(vFormats)
vText = ThisComponent.Text
vViewCursor = ThisComponent.CurrentController.getViewCursor()
vTextCursor = vText.createTextCursorByRange(vViewCursor.getStart())
Dim v
v = vFormats.queryKeys(com.sun.star.util.NumberFormat.ALL, aLocale, False)
For i = LBound(v) To UBound(v)
vFormat=vFormats.getbykey(v(i))
chaine=VFormat.FormatString
If Chaine<>Prevchaine Then
PrevChaine=Chaine
chaine=CStr(v(i)) & CHR$(9) & CHR$(9) & chaine & CHR$(10)
vText.insertString(vTextCursor, Chaine, FALSE)
End If
Next
MsgBox "Finished"
End Sub
|
Changelog| Date | User | Modification |
|---|
| 2004-06-22 | tomsontom | Modified to match new snippet-DTD | | 0000-00-00 | and | |
|