diff --git a/ClosedXML/Excel/ConditionalFormats/Save/XLCFColorScaleConverter.cs b/ClosedXML/Excel/ConditionalFormats/Save/XLCFColorScaleConverter.cs index 5d970d5..6f38667 100644 --- a/ClosedXML/Excel/ConditionalFormats/Save/XLCFColorScaleConverter.cs +++ b/ClosedXML/Excel/ConditionalFormats/Save/XLCFColorScaleConverter.cs @@ -7,20 +7,26 @@ namespace ClosedXML.Excel { - internal class XLCFColorScaleConverter:IXLCFConverter + internal class XLCFColorScaleConverter : IXLCFConverter { public ConditionalFormattingRule Convert(IXLConditionalFormat cf, Int32 priority, XLWorkbook.SaveContext context) { var conditionalFormattingRule = new ConditionalFormattingRule { Type = cf.ConditionalFormatType.ToOpenXml(), Priority = priority }; - + var colorScale = new ColorScale(); - for(Int32 i = 1; i <= cf.Values.Count; i++) + for (Int32 i = 1; i <= cf.ContentTypes.Count; i++) { - var conditionalFormatValueObject = new ConditionalFormatValueObject { Type = cf.ContentTypes[i].ToOpenXml(), Val = cf.Values[i].Value }; + var type = cf.ContentTypes[i].ToOpenXml(); + var val = (cf.Values.ContainsKey(i) && cf.Values[i] != null) ? cf.Values[i].Value : null; + + var conditionalFormatValueObject = new ConditionalFormatValueObject { Type = type }; + if (val != null) + conditionalFormatValueObject.Val = val; + colorScale.Append(conditionalFormatValueObject); } - for (Int32 i = 1; i <= cf.Values.Count; i++) + for (Int32 i = 1; i <= cf.Colors.Count; i++) { Color color = new Color { Rgb = cf.Colors[i].Color.ToHex() }; colorScale.Append(color); diff --git a/ClosedXML/Excel/ConditionalFormats/XLCFColorScaleMax.cs b/ClosedXML/Excel/ConditionalFormats/XLCFColorScaleMax.cs index 64614d8..7fdf8de 100644 --- a/ClosedXML/Excel/ConditionalFormats/XLCFColorScaleMax.cs +++ b/ClosedXML/Excel/ConditionalFormats/XLCFColorScaleMax.cs @@ -22,7 +22,9 @@ } public void HighestValue(XLColor color) { - Maximum(XLCFContentType.Maximum, "0", color); + _conditionalFormat.Values.Add(null); + _conditionalFormat.Colors.Add(color); + _conditionalFormat.ContentTypes.Add(XLCFContentType.Maximum); } } } diff --git a/ClosedXML/Excel/ConditionalFormats/XLCFColorScaleMid.cs b/ClosedXML/Excel/ConditionalFormats/XLCFColorScaleMid.cs index 13c31ec..d868a73 100644 --- a/ClosedXML/Excel/ConditionalFormats/XLCFColorScaleMid.cs +++ b/ClosedXML/Excel/ConditionalFormats/XLCFColorScaleMid.cs @@ -30,7 +30,9 @@ } public void HighestValue(XLColor color) { - Midpoint(XLCFContentType.Maximum, "0", color); + _conditionalFormat.Values.Initialize(null); + _conditionalFormat.Colors.Add(color); + _conditionalFormat.ContentTypes.Add(XLCFContentType.Maximum); } } } diff --git a/ClosedXML/Excel/ConditionalFormats/XLCFColorScaleMin.cs b/ClosedXML/Excel/ConditionalFormats/XLCFColorScaleMin.cs index 0c33c04..9f2b647 100644 --- a/ClosedXML/Excel/ConditionalFormats/XLCFColorScaleMin.cs +++ b/ClosedXML/Excel/ConditionalFormats/XLCFColorScaleMin.cs @@ -27,7 +27,10 @@ public IXLCFColorScaleMid LowestValue(XLColor color) { - return Minimum(XLCFContentType.Minimum, "0", color); + _conditionalFormat.Values.Initialize(null); + _conditionalFormat.Colors.Initialize(color); + _conditionalFormat.ContentTypes.Initialize(XLCFContentType.Minimum); + return new XLCFColorScaleMid(_conditionalFormat); } } } diff --git a/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/Excel/XLWorkbook_Load.cs index d3b05dc..b16b063 100644 --- a/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/Excel/XLWorkbook_Load.cs @@ -1534,6 +1534,8 @@ conditionalFormat.ContentTypes.Add(c.Type.Value.ToClosedXml()); if (c.Val != null) conditionalFormat.Values.Add(new XLFormula { Value = c.Val.Value }); + else + conditionalFormat.Values.Add(null); if (c.GreaterThanOrEqual != null) conditionalFormat.IconSetOperators.Add(c.GreaterThanOrEqual.Value ? XLCFIconSetOperator.EqualOrGreaterThan : XLCFIconSetOperator.GreaterThan);