Skip to content

Instantly share code, notes, and snippets.

@tokland
Last active January 9, 2020 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokland/7665cdf9bb4da85237e9fa41fbdf9d88 to your computer and use it in GitHub Desktop.
Save tokland/7665cdf9bb4da85237e9fa41fbdf9d88 to your computer and use it in GitHub Desktop.
Get cell name from indexes for spreadsheet (Excel, ...)
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
function idOf(idx: number): string {
const quotient = Math.floor(idx / letters.length);
const remainder = idx % letters.length;
return (quotient > 0 ? idOf(quotient - 1) : "") + letters[remainder];
}
function cell(colIdx: number, rowIdx: number): string {
return idOf(colIdx) + (rowIdx + 1).toString();
}
console.log(cell(0, 0)); // A1
console.log(cell(52, 9)); // BA10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment