Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

Created Unassigned: Conditional Formatting Compatibility with SpreadsheetGear [9367]

$
0
0
I need to create 3 conditional formats for the same cell. If a value is below a limit, the cell is shaded red. Equal to the limit gets yellow. Above the limit gets green. Following is a sample method that does this.

private void SetQualityFormat(IXLWorksheet ws)
{
IXLRange rng = ws.Range(3,6,10,6);
float cond1 = 16.0f;
XLColor color1 = XLColor.Red;
XLColor color2 = XLColor.LightYellow;
XLColor color3 = XLColor.LightGreen;

rng.AddConditionalFormat().WhenLessThan(cond1).Fill.SetBackgroundColor(color1);
rng.AddConditionalFormat().WhenEquals(cond1).Fill.SetBackgroundColor(color2);
rng.AddConditionalFormat().WhenGreaterThan(cond1).Fill.SetBackgroundColor(color3);
}

The above code creates conditional formats that work fine with Excel. If the resulting spreadsheet is loaded into a SpreadsheetGear control, the conditional formatting doesn't show up. The problem is in the way ClosedXML generates the Open XML for the conditional formatting, which appears as follows:

```
<conditionalFormatting sqref="F3:F10">
<cfRule type="cellIs" dxfId="60" priority="1" operator="lessThan">
<formula>16</formula>
</cfRule>
</conditionalFormatting>
<conditionalFormatting sqref="F3:F10">
<cfRule type="cellIs" dxfId="59" priority="2" operator="equal">
<formula>16</formula>
</cfRule>
</conditionalFormatting>
<conditionalFormatting sqref="F3:F10">
<cfRule type="cellIs" dxfId="58" priority="3" operator="greaterThan">
<formula>16</formula>
</cfRule>
</conditionalFormatting>
```

SpreadsheetGear interprets this as three overlapping rules, which are not supported by the SG control.

If I use Excel to create similar rules in a spreadsheet, they get generated as follows, which SpreadsheetGear handles just fine.

```
<conditionalFormatting sqref="F3:F10">
<cfRule type="cellIs" dxfId="60" priority="1" operator="lessThan">
<formula>16</formula>
</cfRule>
<cfRule type="cellIs" dxfId="59" priority="2" operator="equal">
<formula>16</formula>
</cfRule>
<cfRule type="cellIs" dxfId="58" priority="3" operator="greaterThan">
<formula>16</formula>
</cfRule>
</conditionalFormatting>
```

Any idea why ClosedXML generates the Open XML differently? Is there a way to code the rules that will cause ClosedXML to generate the second sample of Open XML?

I would think chaining the rules on a single range element would work, but ClosedXML doesn't like the following:

```
rng.AddConditionalFormat().WhenLessThan(cond1).Fill.SetBackgroundColor(color1).
WhenEquals(cond1).Fill.SetBackgroundColor(color2).
WhenGreaterThan(cond1).Fill.SetBackgroundColor(color3);
```

Is there a way to chain the rules using ClosedXML? Or any other way to get the desired output?

Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>