diff --git a/ClosedXML/ClosedXML.csproj b/ClosedXML/ClosedXML.csproj index f4d6ebf..4121264 100644 --- a/ClosedXML/ClosedXML.csproj +++ b/ClosedXML/ClosedXML.csproj @@ -98,6 +98,7 @@ + diff --git a/ClosedXML/Excel/ConditionalFormats/IXLConditionalFormat.cs b/ClosedXML/Excel/ConditionalFormats/IXLConditionalFormat.cs index ce97e2a..8428f7e 100644 --- a/ClosedXML/Excel/ConditionalFormats/IXLConditionalFormat.cs +++ b/ClosedXML/Excel/ConditionalFormats/IXLConditionalFormat.cs @@ -62,7 +62,6 @@ public enum XLCFOperator { Equal, NotEqual, GreaterThan, LessThan, EqualOrGreaterThan, EqualOrLessThan, Between, NotBetween, Contains, NotContains, StartsWith, EndsWith } public interface IXLConditionalFormat { - String Name { get; set; } IXLStyle Style { get; set; } IXLStyle WhenIsBlank(); @@ -115,11 +114,11 @@ XLDictionary Colors { get; } XLDictionary ContentTypes { get; } XLDictionary IconSetOperators { get; } - + XLCFOperator Operator { get; } Boolean Bottom { get; } Boolean Percent { get; } - - + + } } diff --git a/ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverter.cs b/ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverter.cs index d9da914..7edeb9c 100644 --- a/ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverter.cs +++ b/ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverter.cs @@ -4,6 +4,7 @@ using System.Text; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Spreadsheet; +using ClosedXML.Extensions; namespace ClosedXML.Excel { @@ -48,7 +49,10 @@ ConditionalFormattingRuleExtension conditionalFormattingRuleExtension = new ConditionalFormattingRuleExtension { Uri = "{B025F937-C7B1-47D3-B67F-A62EFF666E3E}" }; conditionalFormattingRuleExtension.AddNamespaceDeclaration("x14", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"); - DocumentFormat.OpenXml.Office2010.Excel.Id id = new DocumentFormat.OpenXml.Office2010.Excel.Id { Text = cf.Name }; + DocumentFormat.OpenXml.Office2010.Excel.Id id = new DocumentFormat.OpenXml.Office2010.Excel.Id + { + Text = (cf as XLConditionalFormat).Id.WrapInBraces() + }; conditionalFormattingRuleExtension.Append(id); conditionalFormattingRuleExtensionList.Append(conditionalFormattingRuleExtension); diff --git a/ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverterExtension.cs b/ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverterExtension.cs index 7d489de..703d006 100644 --- a/ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverterExtension.cs +++ b/ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverterExtension.cs @@ -1,8 +1,5 @@ -using DocumentFormat.OpenXml; -//using DocumentFormat.OpenXml.Spreadsheet; +using ClosedXML.Extensions; using DocumentFormat.OpenXml.Office2010.Excel; -using System; -using System.Collections.Generic; namespace ClosedXML.Excel { @@ -17,7 +14,7 @@ ConditionalFormattingRule conditionalFormattingRule = new ConditionalFormattingRule() { Type = DocumentFormat.OpenXml.Spreadsheet.ConditionalFormatValues.DataBar, - Id = cf.Name + Id = (cf as XLConditionalFormat).Id.WrapInBraces() }; DataBar dataBar = new DataBar() diff --git a/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs b/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs index a408063..b42905d 100644 --- a/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs +++ b/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs @@ -7,10 +7,9 @@ { internal class XLConditionalFormat : IXLConditionalFormat, IXLStylized { - public XLConditionalFormat(XLRange range, Boolean copyDefaultModify = false) { - Name = string.Concat("{", Guid.NewGuid().ToString(), "}"); + Id = Guid.NewGuid(); Range = range; Style = new XLStyle(this, range.Worksheet.Style); Values = new XLDictionary(); @@ -19,29 +18,30 @@ IconSetOperators = new XLDictionary(); CopyDefaultModify = copyDefaultModify; } - public XLConditionalFormat(XLConditionalFormat other) + + public XLConditionalFormat(XLConditionalFormat conditionalFormat) { - Name = string.Concat("{", Guid.NewGuid().ToString(), "}"); - Range = other.Range; - Style = new XLStyle(this, other.Style); - Values = new XLDictionary(other.Values); - Colors = new XLDictionary(other.Colors); - ContentTypes = new XLDictionary(other.ContentTypes); - IconSetOperators = new XLDictionary(other.IconSetOperators); + Id = Guid.NewGuid(); + Range = conditionalFormat.Range; + Style = new XLStyle(this, conditionalFormat.Style); + Values = new XLDictionary(conditionalFormat.Values); + Colors = new XLDictionary(conditionalFormat.Colors); + ContentTypes = new XLDictionary(conditionalFormat.ContentTypes); + IconSetOperators = new XLDictionary(conditionalFormat.IconSetOperators); - ConditionalFormatType = other.ConditionalFormatType; - TimePeriod = other.TimePeriod; - IconSetStyle = other.IconSetStyle; - Operator = other.Operator; - Bottom = other.Bottom; - Percent = other.Percent; - ReverseIconOrder = other.ReverseIconOrder; - ShowIconOnly = other.ShowIconOnly; - ShowBarOnly = other.ShowBarOnly; + ConditionalFormatType = conditionalFormat.ConditionalFormatType; + TimePeriod = conditionalFormat.TimePeriod; + IconSetStyle = conditionalFormat.IconSetStyle; + Operator = conditionalFormat.Operator; + Bottom = conditionalFormat.Bottom; + Percent = conditionalFormat.Percent; + ReverseIconOrder = conditionalFormat.ReverseIconOrder; + ShowIconOnly = conditionalFormat.ShowIconOnly; + ShowBarOnly = conditionalFormat.ShowBarOnly; } - public String Name { get; set; } + public Guid Id { get; internal set; } public Boolean CopyDefaultModify { get; set; } private IXLStyle _style; private Int32 _styleCacheId; diff --git a/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/Excel/XLWorkbook_Load.cs index 9db3a92..d00a525 100644 --- a/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/Excel/XLWorkbook_Load.cs @@ -1,5 +1,6 @@ #region +using ClosedXML.Extensions; using ClosedXML.Utils; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; @@ -1875,12 +1876,11 @@ var dataBar = fr.Elements().First(); if (dataBar.ShowValue != null) conditionalFormat.ShowBarOnly = !dataBar.ShowValue.Value; - var id = fr.Descendants().FirstOrDefault(); - if (id != null) - { - conditionalFormat.Name = id.Text; - } + var id = fr.Descendants().FirstOrDefault(); + if (id != null && id.Text != null && !String.IsNullOrWhiteSpace(id.Text)) + conditionalFormat.Id = Guid.Parse(id.Text.Substring(1, id.Text.Length - 2)); + ExtractConditionalFormatValueObjects(conditionalFormat, dataBar); } else if (fr.Elements().Any()) @@ -1922,24 +1922,22 @@ return; } - foreach (var conditionalFormattingRule in - from r in extensions.Descendants() - where r.Type == ConditionalFormatValues.DataBar - select r) + foreach (var conditionalFormattingRule in extensions + .Descendants() + .Where(cf => + cf.Type != null + && cf.Type.HasValue + && cf.Type.Value == ConditionalFormatValues.DataBar)) { - IXLConditionalFormat xLConditionalFormat = ( - from cf in ws.ConditionalFormats - where cf.Name == conditionalFormattingRule.Id - select cf).SingleOrDefault(); - - if (xLConditionalFormat == null) + var xlConditionalFormat = ws.ConditionalFormats + .Cast() + .SingleOrDefault(cf => cf.Id.WrapInBraces() == conditionalFormattingRule.Id); + if (xlConditionalFormat != null) { - continue; + var negativeFillColor = conditionalFormattingRule.Descendants().SingleOrDefault(); + var color = new DocumentFormat.OpenXml.Spreadsheet.Color { Rgb = negativeFillColor.Rgb }; + xlConditionalFormat.Colors.Add(this.GetColor(color)); } - - var negativeFillColor = conditionalFormattingRule.Descendants().SingleOrDefault(); - var color = new DocumentFormat.OpenXml.Spreadsheet.Color { Rgb = negativeFillColor.Rgb }; - xLConditionalFormat.Colors.Add(this.GetColor(color)); } } diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index 50510ce..9979eec 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -4525,12 +4525,10 @@ ) ) { - foreach (IXLConditionalFormat xLConditionalFormat in cfGroup.CfList) + foreach (var xlConditionalFormat in cfGroup.CfList.Cast()) { - DocumentFormat.OpenXml.Office2010.Excel.ConditionalFormattingRule conditionalFormattingRule = ( - from r in worksheetExtensionList.Descendants() - where r.Id == xLConditionalFormat.Name - select r).SingleOrDefault(); + var conditionalFormattingRule = worksheetExtensionList.Descendants() + .SingleOrDefault(r => r.Id == xlConditionalFormat.Id.WrapInBraces()); if (conditionalFormattingRule != null) { WorksheetExtension worksheetExtension = conditionalFormattingRule.Ancestors().SingleOrDefault(); @@ -4542,7 +4540,7 @@ var conditionalFormatting = new DocumentFormat.OpenXml.Office2010.Excel.ConditionalFormatting(); conditionalFormatting.AddNamespaceDeclaration("xm", "http://schemas.microsoft.com/office/excel/2006/main"); - conditionalFormatting.Append(XLCFConvertersExtension.Convert(xLConditionalFormat, context)); + conditionalFormatting.Append(XLCFConvertersExtension.Convert(xlConditionalFormat, context)); var referenceSequence = new DocumentFormat.OpenXml.Office.Excel.ReferenceSequence { Text = cfGroup.RangeId }; conditionalFormatting.Append(referenceSequence); diff --git a/ClosedXML/Extensions/GuidExtensions.cs b/ClosedXML/Extensions/GuidExtensions.cs new file mode 100644 index 0000000..c9d2eba --- /dev/null +++ b/ClosedXML/Extensions/GuidExtensions.cs @@ -0,0 +1,12 @@ +using System; + +namespace ClosedXML.Extensions +{ + internal static class GuidExtensions + { + internal static String WrapInBraces(this Guid guid) + { + return string.Concat('{', guid.ToString(), '}'); + } + } +} diff --git a/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs b/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs index c8bc883..d87a082 100644 --- a/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs +++ b/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs @@ -544,7 +544,7 @@ var guid = new Guid(bytes); var conditionalFormat = ws.RangeUsed().AddConditionalFormat(); - conditionalFormat.Name = string.Concat("{", guid.ToString(), "}"); + //conditionalFormat.Id = string.Concat("{", guid.ToString(), "}"); conditionalFormat.DataBar(XLColor.Green, XLColor.Red, false) .LowestValue() .Maximum(XLCFContentType.Percent, "100");