[]
An IFormatCondition defines a conditional formatting rule for a worksheet range, including the rule type, the range to which the rule applies, and the formatting to use when the rule condition is met.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1", null);
condition.getInterior().setColor(Color.GetYellow());
condition.getBorders().setColor(Color.GetRed());
voiddelete()voidIRange object that specifies the cell range to which the formatting rule is applied.IBorders which represents the borders of this conditional format.TimePeriods date operator used in the format condition.getFont()IFont which represents the font of this conditional format.IInterior which represents the background color of this conditional format.true.intbooleantrue.getText()ContainsOperator enumeration, specifying the text search performed by the conditional formatting rule.getType()voidsetAppliesTo(IRange value) voidsetDateOperator(TimePeriods value) TimePeriods date operator used in the format condition.void1 so that it will be evaluated before all other rules on the worksheet.voidsetFormula1(Object value) voidsetFormula2(Object value) voidvoidsetNumberFormat(String value) true.voidvoidsetPriority(int value) voidsetStopIfTrue(boolean value) true.voidvoidsetTextOperator(ContainsOperator value) ContainsOperator enumeration, specifying the text search performed by the conditional formatting rule.toJson()IRange object that specifies the cell range to which the formatting rule is applied. Use setAppliesTo(IRange) to change the range associated with this conditional formatting rule.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1", null);
condition.getInterior().setColor(Color.GetYellow());
IRange appliesTo = condition.getAppliesTo();
IRange object that specifies the cell range to which the formatting rule is applied.Use this method to change the IRange associated with the current conditional formatting rule.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1", null);
condition.getInterior().setColor(Color.GetYellow());
condition.setAppliesTo(worksheet.getRange("A1:A5"));
value - The IRange object to which this formatting rule is applied. Must not be null.IBorders which represents the borders of this conditional format.Use the returned border collection to access or modify the border settings stored on the conditional formatting rule.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1", null);
condition.getInterior().setColor(Color.GetYellow());
IBorders borders = condition.getBorders();
borders.setColor(Color.GetRed());
IBorders that represents the borders of this conditional format.TimePeriods date operator used in the format condition.For a time period conditional formatting rule, this value indicates the relative date period used by the rule.
worksheet.getRange("A1").setFormula("=TODAY()-1");
worksheet.getRange("B1").setFormula("=TODAY()");
worksheet.getRange("C1").setFormula("=TODAY()+1");
IFormatCondition condition = (IFormatCondition) worksheet.getRange("A1:C1")
.getFormatConditions().add(FormatConditionType.TimePeriod, FormatConditionOperator.None, null, null);
condition.getInterior().setColor(Color.GetYellow());
condition.setDateOperator(TimePeriods.Today);
TimePeriods dateOperator = condition.getDateOperator();
TimePeriods date operator used in the format condition.TimePeriods date operator used in the format condition.Use this method with a conditional formatting rule whose type is FormatConditionType.TimePeriod to specify which relative date period the rule evaluates, such as today, tomorrow, or the last 7 days.
worksheet.getRange("A1").setFormula("=TODAY()-1");
worksheet.getRange("B1").setFormula("=TODAY()");
worksheet.getRange("C1").setFormula("=TODAY()+1");
IFormatCondition condition = (IFormatCondition) worksheet.getRange("A1:C1")
.getFormatConditions().add(FormatConditionType.TimePeriod, FormatConditionOperator.None, null, null);
condition.getInterior().setColor(Color.GetYellow());
condition.setDateOperator(TimePeriods.Today);
value - The TimePeriods value that specifies the date period to use in the format condition.IFont which represents the font of this conditional format.Use the returned font object to configure font settings that are applied when the conditional formatting rule is triggered.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1", null);
IFont font = condition.getFont();
font.setBold(true);
IFont instance that represents the font of this conditional format.The returned value represents the first comparison value or expression associated with the conditional formatting rule. Depending on the rule type, it can be a constant value, a string value, a cell reference, or a formula.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1>0", null);
condition.getInterior().setColor(Color.GetYellow());
Object formula1 = condition.getFormula1();
The set value represents the first comparison value or expression associated with the conditional formatting rule. Depending on the rule type, it can be a constant value, a string, a cell reference, or a formula.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1>0", null);
condition.getInterior().setColor(Color.GetYellow());
condition.setFormula1("=A1>0");
value - The first formula, value, cell reference, or expression associated with this conditional format.This property is used by conditional formatting rules that store two formulas, such as FormatConditionType.CellValue rules that use the FormatConditionOperator.Between or FormatConditionOperator.NotBetween operator.
worksheet.getRange("A1:A5").setValue(new Object[][] {{1}, {3}, {5}, {7}, {9}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("A1:A5")
.getFormatConditions().add(FormatConditionType.CellValue, FormatConditionOperator.Between, "2", "8");
condition.getInterior().setColor(Color.GetYellow());
Object formula2 = condition.getFormula2();
null if the rule does not use a second formula.This property is used by conditional formatting rules that store two formulas, such as FormatConditionType.CellValue rules that use the FormatConditionOperator.Between or FormatConditionOperator.NotBetween operator.
worksheet.getRange("A1:A5").setValue(new Object[][] {{1}, {3}, {5}, {7}, {9}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("A1:A5")
.getFormatConditions().add(FormatConditionType.CellValue, FormatConditionOperator.Between, "2", "8");
condition.getInterior().setColor(Color.GetYellow());
condition.setFormula2(100);
value - The second formula of this conditional format. Sets null if the rule does not use a second formula.IInterior which represents the background color of this conditional format. Use the returned IInterior object to read or modify the fill settings applied when the conditional formatting rule is in effect.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1", null);
IInterior interior = condition.getInterior();
interior.setColor(Color.GetRed());
IInterior instance that represents the background color of this conditional format.true. Returns the format string assigned through setNumberFormat(String) for this conditional formatting rule.
worksheet.getRange("A1:A3").setValue(new Object[][] {{85}, {95}, {105}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("A1:A3")
.getFormatConditions().add(FormatConditionType.CellValue, FormatConditionOperator.Greater, "90", null);
condition.getInterior().setColor(Color.GetYellow());
condition.setNumberFormat("0.00");
String numberFormat = condition.getNumberFormat();
true.true.
worksheet.getRange("A1:A3").setValue(new Object[][] {{85}, {95}, {105}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("A1:A3")
.getFormatConditions().add(FormatConditionType.CellValue, FormatConditionOperator.Greater, "90", null);
condition.getInterior().setColor(Color.GetYellow());
condition.setNumberFormat("0.00");
value - The number format applied to a cell when the conditional formatting rule evaluates to true.The operator specifies how the conditional formatting rule compares the cell value with the rule's formula values. For example, it can indicate comparisons such as equal, greater than, or between.
worksheet.getRange("A1:A3").setValue(new Object[][] {{1}, {3}, {5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("A1:A3")
.getFormatConditions().add(FormatConditionType.CellValue, FormatConditionOperator.Between, "2", "4");
condition.getInterior().setColor(Color.GetYellow());
FormatConditionOperator operator = condition.getOperator();
Use this method to change the comparison operator that the conditional formatting rule uses.
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{10}, {30}, {50}, {70}, {90}});
IFormatCondition condition = (IFormatCondition) range.getFormatConditions().add(
FormatConditionType.CellValue, FormatConditionOperator.Between, 30, 70);
condition.getInterior().setColor(Color.GetYellow());
condition.setOperator(FormatConditionOperator.NotBetween);
value - The conditional formatting operator to apply.A lower priority value indicates that the rule is evaluated earlier. Use setPriority(int), setFirstPriority(), or setLastPriority() to change the evaluation order.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1", null);
condition.getInterior().setColor(Color.GetYellow());
int priority = condition.getPriority();
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1", null);
condition.getInterior().setColor(Color.GetYellow());
condition.setPriority(1);
value - The priority value of the conditional formatting rule.true. If this method returns true, lower-priority formatting rules on the same cell are not evaluated after the current rule evaluates to true.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1>0", null);
condition.getInterior().setColor(Color.GetYellow());
condition.setStopIfTrue(true);
boolean stopIfTrue = condition.getStopIfTrue();
true if additional formatting rules on the cell should not be evaluated after the current rule evaluates to true; otherwise, false.true. If this method Sets true, lower-priority formatting rules on the same cell are not evaluated after the current rule evaluates to true.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1>0", null);
condition.getInterior().setColor(Color.GetYellow());
condition.setStopIfTrue(true);
value - true if additional formatting rules on the cell should not be evaluated after the current rule evaluates to true; otherwise, false.Use this method to configure the text for a text-based conditional formatting rule. This value works together with getTextOperator() to define how the rule searches for matching text.
worksheet.getRange("A1:A3").setValue(new Object[][] {{"Pending"}, {"Done"}, {"Pending Review"}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("A1:A3")
.getFormatConditions().add(FormatConditionType.TextString, FormatConditionOperator.None, null, null);
condition.getInterior().setColor(Color.GetYellow());
condition.setText("Pending");
String text = condition.getText();
Use this method to configure the text for a text-based conditional formatting rule. This value works together with getTextOperator() to define how the rule searches for matching text.
worksheet.getRange("A1:A3").setValue(new Object[][] {{"Pending"}, {"Done"}, {"Pending Review"}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("A1:A3")
.getFormatConditions().add(FormatConditionType.TextString, FormatConditionOperator.None, null, null);
condition.getInterior().setColor(Color.GetYellow());
condition.setText("Pending");
value - The text used by the conditional formatting rule.ContainsOperator enumeration, specifying the text search performed by the conditional formatting rule. Use this method with a FormatConditionType.TextString conditional formatting rule to determine whether the rule checks whether cell text contains, does not contain, begins with, or ends with the specified text.
worksheet.getRange("A1:A4").setValue(new Object[][] {{"Apple"}, {"Pear"}, {"Apricot"}, {"Plum"}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("A1:A4")
.getFormatConditions().add(FormatConditionType.TextString);
condition.getInterior().setColor(Color.GetYellow());
condition.setText("Ap");
condition.setTextOperator(ContainsOperator.BeginsWith);
ContainsOperator textOperator = condition.getTextOperator();
ContainsOperator constants that specifies the text search performed by the conditional formatting rule.ContainsOperator enumeration, specifying the text search performed by the conditional formatting rule. Use this method with a FormatConditionType.TextString conditional formatting rule to control whether the rule checks if cell text contains or does not contain the specified text, or begins or ends with it.
worksheet.getRange("A1:A4").setValue(new Object[][] {{"Apple"}, {"Pear"}, {"Apricot"}, {"Plum"}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("A1:A4")
.getFormatConditions().add(FormatConditionType.TextString);
condition.getInterior().setColor(Color.GetYellow());
condition.setText("Ap");
condition.setTextOperator(ContainsOperator.BeginsWith);
value - One of the ContainsOperator constants that specifies the text search performed by the conditional formatting rule. null is not supported.The returned value is one of the constants in FormatConditionType and indicates which kind of conditional formatting rule this object represents.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1", null);
condition.getInterior().setColor(Color.GetYellow());
FormatConditionType type = condition.getType();
Use this method to remove the current conditional formatting rule from the worksheet.
worksheet.getRange("A1:B5").setValue(new Object[][] {{1, 2}, {0, 1}, {0, 0}, {0, 3}, {4, 5}});
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1", null);
condition.getInterior().setColor(Color.GetYellow());
condition.delete();
1 so that it will be evaluated before all other rules on the worksheet. Use this method to move the current conditional formatting rule to the highest evaluation priority in the worksheet.
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{10}, {30}, {50}, {70}, {90}});
IFormatCondition firstCondition = (IFormatCondition) range.getFormatConditions().add(FormatConditionType.CellValue, FormatConditionOperator.Greater, 60, null);
firstCondition.getInterior().setColor(Color.GetYellow());
IFormatCondition condition = (IFormatCondition) range.getFormatConditions().add(
FormatConditionType.CellValue, FormatConditionOperator.Between, 30, 70);
condition.getInterior().setColor(Color.GetLightBlue());
condition.setFirstPriority();
Use this method to move the current rule to the lowest evaluation priority among the conditional formatting rules on the worksheet.
worksheet.getRange("A1:B5").setValue(new Object[][] {
{10, 5}, {30, 15}, {20, 25}, {50, 35}, {40, 45}
});
IFormatCondition firstCondition = (IFormatCondition) worksheet.getRange("A1:A5")
.getFormatConditions().add(FormatConditionType.CellValue, FormatConditionOperator.Greater, 25, null);
firstCondition.getInterior().setColor(Color.GetYellow());
IFormatCondition secondCondition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.CellValue, FormatConditionOperator.Less, 30, null);
secondCondition.getInterior().setColor(Color.GetLightBlue());
secondCondition.setLastPriority();
Use this method to restore a conditional formatting rule from a JSON string, such as one previously generated by toJson().
worksheet.getRange("A1:A5").setValue(new Object[][] {{1}, {2}, {3}, {4}, {5}});
String json = "{\"ranges\":[{\"row\":0,\"col\":1,\"rowCount\":5,\"colCount\":1}],\"priority\":1,\"ruleType\":1,\"operator\":2,\"value1\":\"3\",\"value2\":\"\"}";
IFormatCondition condition = (IFormatCondition) worksheet.getRange("B1:B5")
.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=TRUE", null);
condition.fromJson(json);
condition.getInterior().setColor(Color.GetYellow());
json - The JSON string that defines the conditional format.The returned string represents the current conditional formatting rule and can be used with fromJson(String) to recreate the rule.
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{10}, {30}, {50}, {70}, {90}});
IFormatCondition condition = (IFormatCondition) range.getFormatConditions().add(
FormatConditionType.CellValue, FormatConditionOperator.Greater, 60, null);
condition.getInterior().setColor(Color.GetYellow());
String json = condition.toJson();