]> source.dussan.org Git - poi.git/commitdiff
bug 56958: patch from Yaniv Kunda: check that cells containing array formulas do...
authorJaven O'Neal <onealj@apache.org>
Sun, 19 Jun 2016 22:00:44 +0000 (22:00 +0000)
committerJaven O'Neal <onealj@apache.org>
Sun, 19 Jun 2016 22:00:44 +0000 (22:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749210 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetUpdateArrayFormulas.java

index 00124e6da1d96bdc7fa1998b437a837a8b80f2db..9f858b77948cf129ef910cfe37c83c4137130586 100644 (file)
@@ -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);
index 85f493f84759edd7e94e4897b0ac78f761ff0b1f..86779673cc32a0d16b83cf966bf75150f957abfe 100644 (file)
@@ -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();
     }