Last active
January 9, 2020 10:59
-
-
Save tokland/7665cdf9bb4da85237e9fa41fbdf9d88 to your computer and use it in GitHub Desktop.
Get cell name from indexes for spreadsheet (Excel, ...)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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