2014-04-25 10 views
6

Próbuję napisać funkcję, która emituje ciąg ciągów pasujących do nagłówków w programie Excel. Jeśli nie są zaznajomieni z Excela, że ​​kolejność wygląda następująco:Excel-podobne nagłówki w JavaScript

A,B,...,Z,AA,...,AZ,BA,...,ZZ,AAA,...,etc. 

Jest to kod mam wymyślić:

function next(id) { 
    if(id === "") 
     return "A"; 
    var prefix = id.substring(0, id.length-1); 
    var last = id[id.length-1] 
    if(last === "Z") 
     return (next(prefix) + "A"); 
    return prefix + String.fromCharCode(id.charCodeAt(id.length-1) + 1); 
} 

Czy znasz lepszy/zmywacz sposób prowadzenia to?

+0

to dość dużo jak bym to zrobić. Rekurencja wydaje się mieć sens. –

+1

To jest pytanie do opublikowania w [Recenzja kodu SE] (http://codereview.stackexchange.com/) zamiast SO. – Teemu

+0

jest kilka funkcji do wykonania w innych językach: http://stackoverflow.com/questions/837155/fastest-function-to-generate-excel-column-letters-in-c-sharp – pennstatephil

Odpowiedz

3

pisałem trochę coś to problem dla ciebie, myślę, że to jasne, a łatwy w użyciu, z testami

więc wystarczy zadzwonić „toExcelHeaderString (4)” dla A, B, C, D

lub dla poszczególnych wierszy "toExcelHeader excel (4)" dla D

/** 
* @param {Number} rows 
* @returns {String} 
*/ 
toExcelHeaderString = function (rows) { 
    return toExcelHeaderArray(rows).join(","); 
} 

// toExcelHeaderString(60) == "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM,AN,AO,AP,AQ,AR,AS,AT,AU,AV,AW,AX,AY,AZ,BA,BB,BC,BD,BE,BF,BG,BH" 

/** 
* @param {Number} rows 
* @returns {Array} 
*/ 
toExcelHeaderArray = function (rows) { 
    var excelHeaderArr = []; 
    for(var index = 1; index <= rows; index++) { 
     excelHeaderArr.push(toExcelHeader(index)); 
    } 
    return excelHeaderArr; 
} 

toExcelHeaderArray(60) == ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ", "BA", "BB", "BC", "BD", "BE", "BF", "BG", "BH"] 

/** 
* @param {Number} index 
* @returns {String} 
*/ 
toExcelHeader = function (index) { 
    if(index <= 0) { 
     throw new Error("index must be 1 or greater"); 
    } 
    index--; 
    var charCodeOfA = ("a").charCodeAt(0); // you could hard code to 97 
    var charCodeOfZ = ("z").charCodeAt(0); // you could hard code to 122 
    var excelStr = ""; 
    var base24Str = (index).toString(charCodeOfZ - charCodeOfA + 1); 
    for(var base24StrIndex = 0; base24StrIndex < base24Str.length; base24StrIndex++) { 
     var base24Char = base24Str[base24StrIndex]; 
     var alphabetIndex = (base24Char * 1 == base24Char) ? base24Char : (base24Char.charCodeAt(0) - charCodeOfA + 10); 
     // bizarre thing, A==1 in first digit, A==0 in other digits 
     if(base24StrIndex == 0) { 
      alphabetIndex -= 1; 
     } 
     excelStr += String.fromCharCode(charCodeOfA*1 + alphabetIndex*1); 
    } 
    return excelStr.toUpperCase(); 
} 
// toExcelHeader(0) == Error 
// toExcelHeader(1) == "A" 
// toExcelHeader(26) == "Z" 
// toExcelHeader(27) == "AA" 
// toExcelHeader(3400) == "EAT" 
// toExcelHeader(2048) == "CAT" 
// toExcelHeader(3733849) == "HELLO" 
// toExcelHeader(10768294) == "WORLD"