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

(-)src/java/org/apache/poi/ss/formula/ptg/AreaPtgBase.java (-2 / +31 lines)
Lines 44-52 Link Here
44
	/** zero based, unsigned 16 bit */
44
	/** zero based, unsigned 16 bit */
45
	private int             field_2_last_row;
45
	private int             field_2_last_row;
46
	/** zero based, unsigned 8 bit */
46
	/** zero based, unsigned 8 bit */
47
	private int             field_3_first_column;
47
	private int             field_3_first_column; //BitFields: (first row relative, first col relative, first column number)
48
	/** zero based, unsigned 8 bit */
48
	/** zero based, unsigned 8 bit */
49
	private int             field_4_last_column;
49
	private int             field_4_last_column; //BitFields: (last row relative, last col relative, last column number)
50
50
51
	private final static BitField   rowRelative = BitFieldFactory.getInstance(0x8000);
51
	private final static BitField   rowRelative = BitFieldFactory.getInstance(0x8000);
52
	private final static BitField   colRelative = BitFieldFactory.getInstance(0x4000);
52
	private final static BitField   colRelative = BitFieldFactory.getInstance(0x4000);
Lines 96-101 Link Here
96
			setLastColRelative(firstColRelative);
96
			setLastColRelative(firstColRelative);
97
		}
97
		}
98
	}
98
	}
99
	
100
	/**
101
	 * Sort the first and last row and columns in-place to the preferred (top left:bottom right) order
102
	 * Note: Sort only occurs when an instance is constructed or when this method is called.
103
	 * 
104
	 * <p>For example, <code>$E5:B$10</code> becomes <code>B5:$E$10</code></p>
105
	 */
106
	public void sortTopLeftToBottomRight() {
107
		if (getFirstRow() > getLastRow()) {
108
			//swap first row and last row numbers and relativity
109
			//Note: cannot just swap the fields because row relativity is stored in fields 3 and 4
110
			final int firstRow = getFirstRow();
111
			final boolean firstRowRel = isFirstRowRelative();
112
			setFirstRow(getLastRow());
113
			setFirstRowRelative(isLastRowRelative());
114
			setLastRow(firstRow);
115
			setLastRowRelative(firstRowRel);
116
		}
117
		if (getFirstColumn() > getLastColumn()) {
118
			//swap first column and last column numbers and relativity
119
			//Note: cannot just swap the fields because row relativity is stored in fields 3 and 4
120
			final int firstCol = getFirstColumn();
121
			final boolean firstColRel = isFirstColRelative();
122
			setFirstColumn(getLastColumn());
123
			setFirstColRelative(isLastColRelative());
124
			setLastColumn(firstCol);
125
			setLastColRelative(firstColRel);
126
		}
127
	}
99
128
100
	protected final void readCoordinates(LittleEndianInput in)  {
129
	protected final void readCoordinates(LittleEndianInput in)  {
101
		field_1_first_row = in.readUShort();
130
		field_1_first_row = in.readUShort();
(-)src/testcases/org/apache/poi/ss/formula/ptg/TestAreaPtg.java (+12 lines)
Lines 33-38 Link Here
33
	AreaPtg relative;
33
	AreaPtg relative;
34
	AreaPtg absolute;
34
	AreaPtg absolute;
35
	
35
	
36
	@Override
36
	protected void setUp() {
37
	protected void setUp() {
37
		short firstRow=5;
38
		short firstRow=5;
38
		short lastRow=13;
39
		short lastRow=13;
Lines 41-46 Link Here
41
		relative = new AreaPtg(firstRow,lastRow,firstCol,lastCol,true,true,true,true);
42
		relative = new AreaPtg(firstRow,lastRow,firstCol,lastCol,true,true,true,true);
42
		absolute = new AreaPtg(firstRow,lastRow,firstCol,lastCol,false,false,false,false);
43
		absolute = new AreaPtg(firstRow,lastRow,firstCol,lastCol,false,false,false,false);
43
	}
44
	}
45
	
46
	public static void testSortTopLeftToBottomRight() {
47
	    AreaPtg ptg = new AreaPtg("A$1:$B5");
48
	    assertEquals("A$1:$B5", ptg.toFormulaString());
49
	    ptg.setFirstColumn(3);
50
	    assertEquals("Area Ptg should not implicitly re-sort itself (except during construction)",
51
	            "D$1:$B5", ptg.toFormulaString());
52
	    ptg.sortTopLeftToBottomRight();
53
	    assertEquals("Area Ptg should restore itself to top-left to lower-right order when explicitly asked",
54
	            "$B$1:D5", ptg.toFormulaString());
55
	}
44
56
45
	public void testSetColumnsAbsolute()
57
	public void testSetColumnsAbsolute()
46
	{
58
	{

Return to bug 58442