diff options
author | Javen O'Neal <onealj@apache.org> | 2016-06-19 22:00:44 +0000 |
---|---|---|
committer | Javen O'Neal <onealj@apache.org> | 2016-06-19 22:00:44 +0000 |
commit | a15ca7acfcc22aa52c5f2112ff37d9eeb3b1d7ca (patch) | |
tree | 7dca65db3f5c2d79e76637402c9b46c9f58c9eb5 /src | |
parent | 9a769fcb0bd1bb22fd1607a2a2a0fa0203e21afb (diff) | |
download | poi-a15ca7acfcc22aa52c5f2112ff37d9eeb3b1d7ca.tar.gz poi-a15ca7acfcc22aa52c5f2112ff37d9eeb3b1d7ca.zip |
bug 56958: patch from Yaniv Kunda: check that cells containing array formulas do not belong to a merged region
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749210 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r-- | src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java | 4 | ||||
-rw-r--r-- | src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetUpdateArrayFormulas.java | 61 |
2 files changed, 50 insertions, 15 deletions
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 00124e6da1..9f858b7794 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -749,9 +749,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { if (cell.isPartOfArrayFormulaGroup()) { CellRangeAddress arrayRange = cell.getArrayFormulaRange(); - if (arrayRange.getNumberOfCells() > 1 && - (arrayRange.isInRange(region.getFirstRow(), region.getFirstColumn()) || - arrayRange.isInRange(region.getFirstRow(), region.getFirstColumn()))) { + if (arrayRange.getNumberOfCells() > 1 && region.intersects(arrayRange)) { String msg = "The range " + region.formatAsString() + " intersects with a multi-cell array formula. " + "You cannot merge cells of an array."; throw new IllegalStateException(msg); diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetUpdateArrayFormulas.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetUpdateArrayFormulas.java index 85f493f847..86779673cc 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetUpdateArrayFormulas.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetUpdateArrayFormulas.java @@ -27,6 +27,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.IOException; +import java.util.Arrays; import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.formula.FormulaParseException; @@ -467,7 +468,7 @@ public abstract class BaseTestSheetUpdateArrayFormulas { } @Test - public void testModifyArrayCells_mergeCells() throws IOException { + public void testModifyArrayCells_mergeCellsSingle() throws IOException { Workbook workbook = _testDataProvider.createWorkbook(); Sheet sheet = workbook.createSheet(); assertEquals(0, sheet.getNumMergedRegions()); @@ -481,19 +482,55 @@ public abstract class BaseTestSheetUpdateArrayFormulas { assertEquals(Cell.CELL_TYPE_FORMULA, scell.getCellType()); assertTrue(scell.isPartOfArrayFormulaGroup()); assertEquals(1, sheet.getNumMergedRegions()); - - //we cannot merge cells included in an array formula - sheet.setArrayFormula("A1:A3*B1:B3", CellRangeAddress.valueOf("C1:C3")); - CellRangeAddress cra = CellRangeAddress.valueOf("C1:C3"); - try { - sheet.addMergedRegion(cra); - fail("expected exception"); - } catch (IllegalStateException e){ - String msg = "The range "+cra.formatAsString()+" intersects with a multi-cell array formula. You cannot merge cells of an array."; - assertEquals(msg, e.getMessage()); + + workbook.close(); + } + + @Test + public void testModifyArrayCells_mergeCellsMulti() throws IOException { + Workbook workbook = _testDataProvider.createWorkbook(); + Sheet sheet = workbook.createSheet(); + int expectedNumMergedRegions = 0; + assertEquals(expectedNumMergedRegions, sheet.getNumMergedRegions()); + + // we cannot merge cells included in an array formula + sheet.setArrayFormula("A1:A4*B1:B4", CellRangeAddress.valueOf("C2:F5")); + for (String ref : Arrays.asList( + "C2:F5", // identity + "D3:E4", "B1:G6", // contains + "B1:C2", "F1:G2", "F5:G6", "B5:C6", // 1x1 corner intersection + "B1:C6", "B1:G2", "F1:G6", "B5:G6", // 1-row/1-column intersection + "B1:D3", "E1:G3", "E4:G6", "B4:D6", // 2x2 corner intersection + "B1:D6", "B1:G3", "E1:G6", "B4:G6" // 2-row/2-column intersection + )) { + CellRangeAddress cra = CellRangeAddress.valueOf(ref); + try { + sheet.addMergedRegion(cra); + fail("expected exception with ref " + ref); + } catch (IllegalStateException e) { + String msg = "The range "+cra.formatAsString()+" intersects with a multi-cell array formula. You cannot merge cells of an array."; + assertEquals(msg, e.getMessage()); + } } //the number of merged regions remains the same - assertEquals(1, sheet.getNumMergedRegions()); + assertEquals(expectedNumMergedRegions, sheet.getNumMergedRegions()); + + // we can merge non-intersecting cells + for (String ref : Arrays.asList( + "C1:F1", //above + "G2:G5", //right + "C6:F6", //bottom + "B2:B5", "H7:J9")) { + CellRangeAddress cra = CellRangeAddress.valueOf(ref); + try { + sheet.addMergedRegion(cra); + expectedNumMergedRegions++; + assertEquals(expectedNumMergedRegions, sheet.getNumMergedRegions()); + } catch (IllegalStateException e) { + fail("did not expect exception with ref: " + ref + "\n" + e.getMessage()); + } + } + workbook.close(); } |