[]
此集合提供对应用于区域的条件格式规则的访问。使用它可以检查现有规则,或添加单元格值、表达式、色阶、数据条、图标集、前 10 项和高于平均值条件等新规则。
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{1}, {3}, {5}, {7}, {9}});
IFormatConditions formatConditions = range.getFormatConditions();
formatConditions.add(FormatConditionType.CellValue, FormatConditionOperator.Between, 1, 5);
add(FormatConditionType type) add(FormatConditionType type,
FormatConditionOperator formatConditionOperator,
Object formula1,
Object formula2) IAboveAverage 对象,该对象表示指定范围的条件格式规则。addColorScale(ColorScaleType colorScaleType) IColorScale 对象,表示使用单元格颜色渐变来指示选定区域中单元格值相对差异的条件格式规则。IDataBar 对象。IIconSetCondition 对象,表示指定区域的图标集条件格式规则。addTop10()ITop10 对象,表示指定范围的条件格式规则。IUniqueValues 对象,表示指定区域的条件格式规则。voiddelete()voidget(int index) intgetCount()toJson()此集合包含与某个范围关联的条件格式。
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{1}, {3}, {5}, {7}, {9}});
IFormatCondition condition = (IFormatCondition)range.getFormatConditions().add(FormatConditionType.CellValue, FormatConditionOperator.Between, 1, 5);
condition.getInterior().setColor(Color.GetYellow());
IFormatCondition condition1 = (IFormatCondition)range.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1>6", null);
condition1.getInterior().setColor(Color.GetLightBlue());
int count = range.getFormatConditions().getCount();
使用此方法可访问集合中某个区域的现有条件格式。
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{1}, {3}, {5}, {7}, {9}});
IFormatCondition condition = (IFormatCondition) range.getFormatConditions().add(FormatConditionType.CellValue, FormatConditionOperator.Between, 1, 5);
condition.getInterior().setColor(Color.GetYellow());
Object condition = range.getFormatConditions().get(0);
index - 条件格式在集合中的零基索引。IllegalArgumentException - 如果 index 小于 0 或大于等于 getCount()。创建指定类型的条件格式规则,并将其添加到此集合中。
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{1}, {3}, {5}, {7}, {9}});
IAboveAverage condition = (IAboveAverage)range.getFormatConditions().add(FormatConditionType.AboveAverageCondition);
condition.getInterior().setColor(Color.GetYellow());
type - 指定新条件格式的类型。创建指定类型的条件格式规则,并将其添加到此集合中。
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{1}, {3}, {5}, {7}, {9}});
IFormatCondition condition = (IFormatCondition)range.getFormatConditions().add(
FormatConditionType.CellValue,
FormatConditionOperator.Between,
1,
5
);
condition.getInterior().setColor(Color.GetYellow());
type - 指定新条件格式的类型。formatConditionOperator - 指定条件格式的运算符。对于 FormatConditionType.CellValue,此值不得为 FormatConditionOperator.None。formula1 - 指定条件格式的第一个公式。对于 FormatConditionType.CellValue,此值不得为 null。formula2 - 指定条件格式的第二个公式。对于 FormatConditionOperator.Between 和 FormatConditionOperator.NotBetween,此值不得为 null。IllegalArgumentException - 如果指定的条件格式类型缺少必需参数。UnsupportedOperationException - 如果 formula1 或 formula2 使用了不受支持的表公式或引用公式。IAboveAverage 对象,该对象表示指定范围的条件格式规则。使用返回的对象可配置基于平均值的条件格式规则,例如将单元格评估为高于平均值还是低于平均值。
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{1}, {2}, {3}, {4}, {10}});
IAboveAverage aboveAverage = range.getFormatConditions().addAboveAverage();
aboveAverage.getInterior().setColor(Color.GetYellow());
aboveAverage.setAboveBelow(AboveBelow.AboveAverage);
IAboveAverage 对象。IColorScale 对象,表示使用单元格颜色渐变来指示选定区域中单元格值相对差异的条件格式规则。使用 colorScaleType 指定要添加的色阶规则类型。
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{1}, {2}, {3}, {4}, {5}});
IColorScale colorScale = range.getFormatConditions().addColorScale(ColorScaleType.TwoColorScale);
colorScale.getColorScaleCriteria().get(0).getFormatColor().setColor(Color.GetRed());
colorScale.getColorScaleCriteria().get(1).getFormatColor().setColor(Color.GetGreen());
colorScaleType - 要添加的色阶类型。IColorScale 对象。IDataBar 对象。使用返回的规则可配置数据条如何可视化区域中的相对单元格值,例如条形填充类型、颜色、方向以及是否显示单元格值。
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{1}, {2}, {3}, {4}, {5}});
IDataBar dataBar = range.getFormatConditions().addDatabar();
dataBar.setBarFillType(DataBarFillType.Solid);
IDataBar 对象。IIconSetCondition 对象,表示指定区域的图标集条件格式规则。使用返回的规则可选择图标集,并配置用于确定每个单元格值显示哪个图标的阈值条件。
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{1}, {2}, {3}, {4}, {5}});
IIconSetCondition condition = range.getFormatConditions().addIconSetCondition();
condition.setIconSet(workbook.getIconSets().get(IconSetType.Icon3Symbols));
condition.getIconCriteria().get(1).setValue(50);
IIconSetCondition 对象。ITop10 对象,表示指定范围的条件格式规则。使用返回的规则可配置条件格式是评估范围中的最高值还是最低值,以及排名是基于计数还是百分比。
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{1}, {2}, {3}, {4}, {5}});
ITop10 top10 = range.getFormatConditions().addTop10();
top10.getInterior().setColor(Color.GetYellow());
top10.setTopBottom(TopBottom.Top);
top10.setRank(3);
ITop10 对象。IUniqueValues 对象,表示指定区域的条件格式规则。返回的规则会添加到此集合,并配置为计算所表示区域中的唯一值。
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{100}, {100}, {200}, {300}, {300}});
IUniqueValues condition = range.getFormatConditions().addUniqueValues();
condition.getInterior().setColor(Color.GetYellow());
IUniqueValues 对象。使用此方法可从该范围的 IFormatConditions 集合中移除所有条件格式规则。
IRange range = worksheet.getRange("A1:A5");
range.setValue(new Object[][] {{1}, {3}, {5}, {7}, {9}});
IFormatCondition condition = (IFormatCondition)range.getFormatConditions().add(FormatConditionType.CellValue, FormatConditionOperator.Between, 1, 5);
condition.getInterior().setColor(Color.GetYellow());
range.getFormatConditions().delete();
此方法会先清除当前区域中现有的条件格式,然后应用指定 JSON 字符串中的新条件格式。
条件格式将在调用该方法的区域中创建。JSON 字符串中包含的区域信息不会保留。
IRange range = worksheet.getRange("E2:E7");
range.setValue(new Object[][] {{67}, {62}, {72}, {66}, {76}, {68}});
range.getFormatConditions().fromJson(
"{\"rules\":[{\"ruleType\":1,\"operator\":6,\"stopIfTrue\":true,\"ranges\":[{\"row\":1,\"rowCount\":6,\"col\":4,\"colCount\":1}],\"value1\":\"66\",\"value2\":\"70\"}]}"
);
json - 定义条件格式的 JSON 字符串。不支持 null。使用此方法可序列化与该范围关联的所有条件格式规则,以便存储或稍后使用 fromJson(String) 恢复。
worksheet.getRange("A1:A5").setValue(new Object[][] {
{10}, {30}, {50}, {70}, {90}
});
IFormatConditions conditions = worksheet.getRange("A1:A5").getFormatConditions();
ITop10 top10 = conditions.addTop10();
top10.getInterior().setColor(Color.GetYellow());
top10.setRank(3);
String json = conditions.toJson();