Convert excel column alphabet (e.g. AA) to number (e.g., 25)

后端 未结 7 1810
余生分开走
余生分开走 2020-12-13 10:55

In my grid the column headers are named A,B,C...,AA,AB,AC,...etc like an excel spreadsheet. How can I convert the string to number like: A => 1, B =>

7条回答
  •  长情又很酷
    2020-12-13 11:19

    Here's a quick example of the code you should implement. This will work with any given number of letters.

    function letterToNumbers(string) {
        string = string.toUpperCase();
        var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', sum = 0, i;
        for (i = 0; i < string.length; i++) {
            sum += Math.pow(letters.length, i) * (letters.indexOf(string.substr(((i + 1) * -1), 1)) + 1);
        }
        return sum;
    }
    

提交回复
热议问题