diff --git a/EPPlus/CellStore.cs b/EPPlus/CellStore.cs index 608fa34b..17ad07bd 100644 --- a/EPPlus/CellStore.cs +++ b/EPPlus/CellStore.cs @@ -1106,7 +1106,7 @@ private int DeletePage(int fromRow, int rows, ColumnIndex column, int pagePos, b var delSize=page.MaxIndex - page.MinIndex+1; rows -= delSize; var prevOffset = page.Offset; - Array.Copy(column._pages, pagePos + 1, column._pages, pagePos, column.PageCount - pagePos + 1); + Array.Copy(column._pages, pagePos + 1, column._pages, pagePos, column.PageCount - pagePos); column.PageCount--; if (column.PageCount == 0) { diff --git a/EPPlus/ExcelRangeBase.cs b/EPPlus/ExcelRangeBase.cs index 2bd7ff46..0519de60 100644 --- a/EPPlus/ExcelRangeBase.cs +++ b/EPPlus/ExcelRangeBase.cs @@ -2987,7 +2987,6 @@ public void Sort(int[] columns, bool[] descending = null, CultureInfo culture = var formulas = GetItems(_worksheet._formulas, _fromRow, _fromCol, _toRow, _toCol); var hyperLinks = GetItems(_worksheet._hyperLinks, _fromRow, _fromCol, _toRow, _toCol); var comments = GetItems(_worksheet._commentsStore, _fromRow, _fromCol, _toRow, _toCol); - var sf = new HashSet(); //Sort the values and styles. _worksheet._values.Clear(_fromRow, _fromCol, _toRow - _fromRow + 1, cols); for (var r = 0; r < l.Count; r++) @@ -3009,19 +3008,19 @@ public void Sort(int[] columns, bool[] descending = null, CultureInfo culture = _worksheet._formulas.SetValue(row, col, formulas[addr]); if (formulas[addr] is int) { - var sfIx = (int)formulas[addr]; - if (!sf.Contains(sfIx)) + int sfIx = (int)formulas[addr]; + var startAddr = new ExcelAddress(Worksheet._sharedFormulas[sfIx].Address); + var f = Worksheet._sharedFormulas[sfIx]; + if (startAddr._fromRow > row) { - var startAddr = new ExcelAddress(Worksheet._sharedFormulas[sfIx].Address); - if (startAddr._fromRow > row) - { - var f = Worksheet._sharedFormulas[sfIx]; - f.Formula = ExcelCellBase.TranslateFromR1C1(ExcelCellBase.TranslateToR1C1(f.Formula, f.StartRow, f.StartCol), row, f.StartCol); - f.StartRow = row; - f.Address = ExcelCellBase.GetAddress(row, col, startAddr._toRow, startAddr._toCol); - } + f.Formula = ExcelCellBase.TranslateFromR1C1(ExcelCellBase.TranslateToR1C1(f.Formula, f.StartRow, f.StartCol), row, f.StartCol); + f.StartRow = row; + f.Address = ExcelCellBase.GetAddress(row, startAddr._fromCol, startAddr._toRow, startAddr._toCol); + } + else if (startAddr._toRow < row) + { + f.Address = ExcelCellBase.GetAddress(startAddr._fromRow, startAddr._fromCol, row, startAddr._toCol); } - sf.Add(sfIx); } } diff --git a/EPPlusTest/Issues.cs b/EPPlusTest/Issues.cs index 3cdcbd8f..ea8ef78c 100644 --- a/EPPlusTest/Issues.cs +++ b/EPPlusTest/Issues.cs @@ -2347,7 +2347,7 @@ public void Issue376() [TestMethod] public void Issue367() { - using(var pck = new ExcelPackage(new FileInfo(@"c:\Temp\ProductFunctionTest.xlsx"))) + using(var pck = OpenTemplatePackage(@"ProductFunctionTest.xlsx")) { var sheet = pck.Workbook.Worksheets.First(); //sheet.Cells["B13"].Value = null; @@ -2359,5 +2359,17 @@ public void Issue367() Assert.AreEqual(0d, sheet.Cells["B16"].Value); } } + [TestMethod] + public void Issue345() + { + using (ExcelPackage package = OpenTemplatePackage("issue345.xlsx")) + { + var worksheet = package.Workbook.Worksheets["test"]; + int[] sortColumns = new int[1]; + sortColumns[0] = 0; + worksheet.Cells["A2:A30864"].Sort(sortColumns); + package.Save(); + } + } } }