[Gelöst]Bedingte Formatierung

Programmierung unter AOO/LO (StarBasic, Python, Java, ...)

Moderator: Moderatoren

Jörg
*****
Beiträge: 401
Registriert: Mo, 21.10.2013 12:05

[Gelöst]Bedingte Formatierung

Beitrag von Jörg »

Hallo Fachleute,
trotz Dannenhöfer will mir das Makro nicht gelingen.
Für Zellen C42 und G42
1. Bed.: kleiner als 120
2. Bed.: zwischen 120 und 139
3. Bed.: größer gleich 141
2 Probleme
1. die 2. Bedingung wird nicht ausgeführt
2. der Code wird nur in Zelle C42, nicht in G42 ausgeführt
Würde mich freuen, wenn Ihr mir da weiterhelfen könntet.
Vielen Dank

Code: Alles auswählen

Sub EinfuegenConditionalFormCell
Dim oCell as Object
Dim oConditionalForm as Object
oCell = ThisComponent.Sheets(9).getCellRangeByName("C42","G42")
oConditionalForm = oCell.ConditionalFormat
Dim oCondition(3) as New com.sun.star.beans.PropertyValue
oCondition(0).Name = "Operator"
oCondition(0).Value = com.sun.star.sheet.ConditionOperator.LESS
oCondition(1).Name = "Formula1"
oCondition(1).Value = "120"
oCondition(2).Name = "StyleName"
oCondition(2).Value = "warnGELB"
oConditionalForm.addNew( oCondition() )
oCondition(0).Value = com.sun.star.sheet.ConditionOperator.BETWEEN
oCondition(1).Name = "Formula1"
oCondition(1).Value = "120"
oCondition(1).Name = "Formula2"
oCondition(1).Value = "139"
oCondition(2).Name = "StyleName"
oCondition(2).Value = "warnGRÜN"
oCondition(0).Value = com.sun.star.sheet.ConditionOperator.GREATER_EQUAL
oCondition(1).Name = "Formula1"
oCondition(1).Value = "141"
oCondition(2).Name = "StyleName"
oCondition(2).Value = "warnROT"
oConditionalForm.addNew( oCondition() )
oCell.ConditionalFormat = oConditionalForm
End Sub
Zuletzt geändert von Jörg am So, 27.04.2025 10:19, insgesamt 1-mal geändert.
Gruß Jörg

Win 10 Pro AOO 4.1.15
Jörg
*****
Beiträge: 401
Registriert: Mo, 21.10.2013 12:05

Re: Bedingte Formatierung

Beitrag von Jörg »

Habe das 2. Problem eben gelöst.

Code: Alles auswählen

oCell = ThisComponent.Sheets(9).getCellRangeByName("C42","G42")
ersetzt durch

Code: Alles auswählen

oCell = ThisComponent.Sheets(9).getCellRangeByName("C42")
oCelle = ThisComponent.Sheets(9).getCellRangeByName("G42")
am Ende folgene Zeile hinzugefügt

Code: Alles auswählen

oCelle.ConditionalFormat = oConditionalForm
Jetzt ist "nur" noch das Problem mit der 2. Bedingung
Gruß Jörg

Win 10 Pro AOO 4.1.15
mikeleb
*******
Beiträge: 1400
Registriert: Fr, 09.12.2011 16:50

Re: Bedingte Formatierung

Beitrag von mikeleb »

Hallo,
du könnest es so lösen:

Code: Alles auswählen

Sub EinfuegenConditionalFormCell
	Dim oCell as Object
	Dim oConditionalForm as Object
	Dim oCondition(3) as New com.sun.star.beans.PropertyValue
	zellen=Array("C42","G42")
	oTab = ThisComponent.Sheets(9)
	for i=0 to ubound(zellen)
		ocell=oTab.getCellRangeByName(zellen(i))
		oConditionalForm = oCell.ConditionalFormat
		'vorhandene Formate löschen
		oConditionalForm.clear
		'Bedingung 1 definieren und setzen
		oCondition(0).Name = "Operator"
		oCondition(0).Value = com.sun.star.sheet.ConditionOperator.LESS
		oCondition(1).Name = "Formula1"
		oCondition(1).Value = "120"
		oCondition(2).Name = "Formula2"
		oCondition(2).Value = ""
		oCondition(3).Name = "StyleName"
		oCondition(3).Value = "warnGELB"
		oConditionalForm.addNew(oCondition)
		'Bedingung 2 definieren und setzen
		oCondition(0).Value = com.sun.star.sheet.ConditionOperator.BETWEEN
		oCondition(1).Value = "120"
		oCondition(2).Value = "139"
		oCondition(3).Value = "warnGRÜN"
		oConditionalForm.addNew( oCondition)
		'Bedingung 3 definieren und setzen
		oCondition(0).Value = com.sun.star.sheet.ConditionOperator.GREATER_EQUAL
		oCondition(1).Value = "141"
		oCondition(2).Value = ""
		oCondition(3).Value = "warnROT"
		oConditionalForm.addNew( oCondition)
		oCell.ConditionalFormat = oConditionalForm
	Next
End Sub
Es bleibt die Frage, was passiert, wenn die Zelle den Wert 140 hat ...
Gruß,
mikeleb
Jörg
*****
Beiträge: 401
Registriert: Mo, 21.10.2013 12:05

Re: Bedingte Formatierung

Beitrag von Jörg »

Hallo mikeleb,
funktioniert. Ich danke Dir.
habe noch eine Frage zum Code.

Code: Alles auswählen

Dim oCondition(3) as New com.sun.star.beans.PropertyValue
Die 3 in Klammern. Ist das die Anzahl der Bedingungen?
Auf diese Weise ließen sich ja mehr als drei Bedingungen einbauen.
Habe von 141 auf 140 geändert.
Nochmals danke.
Gruß Jörg

Win 10 Pro AOO 4.1.15
mikeleb
*******
Beiträge: 1400
Registriert: Fr, 09.12.2011 16:50

Re: Bedingte Formatierung

Beitrag von mikeleb »

Hallo,

Code: Alles auswählen

Dim oCondition(3) as New com.sun.star.beans.PropertyValue
Damit wird ein Array namens oCondition mit 4 Elementen (0, 1, 2, 3) dimensioniert, wobei jedes Element ein PropertyValue ist, d. h. au seinem Namen und einem Value besteht. Eine Bedingung muss ein solches Array sein
Mit (z. B.)

Code: Alles auswählen

		oCondition(0).Name = "Operator"
		oCondition(0).Value = com.sun.star.sheet.ConditionOperator.LESS
		oCondition(1).Name = "Formula1"
		oCondition(1).Value = "120"
		oCondition(2).Name = "Formula2"
		oCondition(2).Value = ""
		oCondition(3).Name = "StyleName"
		oCondition(3).Value = "warnGELB"
wird eine Bedingung definiert (wobei die Eigenschaft "Fomlula2" hier überflüssig wäre, aber bei der nächsten, aufgrund von BETWEEN, benötigt wird).
Mit

Code: Alles auswählen

oConditionalForm.addNew(oCondition)
wirt diese Bedingung dem Bereich hinzugefügt (daher ist es wichtig, dass zu Beginn zunächst alle Bedingungen gelöscht werden - man könnte aber auch gezielt einzelne entfernen).
Per Makro lassen sich daher beliebig viele Bedingungen setzen. Die Beschränkung auf 3 exisitiert nur in der Oberfläche von AOO. In LibreOffice ist diese Beschränkung längst gefallen.
In deinem konkreten Fall, werden für die nächsten Bedingungen nur die .Value geändert, da die Namen ja bereits im Rahmen der ersten Bedingung im Array gesetzt wurden.
Gruß,
mikeleb
Jörg
*****
Beiträge: 401
Registriert: Mo, 21.10.2013 12:05

Re: [Gelöst]Bedingte Formatierung

Beitrag von Jörg »

Hallo mikeleb,
vielen Dank für die Erläuterungen.
Gruß Jörg

Win 10 Pro AOO 4.1.15
Antworten