diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs index 6e2dfbd..1a3e21f 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs @@ -1151,6 +1151,9 @@ if (Worksheet.Internals.ColumnsCollection.TryGetValue(Address.ColumnNumber, out column) && !column.Style.Equals(Worksheet.Style)) return false; } + + if (Worksheet.ConditionalFormats.Any(cf => cf.Range.Contains(this))) + return false; } return true; } @@ -1689,7 +1692,9 @@ SetStyle(source._style ?? source.Worksheet.Workbook.GetStyleById(source._styleCacheId)); - + var conditionalFormats = otherCell.Worksheet.ConditionalFormats.Where(c => c.Range.Contains(otherCell)).ToList(); + foreach(var cf in conditionalFormats) + _worksheet.ConditionalFormats.Add(new XLConditionalFormat(cf as XLConditionalFormat) { Range = AsRange()}); if (copyDataValidations) { diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs index 8510720..e23cf98 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs @@ -17,6 +17,27 @@ ContentTypes = new XLDictionary(); IconSetOperators = new XLDictionary(); } + public XLConditionalFormat(XLConditionalFormat other) + { + 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); + + + 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; + } + private IXLStyle _style; private Int32 _styleCacheId; public IXLStyle Style { get { return GetStyle(); } set { SetStyle(value); } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Misc/XLDictionary.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Misc/XLDictionary.cs index 6d289e8..026f80b 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Misc/XLDictionary.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Misc/XLDictionary.cs @@ -7,6 +7,14 @@ { public class XLDictionary: Dictionary { + public XLDictionary() + { + + } + public XLDictionary(XLDictionary other) + { + other.Values.ForEach(Add); + } public void Initialize(T value) { if (Count > 0) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRangeBase.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRangeBase.cs index 1b2c754..30c12a3 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRangeBase.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRangeBase.cs @@ -153,6 +153,8 @@ /// Boolean Contains(IXLRangeBase range); + Boolean Contains(IXLCell cell); + /// /// Determines whether this range intersects the specified range. /// For whole matches use the range.Contains method. diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs index 0914108..8c7947d 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs @@ -1316,6 +1316,11 @@ mergeToDelete.ForEach(m => Worksheet.Internals.MergedRanges.Remove(m)); } + public Boolean Contains(IXLCell cell) + { + return Contains(cell.Address as XLAddress); + } + public bool Contains(XLAddress first, XLAddress last) { return Contains(first) && Contains(last); diff --git a/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/Misc/CopyContentsTest.cs b/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/Misc/CopyContentsTest.cs index a91d7e3..3ffffbf 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/Misc/CopyContentsTest.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/Misc/CopyContentsTest.cs @@ -56,5 +56,15 @@ originalRange.CopyTo(destRange); } } + + [TestMethod] + public void CopyConditionalFormatsCount() + { + var wb = new XLWorkbook(); + var ws = wb.AddWorksheet("Sheet1"); + ws.FirstCell().AddConditionalFormat().WhenContains("1").Fill.SetBackgroundColor(XLColor.Blue); + ws.Cell("A2").Value = ws.FirstCell(); + Assert.AreEqual(2, ws.ConditionalFormats.Count()); + } } } diff --git a/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/Worksheets/XLWorksheetTests.cs b/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/Worksheets/XLWorksheetTests.cs index cefe55c..9b538e6 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/Worksheets/XLWorksheetTests.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/Worksheets/XLWorksheetTests.cs @@ -98,5 +98,15 @@ var end = DateTime.Now; Assert.IsTrue((end - start).TotalMilliseconds < 500); } + + [TestMethod] + public void CopyConditionalFormatsCount() + { + var wb = new XLWorkbook(); + var ws = wb.AddWorksheet("Sheet1"); + ws.FirstCell().AddConditionalFormat().WhenContains("1").Fill.SetBackgroundColor(XLColor.Blue); + var ws2 = ws.CopyTo("Sheet2"); + Assert.AreEqual(1, ws2.ConditionalFormats.Count()); + } } }