Translate a column index into an Excel Column Name

前端 未结 15 2287
花落未央
花落未央 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:46

    this is with Swift 4 :

    @IBAction func printlaction(_ sender: Any) {
        let textN : Int = Int (number_textfield.text!)!
        reslut.text = String (printEXCL_Letter(index: textN))
    }
    
    
    func printEXCL_Letter(index : Int) -> String {
    
        let letters = ["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"]
    
        var index = index;
        index -= 1
        let index_div = index / 26
    
        if (index_div > 0){
            return printEXCL_Letter(index: index_div) + letters[index % 26];
        }
        else {
            return letters[index % 26]
        }
    }
    

提交回复
热议问题