View | Details | Raw Unified | Return to bug 56958
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (-6 / +3 lines)
Lines 670-681 Link Here
670
670
671
                if (cell.isPartOfArrayFormulaGroup()) {
671
                if (cell.isPartOfArrayFormulaGroup()) {
672
                    CellRangeAddress arrayRange = cell.getArrayFormulaRange();
672
                    CellRangeAddress arrayRange = cell.getArrayFormulaRange();
673
                    if (arrayRange.getNumberOfCells() > 1 &&
673
                    if (arrayRange.getNumberOfCells() > 1 && arrayRange.intersects(region)) {
674
                            (arrayRange.isInRange(region.getFirstRow(), region.getFirstColumn()) ||
674
                        throw new IllegalStateException("The range " + region.formatAsString()
675
                                    arrayRange.isInRange(region.getFirstRow(), region.getFirstColumn()))) {
675
                                + " intersects with a multi-cell array formula. You cannot merge cells of an array.");
676
                        String msg = "The range " + region.formatAsString() + " intersects with a multi-cell array formula. " +
677
                                "You cannot merge cells of an array.";
678
                        throw new IllegalStateException(msg);
679
                    }
676
                    }
680
                }
677
                }
681
            }
678
            }
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (-6 / +3 lines)
Lines 304-315 Link Here
304
304
305
                if(cell.isPartOfArrayFormulaGroup()){
305
                if(cell.isPartOfArrayFormulaGroup()){
306
                    CellRangeAddress arrayRange = cell.getArrayFormulaRange();
306
                    CellRangeAddress arrayRange = cell.getArrayFormulaRange();
307
                    if (arrayRange.getNumberOfCells() > 1 &&
307
                    if (arrayRange.getNumberOfCells() > 1 && arrayRange.intersects(region)) {
308
                            ( arrayRange.isInRange(region.getFirstRow(), region.getFirstColumn()) ||
308
                        throw new IllegalStateException("The range " + region.formatAsString()
309
                              arrayRange.isInRange(region.getFirstRow(), region.getFirstColumn()))  ){
309
                                + " intersects with a multi-cell array formula. You cannot merge cells of an array.");
310
                        String msg = "The range " + region.formatAsString() + " intersects with a multi-cell array formula. " +
311
                                "You cannot merge cells of an array.";
312
                        throw new IllegalStateException(msg);
313
                    }
310
                    }
314
                }
311
                }
315
            }
312
            }
(-)src/java/org/apache/poi/ss/util/CellRangeAddressBase.java (-3 / +16 lines)
Lines 23-29 Link Here
23
/**
23
/**
24
 * See OOO documentation: excelfileformat.pdf sec 2.5.14 - 'Cell Range Address'<p/>
24
 * See OOO documentation: excelfileformat.pdf sec 2.5.14 - 'Cell Range Address'<p/>
25
 *
25
 *
26
 * Common subclass of 8-bit and 16-bit versions
26
 * Common superclass of 8-bit and 16-bit versions
27
 *
27
 *
28
 * @author Josh Micich
28
 * @author Josh Micich
29
 */
29
 */
Lines 124-132 Link Here
124
	public boolean isInRange(int rowInd, int colInd) {
124
	public boolean isInRange(int rowInd, int colInd) {
125
		return _firstRow <= rowInd && rowInd <= _lastRow &&
125
		return _firstRow <= rowInd && rowInd <= _lastRow &&
126
				_firstCol <= colInd && colInd <= _lastCol;
126
				_firstCol <= colInd && colInd <= _lastCol;
127
	}
127
	}
128
129
    /**
130
     * Determines if the given range intersects with the this range.
131
     *
132
     * @param other other range
133
     * @return True if the ranges intersect, false otherwise.
134
     */
135
    public boolean intersects(CellRangeAddressBase other) {
136
        return this._firstRow <= other._lastRow &&
137
                this._firstCol <= other._lastCol &&
138
                other._firstRow <= this._lastRow &&
139
                other._firstCol <= this._lastCol;
140
    }
128
141
129
	/**
142
    /**
130
	 * @param firstCol column number for the upper left hand corner
143
	 * @param firstCol column number for the upper left hand corner
131
	 */
144
	 */
132
	public final void setFirstColumn(int firstCol) {
145
	public final void setFirstColumn(int firstCol) {
(-)src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetUpdateArrayFormulas.java (-11 / +36 lines)
Lines 25-30 Link Here
25
import org.apache.poi.ss.util.CellRangeAddress;
25
import org.apache.poi.ss.util.CellRangeAddress;
26
import org.apache.poi.ss.util.CellReference;
26
import org.apache.poi.ss.util.CellReference;
27
27
28
import java.util.Arrays;
29
28
/**
30
/**
29
 * Common superclass for testing usermodel API for array formulas.<br/>
31
 * Common superclass for testing usermodel API for array formulas.<br/>
30
 * Formula evaluation is not tested here.
32
 * Formula evaluation is not tested here.
Lines 438-456 Link Here
438
        assertTrue(scell.isPartOfArrayFormulaGroup());
440
        assertTrue(scell.isPartOfArrayFormulaGroup());
439
        assertEquals(1, sheet.getNumMergedRegions());
441
        assertEquals(1, sheet.getNumMergedRegions());
440
442
441
        //we cannot merge cells included in an array formula
443
        // we cannot merge cells included in an array formula
442
        CellRange<? extends Cell> mrange =
444
        CellRange<? extends Cell> mrange =
443
                sheet.setArrayFormula("A1:A3*B1:B3", CellRangeAddress.valueOf("C1:C3"));
445
                sheet.setArrayFormula("A1:A3*B1:B3", CellRangeAddress.valueOf("C2:F5"));
444
        CellRangeAddress cra = CellRangeAddress.valueOf("C1:C3");
446
        for (String ref : Arrays.asList(
447
                "C2:F5", // identity
448
                "D3:E4", "B1:G6", // contains
449
                "B1:C2", "F1:G2", "F5:G6", "B5:C6", // 1x1 corner intersection
450
                "B1:C6", "B1:G2", "F1:G6", "B5:G6", // 1-row/1-column intersection
451
                "B1:D3", "E1:G3", "E4:G6", "B4:D6", // 2x2 corner intersection
452
                "B1:D6", "B1:G3", "E1:G6", "B4:G6" // 2-row/2-column intersection
453
        )) {
454
            CellRangeAddress cra = CellRangeAddress.valueOf(ref);
445
        try {
455
            try {
446
            sheet.addMergedRegion(cra);
456
                sheet.addMergedRegion(cra);
447
            fail("expected exception");
457
                fail("expected exception");
448
        } catch (IllegalStateException e){
458
            } catch (IllegalStateException e){
449
            String msg = "The range "+cra.formatAsString()+" intersects with a multi-cell array formula. You cannot merge cells of an array.";
459
                String expectedMessage = "The range " + cra.formatAsString()
450
            assertEquals(msg, e.getMessage());
460
                        + " intersects with a multi-cell array formula. You cannot merge cells of an array.";
461
                assertEquals(expectedMessage, e.getMessage());
451
        }
462
            }
463
        }
464
        int expectedNumMergedRegions = 1;
452
        //the number of merged regions remains the same
465
        //the number of merged regions remains the same
453
        assertEquals(1, sheet.getNumMergedRegions());
466
        assertEquals(expectedNumMergedRegions, sheet.getNumMergedRegions());
467
468
        // we can merge non-intersecting cells
469
        for (String ref : Arrays.asList("C1:F1", "G2:G5", "C6:F6", "B2:B5", "H7:J9")) {
470
            CellRangeAddress cra = CellRangeAddress.valueOf(ref);
471
            try {
472
                sheet.addMergedRegion(cra);
473
                expectedNumMergedRegions++;
474
                assertEquals(expectedNumMergedRegions, sheet.getNumMergedRegions());
475
            } catch (IllegalStateException e) {
476
                fail("did not expect exception");
477
            }
478
        }
454
    }
479
    }
455
480
456
    public void testModifyArrayCells_shiftRows(){
481
    public void testModifyArrayCells_shiftRows(){

Return to bug 56958