diff --git a/ClosedXML/Excel/Columns/XLColumn.cs b/ClosedXML/Excel/Columns/XLColumn.cs index b8036d4..f106457 100644 --- a/ClosedXML/Excel/Columns/XLColumn.cs +++ b/ClosedXML/Excel/Columns/XLColumn.cs @@ -786,5 +786,15 @@ return base.IsEmpty(includeFormats); } + + public Boolean IsEntireRow() + { + return false; + } + + public Boolean IsEntireColumn() + { + return true; + } } } diff --git a/ClosedXML/Excel/Ranges/IXLRangeBase.cs b/ClosedXML/Excel/Ranges/IXLRangeBase.cs index 76902d0..b28e5c8 100644 --- a/ClosedXML/Excel/Ranges/IXLRangeBase.cs +++ b/ClosedXML/Excel/Ranges/IXLRangeBase.cs @@ -239,6 +239,9 @@ Boolean IsEmpty(); Boolean IsEmpty(Boolean includeFormats); + Boolean IsEntireRow(); + Boolean IsEntireColumn(); + IXLPivotTable CreatePivotTable(IXLCell targetCell); IXLPivotTable CreatePivotTable(IXLCell targetCell, String name); diff --git a/ClosedXML/Excel/Ranges/XLRangeBase.cs b/ClosedXML/Excel/Ranges/XLRangeBase.cs index 6e34637..39b80d6 100644 --- a/ClosedXML/Excel/Ranges/XLRangeBase.cs +++ b/ClosedXML/Excel/Ranges/XLRangeBase.cs @@ -46,8 +46,8 @@ #region Constructor - static Int32 IdCounter = 0; - readonly Int32 Id; + private static Int32 IdCounter = 0; + private readonly Int32 Id; protected XLRangeBase(XLRangeAddress rangeAddress) { @@ -82,8 +82,6 @@ #region Public properties - //public XLRangeAddress RangeAddress { get; protected set; } - private XLRangeAddress _rangeAddress; public XLRangeAddress RangeAddress { @@ -516,6 +514,18 @@ CellsUsed(includeFormats).Cast().Any(c => c.IsEmpty(includeFormats)); } + public Boolean IsEntireRow() + { + return RangeAddress.FirstAddress.ColumnNumber == 1 + && RangeAddress.LastAddress.ColumnNumber == XLHelper.MaxColumnNumber; + } + + public Boolean IsEntireColumn() + { + return RangeAddress.FirstAddress.RowNumber == 1 + && RangeAddress.LastAddress.RowNumber == XLHelper.MaxRowNumber; + } + #endregion #region IXLStylized Members @@ -1092,10 +1102,10 @@ { for (int co = lastColumn; co >= firstColumn; co--) { + int newColumn = co + numberOfColumns; for (int ro = lastRow; ro >= firstRow; ro--) { var oldKey = new XLAddress(Worksheet, ro, co, false, false); - int newColumn = co + numberOfColumns; var newKey = new XLAddress(Worksheet, ro, newColumn, false, false); var oldCell = Worksheet.Internals.CellsCollection.GetCell(ro, co) ?? Worksheet.Cell(oldKey); @@ -1106,6 +1116,11 @@ cellsToInsert.Add(newKey, newCell); cellsToDelete.Add(oldKey); } + + if (this.IsEntireColumn()) + { + Worksheet.Column(newColumn).Width = Worksheet.Column(co).Width; + } } } } @@ -1323,10 +1338,11 @@ { for (int ro = lastRow; ro >= firstRow; ro--) { + int newRow = ro + numberOfRows; + for (int co = lastColumn; co >= firstColumn; co--) { var oldKey = new XLAddress(Worksheet, ro, co, false, false); - int newRow = ro + numberOfRows; var newKey = new XLAddress(Worksheet, newRow, co, false, false); var oldCell = Worksheet.Internals.CellsCollection.GetCell(ro, co); if (oldCell != null) @@ -1338,6 +1354,10 @@ cellsToDelete.Add(oldKey); } } + if (this.IsEntireRow()) + { + Worksheet.Row(newRow).Height = Worksheet.Row(ro).Height; + } } } } diff --git a/ClosedXML/Excel/Rows/XLRow.cs b/ClosedXML/Excel/Rows/XLRow.cs index 944cb17..80dee39 100644 --- a/ClosedXML/Excel/Rows/XLRow.cs +++ b/ClosedXML/Excel/Rows/XLRow.cs @@ -715,6 +715,15 @@ return base.IsEmpty(includeFormats); } + public Boolean IsEntireRow() + { + return true; + } + + public Boolean IsEntireColumn() + { + return false; + } } } diff --git a/ClosedXML/Excel/XLWorksheet.cs b/ClosedXML/Excel/XLWorksheet.cs index c28ef98..baa8831 100644 --- a/ClosedXML/Excel/XLWorksheet.cs +++ b/ClosedXML/Excel/XLWorksheet.cs @@ -1569,5 +1569,14 @@ { return Pictures.Add(imageFile, name); } + public Boolean IsEntireRow() + { + return true; + } + + public Boolean IsEntireColumn() + { + return true; + } } } diff --git a/ClosedXML_Tests/Excel/Rows/RowTests.cs b/ClosedXML_Tests/Excel/Rows/RowTests.cs index 17cddeb..08172ab 100644 --- a/ClosedXML_Tests/Excel/Rows/RowTests.cs +++ b/ClosedXML_Tests/Excel/Rows/RowTests.cs @@ -185,6 +185,32 @@ } [Test] + public void InsertingRowsAbove4() + { + using (var wb = new XLWorkbook()) + { + var ws = wb.Worksheets.Add("Sheet1"); + + ws.Row(2).Height = 15; + ws.Row(3).Height = 20; + ws.Row(4).Height = 25; + ws.Row(5).Height = 35; + + ws.Row(2).FirstCell().SetValue("Row height: 15"); + ws.Row(3).FirstCell().SetValue("Row height: 20"); + ws.Row(4).FirstCell().SetValue("Row height: 25"); + ws.Row(5).FirstCell().SetValue("Row height: 35"); + + ws.Range("3:3").InsertRowsAbove(1); + + Assert.AreEqual(15, ws.Row(2).Height); + Assert.AreEqual(20, ws.Row(4).Height); + Assert.AreEqual(25, ws.Row(5).Height); + Assert.AreEqual(35, ws.Row(6).Height); + } + } + + [Test] public void NoRowsUsed() { var wb = new XLWorkbook(); @@ -224,4 +250,4 @@ ws.Rows(1, 2).Ungroup(true); } } -} \ No newline at end of file +}