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 4156433bd..728fedf86 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -1090,7 +1090,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * this each time. * * @return the merged region at the specified index - * @throws IllegalStateException if this worksheet does not contain merged regions */ @Override public CellRangeAddress getMergedRegion(int index) { @@ -1107,15 +1106,14 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * faster than calling {@link #getMergedRegion(int)} each time. * * @return the list of merged regions - * @throws IllegalStateException if this worksheet does not contain merged regions */ @SuppressWarnings("deprecation") @Override public List getMergedRegions() { - CTMergeCells ctMergeCells = worksheet.getMergeCells(); - if(ctMergeCells == null) throw new IllegalStateException("This worksheet does not contain merged regions"); - List addresses = new ArrayList(); + CTMergeCells ctMergeCells = worksheet.getMergeCells(); + if(ctMergeCells == null) return addresses; + for(CTMergeCell ctMergeCell : ctMergeCells.getMergeCellArray()) { String ref = ctMergeCell.getRef(); addresses.add(CellRangeAddress.valueOf(ref)); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 8e77b2c49..aed446562 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -1382,4 +1382,11 @@ public final class TestXSSFSheet extends BaseTestSheet { XSSFSheet sheet = wb.createSheet(); assertNotNull(sheet.createComment()); } + + @Test + public void testNoMergedRegionsIsEmptyList() { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet sheet = wb.createSheet(); + assertTrue(sheet.getMergedRegions().isEmpty()); + } } \ No newline at end of file diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index 4b7d8491b..723bf0d3e 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -657,6 +657,13 @@ public final class TestHSSFSheet extends BaseTestSheet { assertTrue(sheet3.getColumnWidth(0) >= minWithRow1And2); assertTrue(sheet3.getColumnWidth(0) <= maxWithRow1And2); } + + @Test + public void testNoMergedRegionsIsEmptyList() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet s = wb.createSheet("Sheet1"); + assertTrue(s.getMergedRegions().isEmpty()); + } @Test public void autoSizeDate() throws Exception {