Make XSSF and HSSF consistent in getMergedRegions - return empty list if there are none, rather than an exception. Tests for this.

https://bz.apache.org/bugzilla/show_bug.cgi?id=58350

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1704839 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
David North 2015-09-23 12:53:13 +00:00
parent 303cf2a8ed
commit 11f1887784
3 changed files with 17 additions and 5 deletions

View File

@ -1090,7 +1090,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
* this each time. * this each time.
* *
* @return the merged region at the specified index * @return the merged region at the specified index
* @throws IllegalStateException if this worksheet does not contain merged regions
*/ */
@Override @Override
public CellRangeAddress getMergedRegion(int index) { public CellRangeAddress getMergedRegion(int index) {
@ -1107,15 +1106,14 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
* faster than calling {@link #getMergedRegion(int)} each time. * faster than calling {@link #getMergedRegion(int)} each time.
* *
* @return the list of merged regions * @return the list of merged regions
* @throws IllegalStateException if this worksheet does not contain merged regions
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
public List<CellRangeAddress> getMergedRegions() { public List<CellRangeAddress> getMergedRegions() {
CTMergeCells ctMergeCells = worksheet.getMergeCells();
if(ctMergeCells == null) throw new IllegalStateException("This worksheet does not contain merged regions");
List<CellRangeAddress> addresses = new ArrayList<CellRangeAddress>(); List<CellRangeAddress> addresses = new ArrayList<CellRangeAddress>();
CTMergeCells ctMergeCells = worksheet.getMergeCells();
if(ctMergeCells == null) return addresses;
for(CTMergeCell ctMergeCell : ctMergeCells.getMergeCellArray()) { for(CTMergeCell ctMergeCell : ctMergeCells.getMergeCellArray()) {
String ref = ctMergeCell.getRef(); String ref = ctMergeCell.getRef();
addresses.add(CellRangeAddress.valueOf(ref)); addresses.add(CellRangeAddress.valueOf(ref));

View File

@ -1382,4 +1382,11 @@ public final class TestXSSFSheet extends BaseTestSheet {
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
assertNotNull(sheet.createComment()); assertNotNull(sheet.createComment());
} }
@Test
public void testNoMergedRegionsIsEmptyList() {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
assertTrue(sheet.getMergedRegions().isEmpty());
}
} }

View File

@ -658,6 +658,13 @@ public final class TestHSSFSheet extends BaseTestSheet {
assertTrue(sheet3.getColumnWidth(0) <= maxWithRow1And2); assertTrue(sheet3.getColumnWidth(0) <= maxWithRow1And2);
} }
@Test
public void testNoMergedRegionsIsEmptyList() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet("Sheet1");
assertTrue(s.getMergedRegions().isEmpty());
}
@Test @Test
public void autoSizeDate() throws Exception { public void autoSizeDate() throws Exception {
HSSFWorkbook wb = new HSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook();