Conditional Formats

From Apache OpenOffice Wiki
Jump to: navigation, search

A cell can be formatted automatically with a conditional format, depending on its contents or the result of a formula. A conditional format consists of several condition entries that contain the condition and name of a cell style. The style of the first met condition, true or "not zero", is applied to the cell.


A cell or cell range object contains the properties ConditionalFormat and ConditionalFormatLocal. These properties return the interface of the conditional format container The objects of both properties are equal, except for the representation of formulas. The ConditionalFormatLocal property uses function names in the current language.

Documentation note.png After a conditional format is changed, it has to be reinserted into the property set of the cell or cell range.

A condition entry of a conditional format is represented by the service It implements two interfaces:

The service contains all format conditions and returns objects. The interface inserts new conditions and removes them.

  • The method addNew() inserts a new condition. It expects a sequence of objects. The following properties are supported:
    • Operator: A constant describing the operation to perform.
    • Formula1 and Formula2: Strings that contain the values or formulas to evaluate. Formula2 is used only if the property Operator contains BETWEEN or NOT_BETWEEN.
    • SourcePosition: A struct that contains the base address for relative cell references in formulas.
    • StyleName: The name of the cell style to apply.
  • The methods removeByIndex() removes the condition entry at the specified position.
  • The method clear() removes all condition entries.

The following example applies a conditional format to a cell range. It uses the cell style "MyNewCellStyle" that is applied to each cell containing a value greater than 1. The xSheet is the interface of a spreadsheet.

 // get the conditional format object of the cell range xCellRange = xSheet.getCellRangeByName("A1:B10"); xPropSet = (
     UnoRuntime.queryInterface(, xCellRange); xEntries =
     ( xPropSet.getPropertyValue("ConditionalFormat");
 // create a condition and apply it to the range[] aCondition = new[3];
 aCondition[0] = new;
 aCondition[0].Name = "Operator";
 aCondition[0].Value =;
 aCondition[1] = new;
 aCondition[1].Name = "Formula1";
 aCondition[1].Value = "1";
 aCondition[2] = new;
 aCondition[2].Name = "StyleName";
 aCondition[2].Value = "MyNewCellStyle";
 xPropSet.setPropertyValue("ConditionalFormat", xEntries); 
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages