Skip to content

Instantly share code, notes, and snippets.

@kawanet
Created February 23, 2021 13:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kawanet/69237038ea3c414284a5418d732b5660 to your computer and use it in GitHub Desktop.
Save kawanet/69237038ea3c414284a5418d732b5660 to your computer and use it in GitHub Desktop.
Excel の AA 形式のカラム名と配列インデックスを相互変換する JavaScript
/**
* Excel Column: A,B,C,...,Z,AA,AB,...,XFC,XFD
* Column Index: 0,1,2,...,25,26,27,...,16382,16383
*/
const col2idx = c => c.split("").reduce((prev, c) => (prev * 26 + parseInt(c, 36) - 9), 0) - 1;
const idx2col = n => ((n > 25 ? idx2col(Math.floor(n / 26 - 1)) : "") + (n % 26 + 10).toString(36).toUpperCase());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment