diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs index c89ebba..8fc65e2 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/ConditionalFormats/XLConditionalFormat.cs @@ -55,15 +55,15 @@ } public bool StyleChanged { get; set; } - public IXLRange Range { get; set; } - public XLConditionalFormatType ConditionalFormatType { get; set; } - public XLTimePeriod TimePeriod { get; set; } - public XLIconSetStyle IconSetStyle { get; set; } public XLDictionary Values { get; private set; } public XLDictionary Colors { get; private set; } public XLDictionary ContentTypes { get; private set; } public XLDictionary IconSetOperators { get; private set; } + public IXLRange Range { get; set; } + public XLConditionalFormatType ConditionalFormatType { get; set; } + public XLTimePeriod TimePeriod { get; set; } + public XLIconSetStyle IconSetStyle { get; set; } public XLCFOperator Operator { get; set; } public Boolean Bottom { get; set; } public Boolean Percent { get; set; } @@ -71,6 +71,31 @@ public Boolean ShowIconOnly { get; set; } public Boolean ShowBarOnly { get; set; } + public void CopyFrom(IXLConditionalFormat other) + { + Style = other.Style; + 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; + + CopyDictionary(Values, other.Values); + CopyDictionary(Colors, other.Colors); + CopyDictionary(ContentTypes, other.ContentTypes); + CopyDictionary(IconSetOperators, other.IconSetOperators); + } + + private void CopyDictionary(XLDictionary target, XLDictionary source) + { + target.Clear(); + source.ForEach(kp => target.Add(kp.Key, kp.Value)); + } + public IXLStyle WhenIsBlank() { ConditionalFormatType = XLConditionalFormatType.IsBlank; diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs index 160ec36..b3176ab 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs @@ -1050,6 +1050,7 @@ Int32 firstColumnReturn = RangeAddress.FirstAddress.ColumnNumber ; Int32 lastColumnReturn = RangeAddress.FirstAddress.ColumnNumber + numberOfColumns - 1; + Worksheet.BreakConditionalFormatsIntoCells(); using (var asRange = AsRange()) Worksheet.NotifyRangeShiftedColumns(asRange, numberOfColumns); @@ -1255,6 +1256,7 @@ Int32 firstColumnReturn = RangeAddress.FirstAddress.ColumnNumber; Int32 lastColumnReturn = RangeAddress.LastAddress.ColumnNumber; + Worksheet.BreakConditionalFormatsIntoCells(); using (var asRange = AsRange()) Worksheet.NotifyRangeShiftedRows(asRange, numberOfRows); @@ -1400,6 +1402,7 @@ var hyperlinksToRemove = Worksheet.Hyperlinks.Where(hl => Contains(hl.Cell.AsRange())).ToList(); hyperlinksToRemove.ForEach(hl => Worksheet.Hyperlinks.Delete(hl)); + Worksheet.BreakConditionalFormatsIntoCells(); using (var shiftedRange = AsRange()) { if (shiftDeleteCells == XLShiftDeletedCells.ShiftCellsUp) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs index ec44544..6dd02fa 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs @@ -1164,15 +1164,30 @@ newMerge.Add(newRng); } else if (!(range.RangeAddress.FirstAddress.RowNumber <= rngMerged.RangeAddress.FirstAddress.RowNumber - && - range.RangeAddress.FirstAddress.ColumnNumber <= - rngMerged.RangeAddress.LastAddress.ColumnNumber)) + && range.RangeAddress.FirstAddress.ColumnNumber <= rngMerged.RangeAddress.LastAddress.ColumnNumber)) newMerge.Add(rngMerged); } Internals.MergedRanges = newMerge; Workbook.Worksheets.ForEach(ws => MoveNamedRangesRows(range, rowsShifted, ws.NamedRanges)); MoveNamedRangesRows(range, rowsShifted, Workbook.NamedRanges); + + } + + internal void BreakConditionalFormatsIntoCells() + { + var newConditionalFormats = new XLConditionalFormats(); + foreach (var conditionalFormat in ConditionalFormats) + { + foreach (XLCell cell in conditionalFormat.Range.Cells()) + { + var newConditionalFormat = new XLConditionalFormat(cell.AsRange()); + newConditionalFormat.CopyFrom(conditionalFormat); + newConditionalFormats.Add(newConditionalFormat); + } + conditionalFormat.Range.Dispose(); + } + ConditionalFormats = newConditionalFormats; } private void MoveNamedRangesRows(XLRange range, int rowsShifted, IXLNamedRanges namedRanges) diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs index b50c6e3..22bd5de 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/ConditionalFormatting/ConditionalFormatting.cs @@ -514,6 +514,27 @@ } } + public class CFInsertRows : IXLExample + { + public void Create(String filePath) + { + var workbook = new XLWorkbook(); + var ws = workbook.AddWorksheet("Sheet1"); + + ws.Cell(2,1).SetValue(1) + .CellRight().SetValue(1) + .CellRight().SetValue(2) + .CellRight().SetValue(3); + + var range = ws.RangeUsed(); + range.AddConditionalFormat().WhenEqualTo("1").Font.SetBold(); + range.InsertRowsAbove(1); + + + workbook.SaveAs(filePath); + } + } + public class CFTest : IXLExample { public void Create(String filePath) @@ -521,15 +542,13 @@ var workbook = new XLWorkbook(); var ws = workbook.AddWorksheet("Sheet1"); - ws.FirstCell().SetValue(1) - .CellBelow().SetValue(1) - .CellBelow().SetValue(2) - .CellBelow().SetValue(3); + ws.Cell(2, 1).SetValue("1") + .CellRight().SetValue("1") + .CellRight().SetValue("2") + .CellRight().SetValue("3"); - ws.RangeUsed().AddConditionalFormat().IconSet(XLIconSetStyle.ThreeTrafficLights2, true, true) - .AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "0", XLCFContentType.Number) - .AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "2", XLCFContentType.Number) - .AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "3", XLCFContentType.Number); + ws.RangeUsed().AddConditionalFormat().WhenEqualTo("1").Fill.SetBackgroundColor(XLColor.Red); + ws.Range("B2:C2").InsertRowsAbove(1); workbook.SaveAs(filePath); }