Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Commit

Permalink
Fixes issue #351
Browse files Browse the repository at this point in the history
  • Loading branch information
EPPlus committed Dec 30, 2018
1 parent e47dff5 commit 2559308
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
12 changes: 9 additions & 3 deletions EPPlus/CellStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
23 changes: 23 additions & 0 deletions EPPlusTest/CellStoreTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OfficeOpenXml;
using OfficeOpenXml.Style;

namespace EPPlusTest
{
Expand Down Expand Up @@ -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);
}
}
}
}

0 comments on commit 2559308

Please sign in to comment.