UNO: NumberFormat auslesen und umwandeln in StandardFormat

Hallo und guten Morgen,
ich habe ein Problem beim Umwandeln des lokalen NumberFormats in das StandardFormat einer Calc-Zelle. Die Umwandlung läuft in Basic zur Laufzeit.

Die Konstanten sind hier in der API beschrieben.
UNO Konstanten

Das eigentliche Problem betrifft das Format CURRENCY. Als Short definiert mit dem Wert = 8.

Zur Klarstellung:
Zunächst wird der durch das Makro Wert in die Zelle geschrieben:

Sub setCellStandardFormat(oCell As Object , Constant As Long )
Dim nKey As Long
Dim oDoc As Object
	
	oDoc = thisComponent

	Select Case Constant
		Case _NUMBER
			nKey = oDoc.NumberFormats.getStandardFormat( _
           		_NUMBER, _
          		 oDoc.CharLocale)
           
        Case _CURRENCY
        	nKey = oDoc.NumberFormats.getStandardFormat( _
           		_CURRENCY, _
           		oDoc.CharLocale)
        Case _DATE
        	nKey = oDoc.NumberFormats.getStandardFormat( _
           		_DATE, _
           		oDoc.CharLocale)
        Case _TIME
        	nKey = oDoc.NumberFormats.getStandardFormat( _
           		_TIME, _
           		oDoc.CharLocale)
        Case _DATETIME
        	nKey = oDoc.NumberFormats.getStandardFormat( _
           		_DATETIME, _
           		oDoc.CharLocale)
        Case _PERCENT
        	nKey = oDoc.NumberFormats.getStandardFormat( _
           		_PERCENT, _
           		oDoc.CharLocale)
        Case _SCIENTIFIC
        	nKey = oDoc.NumberFormats.getStandardFormat( _
           		_SCIENTIFIC, _
           		oDoc.CharLocale)
        Case _FRACTION
        	nKey = oDoc.NumberFormats.getStandardFormat( _
           		_FRACTION, _
           		oDoc.CharLocale)
        Case _LOGICAL
        	nKey = oDoc.NumberFormats.getStandardFormat( _
           		_LOGICAL, _
           		oDoc.CharLocale)
        Case _TEXT
        	nKey = oDoc.NumberFormats.getStandardFormat( _
           		_TEXT, _
           		oDoc.CharLocale)
	End Select
	
	oCell.NumberFormat = nKey 	
End Sub

Als Ergebnis erhält die Zelle den Index des lokalen Formats (z.B. de_DE). Die Sub erwartet die Zelle und eine Konstante. In diesem konkreten Fall die 8 für CURRENCY. Der lokale Wert (Index: 10115) wird in die Zelle geschrieben und Calc zeigt die Zelle korrekt an.

Das Problem beginnt bei dem umgekehrten Weg. Ich lese die Zelle aus, und der lokale Index soll nun wieder den Standard Index liefern:

Sub getStandardFormatType (oCell As object) As Long
Dim oDoc As Object
Dim oFormats As Object
Dim oFormat As Object
Dim nKey As Long

    oDoc = ThisComponent
    nKey = oCell.NumberFormat
    oFormats = oDoc.NumberFormats
    oFormat = oFormats.getByKey(nKey)
    
    getStandardFormatType = oFormat.Type
    
End Sub

Die Sub liefert nun den Wert 9, der Überhaupt nicht definiert ist. Hat dazu jemand eine Erklärung oder Lösung?

warum machst du dieses ganze »select…case« geschwurbel?

oCell.NumberFormat = oDoc.NumberFormats.getStandardFormat( constant, oDoc.CharLocale )

ansonsten wäre meine Vermutung das oFormat.Type nicht das ist, was du glaubst was es sein sollte?!

Das Problem ist doch, dass da zwar eine Konstante für alle CURRENCY-Formate steht, aber eben nicht die Unterscheidung der verschiedenen Formate in dem entsprechenden Dokument.
Wenn Du ein entsprechenden Formatcode für das aktuelle Dokument erhalten willst, dann musst Du den auch zusammen mit dem Dokument ermitteln.

Ich habe hier für Formulare in Base die folgende Funktion zusammengebaut:

FUNCTION SetNumberFormat(stFormat AS STRING, oFeld AS OBJECT)
	DIM oFormats AS OBJECT
	DIM arLocale as New com.sun.star.lang.Locale
	oFormats = oFeld.FormatsSupplier.getNumberFormats()
	SetNumberFormat = oFormats.queryKey(stFormat, arLocale, True)
	IF SetNumberFormat = -1 THEN  '-1 bedeutet, dass kein entsprechendes Format gefunden wurde
		SetNumberFormat = oFormats.addNew(stFormat, arLocale) ' Neuen Schlüssel erstellen
	END IF
END FUNCTION

Das klappt jedenfalls einwandfrei. Da ist das Feld in dem Formular als Objekt vorgegeben. Dann wird geschaut ob das gewünschte Format, z.B. “#.##0,00 [$€-407];[ROT]-#.##0,00 [$€-407]” existiert und wenn nicht, wird es entsprechend erstellt.

1 Like

Genau das macht die zweite Sub. Die anderen Formate funktionieren auch, nur bei CURRENCY liefert die Sub eine 9 statt eine 8. Und ich weiss nicht wo die 9 herkommt und warum.

Ich möchte kein neues Format setzen, ich möchte einfach nur wissen, wie die mir “unbekannte Zelle” formatiert ist. Die Extension weiss nicht ob sie in Amerika, England oder Deutschland läuft.

Vielleicht habe ich auch nur einen Denkfehler.

Wenn ich mich nicht irre, hat das Zahlenformat “Standard” (“Default”) für das aktuelle Gebietsschema hat immer den Key 0.