bug 58442: in-place reorganize an AreaPtg as upper-left and lower-right

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1711592 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2015-10-31 11:10:28 +00:00
parent 043a86e243
commit 5f23aa95f7
2 changed files with 43 additions and 2 deletions

View File

@ -44,9 +44,9 @@ public abstract class AreaPtgBase extends OperandPtg implements AreaI {
/** zero based, unsigned 16 bit */
private int field_2_last_row;
/** zero based, unsigned 8 bit */
private int field_3_first_column;
private int field_3_first_column; //BitFields: (first row relative, first col relative, first column number)
/** zero based, unsigned 8 bit */
private int field_4_last_column;
private int field_4_last_column; //BitFields: (last row relative, last col relative, last column number)
private final static BitField rowRelative = BitFieldFactory.getInstance(0x8000);
private final static BitField colRelative = BitFieldFactory.getInstance(0x4000);
@ -96,6 +96,35 @@ public abstract class AreaPtgBase extends OperandPtg implements AreaI {
setLastColRelative(firstColRelative);
}
}
/**
* Sort the first and last row and columns in-place to the preferred (top left:bottom right) order
* Note: Sort only occurs when an instance is constructed or when this method is called.
*
* <p>For example, <code>$E5:B$10</code> becomes <code>B5:$E$10</code></p>
*/
public void sortTopLeftToBottomRight() {
if (getFirstRow() > getLastRow()) {
//swap first row and last row numbers and relativity
//Note: cannot just swap the fields because row relativity is stored in fields 3 and 4
final int firstRow = getFirstRow();
final boolean firstRowRel = isFirstRowRelative();
setFirstRow(getLastRow());
setFirstRowRelative(isLastRowRelative());
setLastRow(firstRow);
setLastRowRelative(firstRowRel);
}
if (getFirstColumn() > getLastColumn()) {
//swap first column and last column numbers and relativity
//Note: cannot just swap the fields because row relativity is stored in fields 3 and 4
final int firstCol = getFirstColumn();
final boolean firstColRel = isFirstColRelative();
setFirstColumn(getLastColumn());
setFirstColRelative(isLastColRelative());
setLastColumn(firstCol);
setLastColRelative(firstColRel);
}
}
protected final void readCoordinates(LittleEndianInput in) {
field_1_first_row = in.readUShort();

View File

@ -33,6 +33,7 @@ public final class TestAreaPtg extends TestCase {
AreaPtg relative;
AreaPtg absolute;
@Override
protected void setUp() {
short firstRow=5;
short lastRow=13;
@ -41,6 +42,17 @@ public final class TestAreaPtg extends TestCase {
relative = new AreaPtg(firstRow,lastRow,firstCol,lastCol,true,true,true,true);
absolute = new AreaPtg(firstRow,lastRow,firstCol,lastCol,false,false,false,false);
}
public static void testSortTopLeftToBottomRight() {
AreaPtg ptg = new AreaPtg("A$1:$B5");
assertEquals("A$1:$B5", ptg.toFormulaString());
ptg.setFirstColumn(3);
assertEquals("Area Ptg should not implicitly re-sort itself (except during construction)",
"D$1:$B5", ptg.toFormulaString());
ptg.sortTopLeftToBottomRight();
assertEquals("Area Ptg should restore itself to top-left to lower-right order when explicitly asked",
"$B$1:D5", ptg.toFormulaString());
}
public void testSetColumnsAbsolute()
{