Skip to content

Instantly share code, notes, and snippets.

@bdkosher
Last active August 29, 2015 14:04
Show Gist options
  • Save bdkosher/afe1a65e6678854cf2ec to your computer and use it in GitHub Desktop.
Save bdkosher/afe1a65e6678854cf2ec to your computer and use it in GitHub Desktop.
Converts an Excel column name, e.g. 'A', 'B',...'Z', 'AA', 'AB', etc. to a integer index (0-based)
int colToIndex(String col) {
col.length() == 1 ? col.chars[0] - 65 : (col.chars[0] - 64) * Math.pow(26, col.length() - 1) + colToIndex(col.substring(1))
}
[A:0, B:1, Y:24, Z:25, AA:26, AB:27, AY:50, AZ:51, BA:52, BB:53,
BY:76, BZ:77, CA:78, CB:79, CY:102, CZ:103, DA:104, DB:105,
ZY:700, ZZ:701, AAA:702, AAB: 703, AAZ: 727, ABA: 728, AZZ: 1377,
BAA:1378 ].each { k, v ->
assert colToIndex(k) == v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment