Convert numbers to letters beyond the 26 character alphabet

后端 未结 3 785
故里飘歌
故里飘歌 2020-11-27 19:28

I\'m creating some client side functions for a mappable spreadsheet export feature.

I\'m using jQuery to manage the sort order of the columns, but each column is ord

3条回答
  •  迷失自我
    2020-11-27 20:16

    I think you're looking for something like this

        function colName(n) {
            var ordA = 'a'.charCodeAt(0);
            var ordZ = 'z'.charCodeAt(0);
            var len = ordZ - ordA + 1;
          
            var s = "";
            while(n >= 0) {
                s = String.fromCharCode(n % len + ordA) + s;
                n = Math.floor(n / len) - 1;
            }
            return s;
        }
    
    // Example:
    
        for(n = 0; n < 125; n++)
                document.write(n + ":" + colName(n) + "
    ");

提交回复
热议问题