|
| 1 | +(ns excel-clj.cell |
| 2 | + "A lightweight wrapper over cell values that allows combining both simple |
| 3 | + and wrapped cells with new stile and dimensions." |
| 4 | + {:author "Matthew Downey"} |
| 5 | + (:require [taoensso.encore :as enc])) |
| 6 | + |
| 7 | + |
| 8 | +(defn wrapped? [x] (:excel/wrapped? x)) |
| 9 | + |
| 10 | + |
| 11 | +(defn wrapped |
| 12 | + "If `x` contains cell data wrapped in a map (with style & dimension data), |
| 13 | + return it as-is. Otherwise return a wrapped version." |
| 14 | + [x] |
| 15 | + (if (wrapped? x) |
| 16 | + x |
| 17 | + {:excel/wrapped? true :excel/data x})) |
| 18 | + |
| 19 | + |
| 20 | +(defn style |
| 21 | + "Get the style specification for `x`, or deep-merge its current style spec |
| 22 | + with the given `style-map`." |
| 23 | + ([x] |
| 24 | + (or (:excel/style x) {})) |
| 25 | + ([x style-map] |
| 26 | + (let [style-map (enc/nested-merge (style x) style-map)] |
| 27 | + (assoc (wrapped x) :excel/style style-map)))) |
| 28 | + |
| 29 | + |
| 30 | +(defn dims |
| 31 | + "Get the {:width N, :height N} dimension map for `x`, or merge in the given |
| 32 | + `dims-map` of the same format." |
| 33 | + ([x] |
| 34 | + (or (:excel/dims x) {:width 1 :height 1})) |
| 35 | + ([x dims-map] |
| 36 | + (let [dims-map (merge (dims x) dims-map)] |
| 37 | + (assoc (wrapped x) :excel/dims dims-map)))) |
| 38 | + |
| 39 | + |
| 40 | +(defn data |
| 41 | + "If `x` contains cell data wrapped in a map (with style & dimension data), |
| 42 | + return the wrapped cell value. Otherwise return as-is." |
| 43 | + [x] |
| 44 | + (if (wrapped? x) |
| 45 | + (:excel/data x) |
| 46 | + x)) |
| 47 | + |
| 48 | + |
| 49 | +(comment |
| 50 | + "You don't have to worry about if something is already wrapped or already |
| 51 | + styled:" |
| 52 | + |
| 53 | + (def cell |
| 54 | + (let [header-style {:border-bottom :thin :font {:bold true}}] |
| 55 | + (-> "Header" |
| 56 | + (style header-style) |
| 57 | + (dims {:height 2}) |
| 58 | + (style {:vertical-alignment :center})))) |
| 59 | + |
| 60 | + (clojure.pprint/pprint cell) |
| 61 | + ; #:excel{:wrapped? true, |
| 62 | + ; :data "Header", |
| 63 | + ; :style |
| 64 | + ; {:border-bottom :thin, |
| 65 | + ; :font {:bold true}, |
| 66 | + ; :vertical-alignment :center}, |
| 67 | + ; :dims {:width 1, :height 2}} |
| 68 | + ) |
0 commit comments