Author: nick
Date: Fri Aug 23 19:00:01 2013
New Revision: 1516983

URL: http://svn.apache.org/r1516983
Log:
More unit tests for column conversion, and avoid the use of Math.pow based on 
the suggestion from github-6

Modified:
    poi/trunk/src/java/org/apache/poi/ss/util/CellReference.java
    poi/trunk/src/testcases/org/apache/poi/hssf/util/TestCellReference.java

Modified: poi/trunk/src/java/org/apache/poi/ss/util/CellReference.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/util/CellReference.java?rev=1516983&r1=1516982&r2=1516983&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/util/CellReference.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/util/CellReference.java Fri Aug 23 
19:00:01 2013
@@ -170,22 +170,19 @@ public class CellReference {
         * @return zero based column index
         */
        public static int convertColStringToIndex(String ref) {
-
-               int pos = 0;
                int retval=0;
-               for (int k = ref.length()-1; k >= 0; k--) {
-                       char thechar = ref.charAt(k);
+               char[] refs = ref.toUpperCase().toCharArray();
+               for (int k=0; k<refs.length; k++) {
+                       char thechar = refs[k];
                        if (thechar == ABSOLUTE_REFERENCE_MARKER) {
                                if (k != 0) {
                                        throw new IllegalArgumentException("Bad 
col ref format '" + ref + "'");
                                }
-                               break;
+                               continue;
                        }
-                       // Character.getNumericValue() returns the values
-                       //  10-35 for the letter A-Z
-                       int shift = (int)Math.pow(26, pos);
-                       retval += (Character.getNumericValue(thechar)-9) * 
shift;
-                       pos++;
+                       
+                       // Character is uppercase letter, find relative value 
to A
+                       retval = (retval * 26) + (thechar - 'A' + 1);
                }
                return retval-1;
        }

Modified: 
poi/trunk/src/testcases/org/apache/poi/hssf/util/TestCellReference.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/util/TestCellReference.java?rev=1516983&r1=1516982&r2=1516983&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/util/TestCellReference.java 
(original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/util/TestCellReference.java Fri 
Aug 23 19:00:01 2013
@@ -44,6 +44,17 @@ public final class TestCellReference ext
         assertEquals("ZZ", CellReference.convertNumToColString(701));
         assertEquals("AAA", CellReference.convertNumToColString(702));
         assertEquals("ZZZ", CellReference.convertNumToColString(18277));
+        
+        // Absolute references are allowed for the string ones
+        assertEquals(0, CellReference.convertColStringToIndex("$A"));
+        assertEquals(25, CellReference.convertColStringToIndex("$Z"));
+        assertEquals(26, CellReference.convertColStringToIndex("$AA"));
+        
+        // $ sign isn't allowed elsewhere though
+        try {
+            CellReference.convertColStringToIndex("A$B$");
+            fail("Column reference is invalid and shouldn't be accepted");
+        } catch (IllegalArgumentException e) {}
     }
 
     public void testAbsRef1(){



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org
For additional commands, e-mail: commits-h...@poi.apache.org

Reply via email to