Skip to content

Instantly share code, notes, and snippets.

@tabrindle
Last active October 19, 2016 14:16
Show Gist options
  • Save tabrindle/574bfddccdae39965b86305fb7ce2003 to your computer and use it in GitHub Desktop.
Save tabrindle/574bfddccdae39965b86305fb7ce2003 to your computer and use it in GitHub Desktop.
excel sheet index conversion
// minified
a=>a.toUpperCase().split('').reduce((b,c)=>b*26+c.charCodeAt(0)-"A".charCodeAt(0)-1,0)
// one-lined
column => column.toUpperCase().split('').reduce((acc,val) => acc * 26 + val.charCodeAt(0) - "A".charCodeAt(0) - 1,0);
// expanded
function convert_column_to_index(column_name) {
return column_name.split('').reduce(function(acc,val) {
return acc * 26 + val.charCodeAt(0) - ("A".charCodeAt(0) - 1)
}, 0);
}
// william
function nameToIndex(input) {
var indexNum = 0;
var inputArray = input.split("");
inputArray.reverse();
for (var i = 0; i < inputArray.length; i++) {
indexNum += (Math.pow(26, i) * (inputArray[0].toLowerCase().charCodeAt() - 96));
}
return indexNum;
}
// alt
function convert_column_to_index(column_name) {
return column_name.split('').reduce(function(acc,val) {
return acc * 26 + ("abcdefghijklmnopqrstuvwxyz".indexOf(val) + 1)
}, 0);
}
//tests
expect("a").toBe(1);
expect("z").toBe(26);
expect("aa").toBe(27)'
expect("bb").toBe(54)'
expect("abc").toBe(731);
expect("ccc").toBe(2109);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment