Translate a column index into an Excel Column Name

前端 未结 15 2354
花落未央
花落未央 2020-11-27 20:41

Given a columns\' index, how can you get an Excel column name?

The problem is trickier than it sounds because it\'s not just base-26. The columns

15条回答
  •  一个人的身影
    2020-11-27 20:53

    JavaScript Solution

    /**
     * Calculate the column letter abbreviation from a 0 based index
     * @param {Number} value
     * @returns {string}
     */
    getColumnFromIndex = function (value) {
        var base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
        value++;
        var remainder, result = "";
        do {
            remainder = value % 26;
            result = base[(remainder || 26) - 1] + result;
             value = Math.floor(value / 26);
        } while (value > 0);
        return result;
    };
    

提交回复
热议问题