Skip to content

Instantly share code, notes, and snippets.

@dungsaga
Forked from danmana/Excel column name to number.js
Last active December 30, 2025 03:46
Show Gist options
  • Select an option

  • Save dungsaga/4ea4fd1764e7c41a8421b38fc004b9ef to your computer and use it in GitHub Desktop.

Select an option

Save dungsaga/4ea4fd1764e7c41a8421b38fc004b9ef to your computer and use it in GitHub Desktop.
Convert from excel column names to a index (1 based)
function colNum(name = '') {
const val = (c) => c.charCodeAt(0) - 'A'.charCodeAt(0) + 1
return name.split('')
.reduce((acc, v) => val(v) + acc * val('Z'), 0)
}
colNum('A') === 1
colNum('Z') === 26
colNum('AA') === 27
colNum('AZ') === 52
colNum('ZZ') === 702
colNum('AAA') === 703
colNum('AZA') === 1353
colNum('ZZZ') === 18278
@dungsaga
Copy link
Author

function col(name) {
	let a = 'A'.charCodeAt(0)
	let idx = 0
	for (let i = 0; i < name.length; i++) {
		idx *= 26
		idx += name.charCodeAt(i) - a + 1
	}
	return idx
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment