Skip to content

Instantly share code, notes, and snippets.

@hikoz
Created November 4, 2011 06:00
Show Gist options
  • Save hikoz/1338755 to your computer and use it in GitHub Desktop.
Save hikoz/1338755 to your computer and use it in GitHub Desktop.
Excel列名変換をClojureで
(defn pow [^long m ^long n]
(apply * (repeat n m)))
(defn ^long col2row [^String col]
(apply + (map-indexed (fn [i c] * (- (int c) 64) (pow 26 i))
(reverse col))))
(defn ^String row2col [^long row]
(let [n (mod (- row 1) 26)
c (char (+ n 1 64))
r (quot (- row n) 26)]
(if (zero? r)
c
(str (row2col r) c))))
(defn ^String row2col+ [^long row]
(->> (iterate (fn [[c r]] (let [n (mod (- r 1) 26)]
[(inc n) (quot (- r n) 26)]))
[0 row])
(partition 2 1)
(take-while #(not= 0 (second (first %))))
(reverse)
(map #(char (+ (first (second %)) 64)))
(apply str)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment