Document Solutions for Excel, Java Edition | Document Solutions
Features / Conditional Formatting / Color Scale Rule
In This Topic
    Color Scale Rule
    In This Topic

    The color scale rule uses a sliding color scale to format cells or a range of cells. For instance, if numeric cell value 1 is represented with color yellow and 50 with green, then 25 would be light green. This rule can be added using the methods of the IColorScale interface.

    Refer to the following example code to add color scale rule to a cell range in a worksheet.

    Java
    Copy Code
    // Adding Color Scale Rule 
    IColorScale twoColorScaleRule = worksheet.getRange("A2:E2").getFormatConditions().addColorScale(ColorScaleType.TwoColorScale);
    
    worksheet.getRange("A2").setValue(1);
    worksheet.getRange("B2").setValue(2);
    worksheet.getRange("C2").setValue(3);
    worksheet.getRange("D2").setValue(4);
    worksheet.getRange("E2").setValue(5);
    
    twoColorScaleRule.getColorScaleCriteria().get(0).setType(ConditionValueTypes.Number);
    twoColorScaleRule.getColorScaleCriteria().get(0).setValue(1);
    twoColorScaleRule.getColorScaleCriteria().get(0).getFormatColor().setColor(Color.FromArgb(255,0,0));
    
    twoColorScaleRule.getColorScaleCriteria().get(1).setType(ConditionValueTypes.Number); 
    twoColorScaleRule.getColorScaleCriteria().get(1).setValue(5);
    twoColorScaleRule.getColorScaleCriteria().get(1).getFormatColor().setColor(Color.FromArgb(0,255,0));