OO-Snippets: create number formats

Commons

KeywordsNumber, Format, Style, NumberFormat, GetNumberFormats, FormatString, QueryKey, QueryKeys
LanguageOOBasic
ApplicationOffice
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.

Code-Snippet-Listing (snippet-source)

'******************************************************************
'Author: Andrew Pitonyak
'email:   andrew@pitonyak.org
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 you choose to query on types, you need to use the type
  'com.sun.star.util.NumberFormat.DATE
  'I could set the locale from values stored at
  'http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt
  'http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html
  'I use a NULL locale and let it use what ever it likes.
  'First, see if the number format exists
  If ( Not IsMissing(locale)) Then
    aLocale = locale
  End If
  formatNum = oFormats.queryKey (sFormat, aLocale, TRUE)
  MsgBox "Current Format number is" & formatNum
  'If the number format does not exist then add it
  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

'******************************************************************
'Author: Andrew Pitonyak
'email:   andrew@pitonyak.org
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 you choose to query on types, you need to use the type
  'com.sun.star.util.NumberFormat.DATE
  'I could set the locale from values stored at
  'http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt
  'http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html
  'I use a NULL locale and let it use what ever it likes.
  'First, see if the number format exists
  If ( Not IsMissing(locale)) Then
    aLocale = locale
  End If
  formatNum = oFormats.queryKey (sFormat, aLocale, TRUE)
  MsgBox "Current Format number is" & formatNum
  'If the number format does not exist then add it
  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()
  'Author : Laurent Godard
  'e-mail : listes.godard@laposte.net
  'Modified : Andrew Pitonyak
  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

DateUserModification
2004-06-22tomsontomModified to match new snippet-DTD
0000-00-00and

and