]> source.dussan.org Git - poi.git/commitdiff
bug 58442: in-place reorganize an AreaPtg as upper-left and lower-right
authorJaven O'Neal <onealj@apache.org>
Sat, 31 Oct 2015 11:10:28 +0000 (11:10 +0000)
committerJaven O'Neal <onealj@apache.org>
Sat, 31 Oct 2015 11:10:28 +0000 (11:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1711592 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/formula/ptg/AreaPtgBase.java
src/testcases/org/apache/poi/ss/formula/ptg/TestAreaPtg.java

index 96eda875764e946dddeef7468d07bd29d43a4a14..bc808b0f026497210a4b71fdb54f3831ebedfe1b 100644 (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();
index 0dcbeafc5c2dc62d0fe4f8e5dbdd7b842dbab6c8..8cf4d6aafda84ec081e9f98601c6e1b2fb98b75e 100644 (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()
        {