diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs index 31bcea2..09c6e3f 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs @@ -880,21 +880,41 @@ public IXLCell Clear(XLClearOptions clearOptions = XLClearOptions.ContentsAndFormats) { - if (clearOptions == XLClearOptions.Contents || clearOptions == XLClearOptions.ContentsAndFormats) + return Clear(clearOptions, false); + } + + internal IXLCell Clear(XLClearOptions clearOptions, bool calledFromRange) + { + //Note: We have to check if the cell is part of a merged range. If so we have to clear the whole range + //Checking if called from range to avoid stack overflow + if (IsMerged() && !calledFromRange) { - Hyperlink = null; - _richText = null; - //_comment = null; - _cellValue = String.Empty; - FormulaA1 = String.Empty; + using (var asRange = AsRange()) + { + var firstOrDefault = Worksheet.Internals.MergedRanges.FirstOrDefault(asRange.Intersects); + if (firstOrDefault != null) + firstOrDefault.Clear(clearOptions); + } } - - if (clearOptions == XLClearOptions.Formats || clearOptions == XLClearOptions.ContentsAndFormats) + else { - if (HasDataValidation) - DataValidation.Clear(); + if (clearOptions == XLClearOptions.Contents || clearOptions == XLClearOptions.ContentsAndFormats) + { + Hyperlink = null; + _richText = null; + //_comment = null; + _cellValue = String.Empty; + FormulaA1 = String.Empty; + } - SetStyle(Worksheet.Style); + if (clearOptions == XLClearOptions.Formats || clearOptions == XLClearOptions.ContentsAndFormats) + { + if (HasDataValidation) + DataValidation.Clear(); + + + SetStyle(Worksheet.Style); + } } return this; diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs index d44d6eb..5505211 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs @@ -370,7 +370,7 @@ clearOptions == XLClearOptions.ContentsAndFormats; foreach (var cell in CellsUsed(includeFormats)) { - cell.Clear(clearOptions); + cell.Clear(clearOptions, true); } if (includeFormats)