Created
January 6, 2012 08:42
-
-
Save ghoseb/1569742 to your computer and use it in GitHub Desktop.
Convert numbers to Excel headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(let [chr (zipmap (range) "ABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
encode (fn [n acc] | |
(if (< n 26) | |
(cons n acc) | |
(recur (dec (quot n 26)) (cons (rem n 26) acc))))] | |
(defn col-str | |
"Convert numbers to Excel headers." | |
[n] | |
(apply str (map chr (encode n '()))))) | |
;;(col-str 0) | |
;;=> "A" | |
;;(col-str 25) | |
;;=> "Z" | |
;;(col-str 26) | |
;;=> "AA" | |
;;(col-str 701) | |
;;=> "ZZ" | |
;;(col-str 702) | |
;;=> "AAA" |
kumarshantanu
commented
Jan 6, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment