diff --git a/EPPlus/CellStore.cs b/EPPlus/CellStore.cs index b1e50485..608fa34b 100644 --- a/EPPlus/CellStore.cs +++ b/EPPlus/CellStore.cs @@ -864,7 +864,7 @@ internal void Insert(int fromRow, int fromCol, int rows, int columns) var pagePos = column.GetPosition(fromRow); if (pagePos >= 0) { - if (fromRow >= column._pages[pagePos].MinIndex && fromRow <= column._pages[pagePos].MaxIndex) //The row is inside the page + if (IsWithinPage(fromRow, column, pagePos)) //The row is inside the page { int offset = fromRow - column._pages[pagePos].IndexOffset; var rowPos = column._pages[pagePos].GetPosition(offset); @@ -874,7 +874,7 @@ internal void Insert(int fromRow, int fromCol, int rows, int columns) } UpdateIndexOffset(column, pagePos, rowPos, fromRow, rows); } - else if (column._pages[pagePos].MinIndex > fromRow - 1 && pagePos > 0) //The row is on the page before. + else if (pagePos > 0 && IsWithinPage(fromRow, column, pagePos-1)) //The row is inside the previous page { int offset = fromRow - ((page - 1) << pageBits); var rowPos = column._pages[pagePos - 1].GetPosition(offset); @@ -909,7 +909,13 @@ internal void Insert(int fromRow, int fromCol, int rows, int columns) } } } - internal void Clear(int fromRow, int fromCol, int rows, int columns) + + private static bool IsWithinPage(int row, ColumnIndex column, int pagePos) + { + return (row >= column._pages[pagePos].MinIndex && row <= column._pages[pagePos].MaxIndex); + } + + internal void Clear(int fromRow, int fromCol, int rows, int columns) { Delete(fromRow, fromCol, rows, columns, false); } diff --git a/EPPlusTest/CellStoreTest.cs b/EPPlusTest/CellStoreTest.cs index 26faa104..59b525dd 100644 --- a/EPPlusTest/CellStoreTest.cs +++ b/EPPlusTest/CellStoreTest.cs @@ -1,6 +1,8 @@ using System; +using System.Drawing; using Microsoft.VisualStudio.TestTools.UnitTesting; using OfficeOpenXml; +using OfficeOpenXml.Style; namespace EPPlusTest { @@ -185,5 +187,26 @@ public void CopyCellsTest() Assert.AreEqual(ws.Cells["B1"].Value, ws.Cells["D1"].Value); Assert.AreNotEqual(ws.Cells["B1"].Formula, ws.Cells["D1"].Formula); } + [TestMethod] + public void Issues351() + { + using (var package = new ExcelPackage()) + { + // Arrange + var worksheet = package.Workbook.Worksheets.Add("Test"); + worksheet.Cells[1, 1].Value = "A"; // If you remove this "anchor", the problem doesn't happen. + worksheet.Cells[1026, 1].Value = "B"; + worksheet.Cells[1026, 2].Value = "B"; + var range = worksheet.Row(1026); + range.Style.Fill.PatternType = ExcelFillStyle.Solid; + range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 255, 0)); + + // Act - This should shift the whole row 1026 down 1 + worksheet.InsertRow(1024, 1); + + // Assert - This value should be null, instead it's "B" + Assert.IsNull(worksheet.Cells[1025, 1].Value); + } + } } }