Algorithm to get the excel-like column name of a number

后端 未结 10 1852
囚心锁ツ
囚心锁ツ 2020-11-30 18:08

I\'m working on a script that generate some Excel documents and I need to convert a number into its column name equivalent. For example:

1 => A
2 => B
         


        
10条回答
  •  旧巷少年郎
    2020-11-30 18:43

    To anyone looking for a Javascript implementation of this, here is @ircmaxell's answer in Javascript..

    function getNameFromNumber(num){
        let numeric = num%26;
        let letter = String.fromCharCode(65+numeric);
        let num2 = parseInt(num/26);
        if(num2 > 0) {
          return getNameFromNumber(num2 - 1)+letter;
        } else {
          return letter;
        }
    }
    
    

提交回复
热议问题