From c61245b8d5c2fdaeb3ea6cfc966c7097dc6b6d8d Mon Sep 17 00:00:00 2001 From: Tim Lee Date: Mon, 16 Dec 2024 16:28:33 +0800 Subject: [PATCH 1/2] Fix ShiftMergedRegions RT --- main/SS/UserModel/Helpers/RowShifter.cs | 60 ++++++++++++------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/main/SS/UserModel/Helpers/RowShifter.cs b/main/SS/UserModel/Helpers/RowShifter.cs index db07347fa..f6a46bb01 100644 --- a/main/SS/UserModel/Helpers/RowShifter.cs +++ b/main/SS/UserModel/Helpers/RowShifter.cs @@ -49,39 +49,50 @@ public List ShiftMergedRegions(int startRow, int endRow, int n { var ShiftedRegions = new List(); ISet removedIndices = new HashSet(); - //move merged regions completely if they fall within the new region boundaries when they are Shifted var size = sheet.NumMergedRegions; - var lastCol = sheet.Any() ? sheet.Max(r => r.LastCellNum) : 0; for (var i = 0; i < size; i++) { var merged = sheet.GetMergedRegion(i); - // remove merged region that overlaps Shifting - if (RemovalNeeded(merged, startRow, endRow, n, lastCol)) + //Shift if the merged region inside the Shifting rows + if (merged.FirstRow >= startRow && merged.LastRow <= endRow) { + merged.FirstRow += n; + merged.LastRow += n; + //have to Remove/add it back + ShiftedRegions.Add(merged); removedIndices.Add(i); + continue; } - var inStart = merged.FirstRow >= startRow || merged.LastRow >= startRow; - var inEnd = merged.FirstRow <= endRow || merged.LastRow <= endRow; - //don't check if it's not within the Shifted area - if (!inStart || !inEnd) + if (n > 0) { - continue; + // area is moved down + if (merged.LastRow < startRow + || merged.FirstRow > endRow + n + || merged.FirstRow > endRow && merged.LastRow < startRow + n + ) + { + continue; + } } - - //only shift if the region outside the Shifted rows is not merged too - if (!merged.ContainsRow(startRow - 1) && !merged.ContainsRow(endRow + 1)) + else { - merged.FirstRow += n; - merged.LastRow += n; - //have to Remove/add it back - ShiftedRegions.Add(merged); - removedIndices.Add(i); + // area is moved up + if (merged.LastRow < startRow + n + || merged.FirstRow > endRow + || merged.FirstRow > endRow + n && merged.LastRow < startRow + ) + { + continue; + } } + + //remove merged region that overlaps Shifting + removedIndices.Add(i); } if (removedIndices.Count != 0) @@ -89,7 +100,7 @@ public List ShiftMergedRegions(int startRow, int endRow, int n sheet.RemoveMergedRegions(removedIndices.ToList()); } - //read so it doesn't Get Shifted again + //add it which is within the shifted area back foreach (var region in ShiftedRegions) { sheet.AddMergedRegion(region); @@ -97,19 +108,6 @@ public List ShiftMergedRegions(int startRow, int endRow, int n return ShiftedRegions; } - - // Keep in sync with {@link ColumnShifter#removalNeeded} - private bool RemovalNeeded(CellRangeAddress merged, int startRow, int endRow, int n, int lastCol) - { - // build a range of the rows that are overwritten, i.e. the target-area, but without - // rows that are moved along - var firstRow = startRow + n; - var lastRow = endRow + n; - CellRangeAddress overwrite = new CellRangeAddress(firstRow, lastRow, 0, lastCol); - - // if the merged-region and the overwritten area intersect, we need to remove it - return merged.Intersects(overwrite); - } /// /// Verify that the given column indices and step denote a valid range of columns to shift From 69c904672c503cd5d7ee4801876ae0930ea112c4 Mon Sep 17 00:00:00 2001 From: Tim Lee Date: Mon, 23 Dec 2024 10:40:44 +0800 Subject: [PATCH 2/2] Update TestXSSFSheet.cs --- testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs index 9082d2e95..2f60e6b8f 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs @@ -145,13 +145,13 @@ public void ShiftRows_ShiftRowsWithVariousMergedRegions_RowsShiftedWithMergedReg Assert.True(sheet.MergedRegions.Any(r => r.FormatAsString().Equals("J11:K12"))); Assert.AreEqual("regionThatEndsWithinShiftedRows", sheet.GetRow(1).GetCell(11).StringCellValue); - Assert.True(sheet.MergedRegions.Any(r => r.FormatAsString().Equals("L2:M3"))); + Assert.False(sheet.MergedRegions.Any(r => r.FormatAsString().Equals("L2:M3"))); Assert.AreEqual("regionThatEndsOnLastShiftedRow", sheet.GetRow(1).GetCell(13).StringCellValue); - Assert.True(sheet.MergedRegions.Any(r => r.FormatAsString().Equals("N2:O4"))); + Assert.False(sheet.MergedRegions.Any(r => r.FormatAsString().Equals("N2:O4"))); Assert.AreEqual("regionThatEndsOutsideShiftedRows", sheet.GetRow(1).GetCell(15).StringCellValue); - Assert.True(sheet.MergedRegions.Any(r => r.FormatAsString().Equals("P2:Q5"))); + Assert.False(sheet.MergedRegions.Any(r => r.FormatAsString().Equals("P2:Q5"))); Assert.AreEqual("reallyLongRegion", sheet.GetRow(1).GetCell(17).StringCellValue); Assert.False(sheet.MergedRegions.Any(r => r.FormatAsString().Equals("R2:S12"))); @@ -180,13 +180,13 @@ public void ShiftRows_ShiftRowsWithVariousMergedRegions_RowsShiftedWithMergedReg Assert.True(sheetLoaded.MergedRegions.Any(r => r.FormatAsString().Equals("J11:K12"))); Assert.AreEqual("regionThatEndsWithinShiftedRows", sheetLoaded.GetRow(1).GetCell(11).StringCellValue); - Assert.True(sheetLoaded.MergedRegions.Any(r => r.FormatAsString().Equals("L2:M3"))); + Assert.False(sheetLoaded.MergedRegions.Any(r => r.FormatAsString().Equals("L2:M3"))); Assert.AreEqual("regionThatEndsOnLastShiftedRow", sheetLoaded.GetRow(1).GetCell(13).StringCellValue); - Assert.True(sheetLoaded.MergedRegions.Any(r => r.FormatAsString().Equals("N2:O4"))); + Assert.False(sheetLoaded.MergedRegions.Any(r => r.FormatAsString().Equals("N2:O4"))); Assert.AreEqual("regionThatEndsOutsideShiftedRows", sheetLoaded.GetRow(1).GetCell(15).StringCellValue); - Assert.True(sheetLoaded.MergedRegions.Any(r => r.FormatAsString().Equals("P2:Q5"))); + Assert.False(sheetLoaded.MergedRegions.Any(r => r.FormatAsString().Equals("P2:Q5"))); Assert.AreEqual("reallyLongRegion", sheetLoaded.GetRow(1).GetCell(17).StringCellValue); Assert.False(sheetLoaded.MergedRegions.Any(r => r.FormatAsString().Equals("R2:S12")));