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

(-)a/src/java/org/apache/poi/ss/formula/FormulaParser.java (-3 / +3 lines)
Lines 617-632 Link Here
617
		return new ParseNode(ptg);
617
		return new ParseNode(ptg);
618
	}
618
	}
619
619
620
	private static AreaReference createAreaRef(SimpleRangePart part1, SimpleRangePart part2) {
620
	private AreaReference createAreaRef(SimpleRangePart part1, SimpleRangePart part2) {
621
		if (!part1.isCompatibleForArea(part2)) {
621
		if (!part1.isCompatibleForArea(part2)) {
622
			throw new FormulaParseException("has incompatible parts: '"
622
			throw new FormulaParseException("has incompatible parts: '"
623
					+ part1.getRep() + "' and '" + part2.getRep() + "'.");
623
					+ part1.getRep() + "' and '" + part2.getRep() + "'.");
624
		}
624
		}
625
		if (part1.isRow()) {
625
		if (part1.isRow()) {
626
			return AreaReference.getWholeRow(part1.getRep(), part2.getRep());
626
			return AreaReference.getWholeRow(_ssVersion, part1.getRep(), part2.getRep());
627
		}
627
		}
628
		if (part1.isColumn()) {
628
		if (part1.isColumn()) {
629
			return AreaReference.getWholeColumn(part1.getRep(), part2.getRep());
629
			return AreaReference.getWholeColumn(_ssVersion, part1.getRep(), part2.getRep());
630
		}
630
		}
631
		return new AreaReference(part1.getCellReference(), part2.getCellReference());
631
		return new AreaReference(part1.getCellReference(), part2.getCellReference());
632
	}
632
	}
(-)a/src/java/org/apache/poi/ss/formula/ptg/AreaPtgBase.java (-1 / +2 lines)
Lines 17-22 Link Here
17
17
18
package org.apache.poi.ss.formula.ptg;
18
package org.apache.poi.ss.formula.ptg;
19
19
20
import org.apache.poi.ss.SpreadsheetVersion;
20
import org.apache.poi.ss.util.AreaReference;
21
import org.apache.poi.ss.util.AreaReference;
21
import org.apache.poi.ss.util.CellReference;
22
import org.apache.poi.ss.util.CellReference;
22
import org.apache.poi.util.BitField;
23
import org.apache.poi.util.BitField;
Lines 256-262 Link Here
256
		CellReference topLeft = new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative());
257
		CellReference topLeft = new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative());
257
		CellReference botRight = new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative());
258
		CellReference botRight = new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative());
258
259
259
		if(AreaReference.isWholeColumnReference(topLeft, botRight)) {
260
		if(AreaReference.isWholeColumnReference(SpreadsheetVersion.EXCEL97, topLeft, botRight)) {
260
			return (new AreaReference(topLeft, botRight)).formatAsString();
261
			return (new AreaReference(topLeft, botRight)).formatAsString();
261
		}
262
		}
262
		return topLeft.formatAsString() + ":" + botRight.formatAsString();
263
		return topLeft.formatAsString() + ":" + botRight.formatAsString();
(-)a/src/java/org/apache/poi/ss/util/AreaReference.java (-8 / +18 lines)
Lines 35-47 Link Here
35
    private final CellReference _firstCell;
35
    private final CellReference _firstCell;
36
    private final CellReference _lastCell;
36
    private final CellReference _lastCell;
37
    private final boolean _isSingleCell;
37
    private final boolean _isSingleCell;
38
    private SpreadsheetVersion _version;
38
39
40
    /**
41
     * @deprecated Prefer supplying a version.
42
     */
43
    @Deprecated
44
    public AreaReference(String reference) {
45
        this(reference, SpreadsheetVersion.EXCEL97);
46
    }
47
    
39
    /**
48
    /**
40
     * Create an area ref from a string representation.  Sheet names containing special characters should be
49
     * Create an area ref from a string representation.  Sheet names containing special characters should be
41
     * delimited and escaped as per normal syntax rules for formulas.<br/> 
50
     * delimited and escaped as per normal syntax rules for formulas.<br/> 
42
     * The area reference must be contiguous (i.e. represent a single rectangle, not a union of rectangles)
51
     * The area reference must be contiguous (i.e. represent a single rectangle, not a union of rectangles)
43
     */
52
     */
44
    public AreaReference(String reference) {
53
    public AreaReference(String reference, SpreadsheetVersion version) {
54
        _version = version;
45
        if(! isContiguous(reference)) {
55
        if(! isContiguous(reference)) {
46
            throw new IllegalArgumentException(
56
            throw new IllegalArgumentException(
47
                    "References passed to the AreaReference must be contiguous, " +
57
                    "References passed to the AreaReference must be contiguous, " +
Lines 169-198 Link Here
169
       return false;
179
       return false;
170
    }
180
    }
171
181
172
    public static AreaReference getWholeRow(String start, String end) {
182
    public static AreaReference getWholeRow(SpreadsheetVersion version, String start, String end) {
173
        return new AreaReference("$A" + start + ":$IV" + end);
183
        return new AreaReference("$A" + start + ":$" + version.getLastColumnName() + end, version);
174
    }
184
    }
175
185
176
    public static AreaReference getWholeColumn(String start, String end) {
186
    public static AreaReference getWholeColumn(SpreadsheetVersion version, String start, String end) {
177
        return new AreaReference(start + "$1:" + end + "$65536");
187
        return new AreaReference(start + "$1:" + end + "$" + version.getMaxRows(), version);
178
    }
188
    }
179
189
180
    /**
190
    /**
181
     * Is the reference for a whole-column reference,
191
     * Is the reference for a whole-column reference,
182
     *  such as C:C or D:G ?
192
     *  such as C:C or D:G ?
183
     */
193
     */
184
    public static boolean isWholeColumnReference(CellReference topLeft, CellReference botRight) {
194
    public static boolean isWholeColumnReference(SpreadsheetVersion version, CellReference topLeft, CellReference botRight) {
185
        // These are represented as something like
195
        // These are represented as something like
186
        //   C$1:C$65535 or D$1:F$0
196
        //   C$1:C$65535 or D$1:F$0
187
        // i.e. absolute from 1st row to 0th one
197
        // i.e. absolute from 1st row to 0th one
188
        if(topLeft.getRow() == 0 && topLeft.isRowAbsolute() &&
198
        if(topLeft.getRow() == 0 && topLeft.isRowAbsolute() &&
189
            botRight.getRow() == SpreadsheetVersion.EXCEL97.getLastRowIndex() && botRight.isRowAbsolute()) {
199
            botRight.getRow() == version.getLastRowIndex() && botRight.isRowAbsolute()) {
190
            return true;
200
            return true;
191
        }
201
        }
192
        return false;
202
        return false;
193
    }
203
    }
194
    public boolean isWholeColumnReference() {
204
    public boolean isWholeColumnReference() {
195
        return isWholeColumnReference(_firstCell, _lastCell);
205
        return isWholeColumnReference(_version, _firstCell, _lastCell);
196
    }
206
    }
197
207
198
    /**
208
    /**
(-)a/src/testcases/org/apache/poi/ss/formula/TestFormulaParser.java (+48 lines)
Line 0 Link Here
1
package org.apache.poi.ss.formula;
2
3
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
4
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
5
import org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook;
6
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
7
8
import junit.framework.TestCase;
9
10
/**
11
 * Test {@link FormulaParser}'s handling of row numbers at the edge of the
12
 * HSSF/XSSF ranges.
13
 * 
14
 * @author David North
15
 */
16
public class TestFormulaParser extends TestCase {
17
18
    public void testHSSFFailsForOver65536() {
19
        FormulaParsingWorkbook workbook = HSSFEvaluationWorkbook.create(new HSSFWorkbook());
20
        try {
21
            FormulaParser.parse("Sheet1!1:65537", workbook, FormulaType.CELL, 0);
22
            fail("Expected exception");
23
        }
24
        catch (FormulaParseException expected) {
25
        }
26
    }
27
28
    public void testHSSFPassCase() {
29
        FormulaParsingWorkbook workbook = HSSFEvaluationWorkbook.create(new HSSFWorkbook());
30
        FormulaParser.parse("Sheet1!1:65536", workbook, FormulaType.CELL, 0);
31
    }
32
33
    public void testXSSFWorksForOver65536() {
34
        FormulaParsingWorkbook workbook = XSSFEvaluationWorkbook.create(new XSSFWorkbook());
35
        FormulaParser.parse("Sheet1!1:65537", workbook, FormulaType.CELL, 0);
36
    }
37
38
    public void testXSSFFailCase() {
39
        FormulaParsingWorkbook workbook = XSSFEvaluationWorkbook.create(new XSSFWorkbook());
40
        try {
41
            FormulaParser.parse("Sheet1!1:1048577", workbook, FormulaType.CELL, 0); // one more than max rows.
42
            fail("Expected exception");
43
        }
44
        catch (FormulaParseException expected) {
45
        }
46
    }
47
48
}
(-)a/src/testcases/org/apache/poi/ss/util/TestAreaReference.java (+44 lines)
Line 0 Link Here
1
package org.apache.poi.ss.util;
2
3
import org.apache.poi.ss.SpreadsheetVersion;
4
5
import junit.framework.TestCase;
6
7
/**
8
 * Test for {@link AreaReference} handling of max rows.
9
 * 
10
 * @author David North
11
 */
12
public class TestAreaReference extends TestCase {
13
    
14
    public void testWholeColumn() {
15
        AreaReference oldStyle = AreaReference.getWholeColumn(SpreadsheetVersion.EXCEL97, "A", "B");
16
        assertEquals(0, oldStyle.getFirstCell().getCol());
17
        assertEquals(0, oldStyle.getFirstCell().getRow());
18
        assertEquals(1, oldStyle.getLastCell().getCol());
19
        assertEquals(SpreadsheetVersion.EXCEL97.getLastRowIndex(), oldStyle.getLastCell().getRow());
20
        assertTrue(oldStyle.isWholeColumnReference());
21
        
22
        AreaReference newStyle = AreaReference.getWholeColumn(SpreadsheetVersion.EXCEL2007, "A", "B");
23
        assertEquals(0, newStyle.getFirstCell().getCol());
24
        assertEquals(0, newStyle.getFirstCell().getRow());
25
        assertEquals(1, newStyle.getLastCell().getCol());
26
        assertEquals(SpreadsheetVersion.EXCEL2007.getLastRowIndex(), newStyle.getLastCell().getRow());
27
        assertTrue(newStyle.isWholeColumnReference());
28
    }
29
    
30
    public void testWholeRow() {
31
        AreaReference oldStyle = AreaReference.getWholeRow(SpreadsheetVersion.EXCEL97, "1", "2");
32
        assertEquals(0, oldStyle.getFirstCell().getCol());
33
        assertEquals(0, oldStyle.getFirstCell().getRow());
34
        assertEquals(SpreadsheetVersion.EXCEL97.getLastColumnIndex(), oldStyle.getLastCell().getCol());
35
        assertEquals(1, oldStyle.getLastCell().getRow());
36
        
37
        AreaReference newStyle = AreaReference.getWholeRow(SpreadsheetVersion.EXCEL2007, "1", "2");
38
        assertEquals(0, newStyle.getFirstCell().getCol());
39
        assertEquals(0, newStyle.getFirstCell().getRow());
40
        assertEquals(SpreadsheetVersion.EXCEL2007.getLastColumnIndex(), newStyle.getLastCell().getCol());
41
        assertEquals(1, newStyle.getLastCell().getRow());
42
    }
43
44
}

Return to bug 56328