More unit tests for column conversion, and avoid the use of Math.pow based on the suggestion from github-6
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1516983 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b79d4fda52
commit
673864cdc5
@ -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;
|
||||
}
|
||||
|
@ -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(){
|
||||
|
Loading…
Reference in New Issue
Block a user