]> source.dussan.org Git - poi.git/commitdiff
More unit tests for column conversion, and avoid the use of Math.pow based on the...
authorNick Burch <nick@apache.org>
Fri, 23 Aug 2013 19:00:01 +0000 (19:00 +0000)
committerNick Burch <nick@apache.org>
Fri, 23 Aug 2013 19:00:01 +0000 (19:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1516983 13f79535-47bb-0310-9956-ffa450edef68

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

index c9bea9fd123f1afc0aa621fda4a71838f6005f87..3bd7ba908875e2ca881761607708614bb5f23321 100644 (file)
@@ -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;
        }
index 2e1a775a08826bab81f79571e99ef3dc289e991d..79fc876e93d7b43add270e93430e018ac53153c1 100644 (file)
@@ -44,6 +44,17 @@ public final class TestCellReference extends TestCase {
         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(){