]> source.dussan.org Git - poi.git/commitdiff
Bug 63371: addMergedRegion does not update CTMergeCells.getCount
authorDominik Stadler <centic@apache.org>
Tue, 23 Apr 2019 20:27:53 +0000 (20:27 +0000)
committerDominik Stadler <centic@apache.org>
Tue, 23 Apr 2019 20:27:53 +0000 (20:27 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1858022 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

index 955fcdec7c04d0f79b11f7b590110251c9ce5fec..563df1ecd0b06947a0b979f3ddd9e143ecaf2842 100644 (file)
@@ -416,7 +416,12 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
         CTMergeCells ctMergeCells = worksheet.isSetMergeCells() ? worksheet.getMergeCells() : worksheet.addNewMergeCells();
         CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell();
         ctMergeCell.setRef(region.formatAsString());
-        return ctMergeCells.sizeOfMergeCellArray()-1;
+        final int numMergeRegions=ctMergeCells.sizeOfMergeCellArray();
+
+        // also adjust the number of merged regions overall
+        ctMergeCells.setCount(numMergeRegions);
+
+        return numMergeRegions-1;
     }
 
     /**
index 683ad8a0e61386dfddde14e6bc28679078be4875..65227c0b78fec0e39f008932a29be4cb976fc695 100644 (file)
@@ -131,6 +131,8 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcCell;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMergeCell;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMergeCells;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTFontImpl;
 import org.xml.sax.InputSource;
@@ -3431,4 +3433,33 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
         assertEquals("The data in the text-file should exactly match the data that we read from the workbook", testData, value);
     }
 
+
+    @Test
+    public void bug63371() {
+        XSSFWorkbook wb = new XSSFWorkbook();
+        XSSFSheet sheet = wb.createSheet();
+
+        CellRangeAddress region = new CellRangeAddress(1, 1, 1, 2);
+        assertEquals(0, sheet.addMergedRegion(region));
+        //System.out.println(String.format("%s: index=%d", "testAddMergedRegion", index));
+
+        final List<CellRangeAddress> ranges = sheet.getMergedRegions();
+        final int numMergedRegions = sheet.getNumMergedRegions();
+        final CTWorksheet ctSheet = sheet.getCTWorksheet();
+        final CTMergeCells ctMergeCells = ctSheet.getMergeCells();
+        final List<CTMergeCell> ctMergeCellList = ctMergeCells.getMergeCellList();
+        final long ctMergeCellCount = ctMergeCells.getCount();
+        final int ctMergeCellListSize = ctMergeCellList.size();
+
+        /*System.out.println(String.format("\ntestMergeRegions(%s)", "After adding first region"));
+        System.out.println(String.format("ranges.size=%d", ranges.size()));
+        System.out.println(String.format("numMergedRegions=%d", numMergedRegions));
+        System.out.println(String.format("ctMergeCellCount=%d", ctMergeCellCount));
+        System.out.println(String.format("ctMergeCellListSize=%d", ctMergeCellListSize));*/
+
+        assertEquals(1, ranges.size());
+        assertEquals(1, numMergedRegions);
+        assertEquals(1, ctMergeCellCount);
+        assertEquals(1, ctMergeCellListSize);
+    }
 }