diff options
author | PJ Fanning <fanningpj@apache.org> | 2020-12-11 17:51:06 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2020-12-11 17:51:06 +0000 |
commit | 1f072afe18b3022f90a7f2dae52b9606b459e045 (patch) | |
tree | 68c60e726d2689fe0875b31ed49f9ed290c94b59 | |
parent | c0ecc83ee4e52e8feffa4737e276420ac4451fca (diff) | |
download | poi-1f072afe18b3022f90a7f2dae52b9606b459e045.tar.gz poi-1f072afe18b3022f90a7f2dae52b9606b459e045.zip |
[bug-60397] improve speed of excel cell merge. Thanks to Alex Herve
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884329 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java | 13 | ||||
-rw-r--r-- | src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java | 54 |
2 files changed, 64 insertions, 3 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 155aa70556..7eb2a45b39 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -379,12 +379,19 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { CTMergeCells ctMergeCells = worksheet.isSetMergeCells() ? worksheet.getMergeCells() : worksheet.addNewMergeCells(); CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell(); ctMergeCell.setRef(region.formatAsString()); - final int numMergeRegions=ctMergeCells.sizeOfMergeCellArray(); + + long count = ctMergeCells.getCount(); + + if (count == 0) { + count=ctMergeCells.sizeOfMergeCellArray(); + } else { + count++; + } // also adjust the number of merged regions overall - ctMergeCells.setCount(numMergeRegions); + ctMergeCells.setCount(count); - return numMergeRegions-1; + return Math.toIntExact(count-1); } /** diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index aff4e21f97..6fe63c5e94 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -3450,6 +3450,60 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(1, numMergedRegions); assertEquals(1, ctMergeCellCount); assertEquals(1, ctMergeCellListSize); + + } + } + + @Test + public void bug60397() throws IOException { + try (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)); + + List<CellRangeAddress> ranges = sheet.getMergedRegions(); + int numMergedRegions = sheet.getNumMergedRegions(); + CTWorksheet ctSheet = sheet.getCTWorksheet(); + CTMergeCells ctMergeCells = ctSheet.getMergeCells(); + List<CTMergeCell> ctMergeCellList = ctMergeCells.getMergeCellList(); + long ctMergeCellCount = ctMergeCells.getCount(); + 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); + + CellRangeAddress region2 = new CellRangeAddress(1, 2, 4, 6); + assertEquals(1, sheet.addMergedRegion(region2)); + //System.out.println(String.format("%s: index=%d", "testAddMergedRegion", index)); + + ranges = sheet.getMergedRegions(); + numMergedRegions = sheet.getNumMergedRegions(); + ctSheet = sheet.getCTWorksheet(); + ctMergeCells = ctSheet.getMergeCells(); + ctMergeCellList = ctMergeCells.getMergeCellList(); + ctMergeCellCount = ctMergeCells.getCount(); + ctMergeCellListSize = ctMergeCellList.size(); + + /*System.out.println(String.format("\ntestMergeRegions(%s)", "After adding second 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(2, ranges.size()); + assertEquals(2, numMergedRegions); + assertEquals(2, ctMergeCellCount); + assertEquals(2, ctMergeCellListSize); } } |