[]
        
首页
开发者学堂
文档
论坛
市场
生态机会
活动
立即试用
(Showing Draft Content)

IFormatConditions

Interface IFormatConditions


public interface IFormatConditions
表示与区域关联的条件格式。

此集合提供对应用于区域的条件格式规则的访问。使用它可以检查现有规则,或添加单元格值、表达式、色阶、数据条、图标集、前 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);
 
  • Method Details

    • getCount

      int 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());
       IFormatCondition condition1 = (IFormatCondition)range.getFormatConditions().add(FormatConditionType.Expression, FormatConditionOperator.None, "=A1>6", null);
       condition1.getInterior().setColor(Color.GetLightBlue());
       int count = range.getFormatConditions().getCount();
       
      Returns:
      集合中条件格式对象的数量。
    • get

      Object get(int index)
      返回指定零基索引处的条件格式。

      使用此方法可访问集合中某个区域的现有条件格式。

      
       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);
       
      Parameters:
      index - 条件格式在集合中的零基索引。
      Returns:
      指定的条件格式。
      Throws:
      IllegalArgumentException - 如果 index 小于 0 或大于等于 getCount()。
    • add

      添加一个新的条件格式。

      创建指定类型的条件格式规则,并将其添加到此集合中。

      
       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());
       
      Parameters:
      type - 指定新条件格式的类型。
      Returns:
      新的条件格式。
    • add

      Object add(FormatConditionType type, FormatConditionOperator formatConditionOperator, Object formula1, Object formula2)
      添加一个新的条件格式。

      创建指定类型的条件格式规则,并将其添加到此集合中。

      
       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());
       
      Parameters:
      type - 指定新条件格式的类型。
      formatConditionOperator - 指定条件格式的运算符。对于 FormatConditionType.CellValue,此值不得为 FormatConditionOperator.None。
      formula1 - 指定条件格式的第一个公式。对于 FormatConditionType.CellValue,此值不得为 null。
      formula2 - 指定条件格式的第二个公式。对于 FormatConditionOperator.Between 和 FormatConditionOperator.NotBetween,此值不得为 null。
      Returns:
      新的条件格式。
      Throws:
      IllegalArgumentException - 如果指定的条件格式类型缺少必需参数。
      UnsupportedOperationException - 如果 formula1 或 formula2 使用了不受支持的表公式或引用公式。
    • addAboveAverage

      IAboveAverage addAboveAverage()
      返回新的 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);
       
      Returns:
      新的 IAboveAverage 对象。
    • addColorScale

      IColorScale addColorScale(ColorScaleType colorScaleType)
      返回新的 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());
       
      Parameters:
      colorScaleType - 要添加的色阶类型。
      Returns:
      新的 IColorScale 对象。
    • addDatabar

      IDataBar addDatabar()
      返回表示指定区域的数据条条件格式规则的 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);
       
      Returns:
      新的 IDataBar 对象。
    • addIconSetCondition

      IIconSetCondition addIconSetCondition()
      返回新的 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);
       
      Returns:
      新的 IIconSetCondition 对象。
    • addTop10

      ITop10 addTop10()
      返回 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);
       
      Returns:
      新的 ITop10 对象。
    • addUniqueValues

      IUniqueValues addUniqueValues()
      返回新的 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());
       
      Returns:
      新条件格式规则的 IUniqueValues 对象。
    • delete

      void delete()
      删除与所表示范围相关联的所有条件格式。

      使用此方法可从该范围的 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();
       
    • fromJson

      void fromJson(String json)
      根据 JSON 字符串生成条件格式。

      此方法会先清除当前区域中现有的条件格式,然后应用指定 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\"}]}"
       );
       
      Parameters:
      json - 定义条件格式的 JSON 字符串。不支持 null。
    • toJson

      String toJson()
      从条件格式生成一个 JSON 字符串。

      使用此方法可序列化与该范围关联的所有条件格式规则,以便存储或稍后使用 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();
       
      Returns:
      条件格式的 JSON 字符串。