Skip to content

Commit

Permalink
#1828 - fixed an issue with shifting formula addresses on sorting a r…
Browse files Browse the repository at this point in the history
…ange
  • Loading branch information
swmal committed Jan 29, 2025
1 parent daf4958 commit fe1288d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/EPPlus/Sorting/Internal/SortItemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,21 @@ internal static class SortItemFactory
internal static List<SortItem<ExcelValue>> Create(ExcelRangeBase range)
{
var e = new CellStoreEnumerator<ExcelValue>(range.Worksheet._values, range._fromRow, range._fromCol, range._toRow, range._toCol);
var dim = range.Worksheet.Dimension;
var sortItems = new List<SortItem<ExcelValue>>();
SortItem<ExcelValue> item = new SortItem<ExcelValue>();
var cols = range._toCol - range._fromCol + 1;
if(dim != null && cols > dim.End.Column)
{
cols = dim.End.Column;
}
while (e.Next())
{
if(dim != null)
{
if (e.Row > dim.End.Row) continue;
if (e.Column > dim.End.Column) continue;
}
if (sortItems.Count == 0 || sortItems[sortItems.Count - 1].Row != e.Row)
{
item = new SortItem<ExcelValue>() { Row = e.Row, Items = new ExcelValue[cols] };
Expand Down
10 changes: 8 additions & 2 deletions src/EPPlus/Sorting/RangeSorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ private void ApplySortedRange(ExcelRangeBase range, List<SortItem<ExcelValue>> s
{
//Sort the values and styles.
var nColumnsInRange = range._toCol - range._fromCol + 1;
var dim = range.Worksheet.Dimension;
if(dim != null && nColumnsInRange > dim.End.Column)
{
nColumnsInRange = dim.End.Column;
}
_worksheet._values.Clear(range._fromRow, range._fromCol, range._toRow - range._fromRow + 1, nColumnsInRange);
for (var r = 0; r < sortItems.Count; r++)
{
Expand Down Expand Up @@ -235,11 +240,12 @@ private void HandleFormula(RangeWorksheetData wsd, int row, int col, string addr
if (startAddr._fromRow > row)
{
f.Formula = ExcelCellBase.TranslateFromR1C1(ExcelCellBase.TranslateToR1C1(f.Formula, f.StartRow, f.StartCol), row, f.StartCol);
f.Address = ExcelCellBase.GetAddress(row, startAddr._fromCol, startAddr._toRow, startAddr._toCol);
f.Address = ExcelCellBase.GetAddress(row, startAddr._fromCol, startAddr._toRow - (startAddr._toRow - row), startAddr._toCol);
}
else if (startAddr._toRow < row)
{
f.Address = ExcelCellBase.GetAddress(startAddr._fromRow, startAddr._fromCol, row, startAddr._toCol);
f.Formula = ExcelCellBase.TranslateFromR1C1(ExcelCellBase.TranslateToR1C1(f.Formula, f.StartRow, f.StartCol), row, f.StartCol);
f.Address = ExcelCellBase.GetAddress(startAddr._fromRow + (row - startAddr._fromRow), startAddr._fromCol, row, startAddr._toCol);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/EPPlusTest/Issues/FormulaCalculationIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,16 @@ public void i1766()

Assert.AreEqual(area, 35D);
Assert.AreEqual(area, area2);
}

[TestMethod]
public void Sc809()
{
var p = OpenTemplatePackage("Sc809.xlsx");
var sheet = p.Workbook.Worksheets.First();
sheet.Cells.Sort(column: 0);
sheet.Calculate();
SaveWorkbook("Sc809_Output_NotSorted.xlsx", p);
}
}
}

0 comments on commit fe1288d

Please sign in to comment.