From 232d734941d4a082841e42b9451f2ed1b540bf04 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sun, 1 Nov 2020 09:21:45 +0000 Subject: [PATCH] Bug 64460: Fix invalid moving of merged regions Also fix and enable two tests ignored previously git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1883037 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/usermodel/XSSFSheet.java | 5 +- .../TestXSSFSheetShiftRowsAndColumns.java | 131 +++++++++--------- .../usermodel/BaseTestSheetShiftColumns.java | 1 + 3 files changed, 70 insertions(+), 67 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index cce5bb2b90..592ff3a007 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -103,7 +103,6 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; * contain text, numbers, dates, and formulas. Cells can also be formatted. *

*/ -@SuppressWarnings("deprecation") public class XSSFSheet extends POIXMLDocumentPart implements Sheet { private static final POILogger logger = POILogFactory.getLogger(XSSFSheet.class); @@ -136,7 +135,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { private Map sharedFormulas; private SortedMap tables; private List arrayFormulas; - private XSSFDataValidationHelper dataValidationHelper; + private final XSSFDataValidationHelper dataValidationHelper; /** * Creates new XSSFSheet - called by XSSFWorkbook to create a sheet from scratch. @@ -3027,7 +3026,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { FormulaShifter formulaShifter = FormulaShifter.createForColumnShift(this.getWorkbook().getSheetIndex(this), this.getSheetName(), startColumn, endColumn, n, SpreadsheetVersion.EXCEL2007); XSSFColumnShifter columnShifter = new XSSFColumnShifter(this); columnShifter.shiftColumns(startColumn, endColumn, n); - columnShifter.shiftMergedRegions(startColumn, startColumn, n); + columnShifter.shiftMergedRegions(startColumn, endColumn, n); columnShifter.updateFormulas(formulaShifter); columnShifter.updateConditionalFormatting(formulaShifter); columnShifter.updateHyperlinks(formulaShifter); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRowsAndColumns.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRowsAndColumns.java index 86a71b8fb6..37f57baa3c 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRowsAndColumns.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRowsAndColumns.java @@ -22,7 +22,6 @@ import org.apache.poi.ss.util.CellAddress; import org.apache.poi.ss.util.CellRangeAddress; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; @@ -33,23 +32,28 @@ import java.io.OutputStream; import java.util.Locale; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class TestXSSFSheetShiftRowsAndColumns { - final File resultDir=new File("build/custom-reports-test"); - XSSFWorkbook workbook=null; - XSSFSheet sheet=null; - String fileName=null; - final int numRows=4; - final int numCols=4; - final int INSERT_ROW=1; - final int INSERT_COLUMN=1; - final int FIRST_MERGE_ROW=INSERT_ROW+1; - final int LAST_MERGE_ROW=numRows-1; - final int FIRST_MERGE_COL=INSERT_COLUMN+1; - final int LAST_MERGE_COL=numCols-1; + private static final File resultDir = new File("build/custom-reports-test"); + + private static final int numRows = 4; + private static final int numCols = 4; + + private static final int INSERT_ROW = 1; + private static final int INSERT_COLUMN = 1; + private static final int FIRST_MERGE_ROW = INSERT_ROW+1; + private static final int LAST_MERGE_ROW = numRows-1; + private static final int FIRST_MERGE_COL = INSERT_COLUMN+1; + private static final int LAST_MERGE_COL = numCols-1; + + private XSSFWorkbook workbook = null; + private XSSFSheet sheet = null; + private String fileName = null; public TestXSSFSheetShiftRowsAndColumns() { - resultDir.mkdirs(); + assertTrue("Failed to create directory " + resultDir, + resultDir.exists() || resultDir.mkdirs()); } /** @@ -57,8 +61,10 @@ public class TestXSSFSheetShiftRowsAndColumns { * 0 to numCols-1. */ @Before - public void setup() { - final String procName="TestXSSFSheetShiftRowsAndColumns.setup"; + public void setup() throws IOException { + final String procName = "TestXSSFSheetShiftRowsAndColumns.setup"; + fileName = procName+".xlsx"; + workbook = new XSSFWorkbook(); sheet = workbook.createSheet(); @@ -73,10 +79,11 @@ public class TestXSSFSheetShiftRowsAndColumns { /* * Add a merge area */ - final CellRangeAddress range=new CellRangeAddress(FIRST_MERGE_ROW,LAST_MERGE_ROW,FIRST_MERGE_COL,LAST_MERGE_COL); + final CellRangeAddress range = new CellRangeAddress(FIRST_MERGE_ROW,LAST_MERGE_ROW,FIRST_MERGE_COL,LAST_MERGE_COL); sheet.addMergedRegion(range); System.out.println(String.format(Locale.US, "\n%s: mergeArea=%s", procName,range)); + writeFile(procName); } @@ -84,31 +91,28 @@ public class TestXSSFSheetShiftRowsAndColumns { * This method writes the workbook to resultDir/fileName. */ @After - public void cleanup() { - final String procName="TestXSSFSheetRemoveTable.cleanup"; + public void cleanup() throws IOException { + final String procName = "TestXSSFSheetRemoveTable.cleanup"; if (workbook == null) { System.out.println(String.format(Locale.ROOT,"%s: workbook==null",procName)); return; } - if(fileName==null) { + if(fileName == null) { System.out.println(String.format(Locale.ROOT, "%s: fileName==null",procName)); return; } - final File file=new File(resultDir,fileName); + writeFile(procName); + + workbook.close(); + } + + private void writeFile(String procName) throws IOException { + final File file = new File(resultDir,fileName); try (OutputStream fileOut = new FileOutputStream(file)) { workbook.write(fileOut); System.out.println(String.format(Locale.ROOT, "%s: test file written to %s",procName,file.getAbsolutePath())); - } catch (Exception e) { - System.err.println(e.getMessage()); - } finally { - try { - workbook.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } } } @@ -117,8 +121,8 @@ public class TestXSSFSheetShiftRowsAndColumns { */ @Test public void testNoShift() { - final String procName="testNoShift"; - fileName=procName+".xlsx"; + final String procName = "testNoShift"; + fileName = procName+".xlsx"; testCellAddresses(procName,0,0); testMergeRegion(procName,0,0); @@ -128,21 +132,20 @@ public class TestXSSFSheetShiftRowsAndColumns { @Test public void testShiftOneRowAndTestAddresses() { - final String procName="testShiftOneRowAndTestAddresses"; - fileName=procName+".xlsx"; - final int nRowsToShift=1; + final String procName = "testShiftOneRowAndTestAddresses"; + fileName = procName+".xlsx"; + final int nRowsToShift = 1; sheet.shiftRows(INSERT_ROW, numRows-1, nRowsToShift); testCellAddresses(procName,nRowsToShift,0); System.out.println(String.format(Locale.US, "%s: finished without error", procName)); } - @Ignore("currently fails") @Test public void testShiftOneRowAndTestMergeRegion() { - final String procName="testShiftOneRowAndTestMergeRegion"; - fileName=procName+".xlsx"; - final int nRowsToShift=1; + final String procName = "testShiftOneRowAndTestMergeRegion"; + fileName = procName+".xlsx"; + final int nRowsToShift = 1; sheet.shiftRows(INSERT_ROW, numRows-1, nRowsToShift); testMergeRegion(procName,nRowsToShift,0); @@ -151,24 +154,23 @@ public class TestXSSFSheetShiftRowsAndColumns { @Test public void testShiftOneColumnAndTestAddresses() { - final String procName="testShiftOneColumnAndTestAddresses"; - fileName=procName+".xlsx"; - final int nShift=1; + final String procName = "testShiftOneColumnAndTestAddresses"; + fileName = procName+".xlsx"; + final int nShift = 1; sheet.shiftColumns(INSERT_COLUMN, numCols-1, nShift); testCellAddresses(procName,0,nShift); System.out.println(String.format(Locale.US, "%s: finished without error", procName)); } - @Ignore("currently fails") @Test public void testShiftOneColumnAndTestMergeRegion() { - final String procName="testShiftOneColumnAndTestMergeRegion"; - fileName=procName+".xlsx"; - final int nShift=1; + final String procName = "testShiftOneColumnAndTestMergeRegion"; + fileName = procName+".xlsx"; + final int nShift = 1; sheet.shiftColumns(INSERT_COLUMN, numCols-1, nShift); - testMergeRegion(procName,0,nShift); + testMergeRegion(procName,0, nShift); System.out.println(String.format(Locale.US, "%s: finished without error", procName)); } @@ -176,26 +178,26 @@ public class TestXSSFSheetShiftRowsAndColumns { * Verify that the cell addresses are consistent */ private void testCellAddresses(String procName,int nRowsToShift,int nColsToShift) { - final int nNumRows=nRowsToShift+this.numCols; - final int nNumCols=nColsToShift+this.numCols; - for(int nRow=0;nRow