|
1 | 1 | (ns bb-excel.core
|
2 | 2 | (:require [clojure.data.xml :refer [parse-str]]
|
3 |
| - [clojure.java.io :refer [file]] |
| 3 | + [clojure.java.io :as io] |
4 | 4 | [clojure.set :refer [rename-keys]])
|
5 |
| - (:import [java.time LocalDate Month] |
| 5 | + (:import [java.io File] |
6 | 6 | [java.text SimpleDateFormat]
|
| 7 | + [java.time LocalDate Month] |
7 | 8 | [java.time.format DateTimeFormatter]
|
8 | 9 | [java.util TimeZone]
|
9 |
| - [java.util.zip ZipFile]) |
| 10 | + (java.util.zip ZipFile)) |
10 | 11 | (:gen-class))
|
11 | 12 |
|
12 | 13 | (set! *warn-on-reflection* true)
|
13 | 14 |
|
14 |
| -(defonce sdf (SimpleDateFormat. "HH:mm:ss")) |
| 15 | +(defonce ^SimpleDateFormat sdf (SimpleDateFormat. "HH:mm:ss")) |
15 | 16 | (.setTimeZone sdf (TimeZone/getTimeZone "UTC"))
|
16 | 17 |
|
17 | 18 | (def error-codes
|
|
66 | 67 | :xmlns.http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Frelationships/id
|
67 | 68 | :xmlns.http%3A%2F%2Fpurl.oclc.org%2Fooxml%2FofficeDocument%2Frelationships/id}})
|
68 | 69 |
|
| 70 | +(defn- zipfile-or-nil |
| 71 | + "Retrieve ZipFile object if provided `file-or-filename` point to existing file or nil" |
| 72 | + [file-or-filename] |
| 73 | + (when-let [^File file (condp instance? file-or-filename |
| 74 | + String (io/file file-or-filename) |
| 75 | + File file-or-filename |
| 76 | + nil)] |
| 77 | + (when (.exists file) |
| 78 | + (ZipFile. file)))) |
| 79 | + |
69 | 80 | (defn get-sheet-names
|
70 | 81 | "Retrieves a list of Sheet Names from a given Excel Spreadsheet
|
71 |
| - Returns nil if the file does not exist or a non-string is passed as the filename" |
72 |
| - [filename] |
73 |
| - (when (and (not-any? (fn [f] (f filename)) [nil? coll?]) |
74 |
| - (.exists (file filename))) |
75 |
| - (let [^ZipFile zf (ZipFile. ^String filename) |
76 |
| - wb (.getEntry zf "xl/workbook.xml") |
77 |
| - ins (.getInputStream zf wb) |
| 82 | + Returns nil if the file does not exist or a non-string is passed as the `file-or-filename`" |
| 83 | + [file-or-filename] |
| 84 | + (when-let [^ZipFile zipfile (zipfile-or-nil file-or-filename)] |
| 85 | + (let [wb (.getEntry zipfile "xl/workbook.xml") |
| 86 | + ins (.getInputStream zipfile wb) |
78 | 87 | x (parse-str (slurp ins))
|
79 | 88 | y (filter #((:sheet-tag tags) (:tag %)) (xml-seq x))]
|
80 | 89 | (->> y
|
|
103 | 112 | [n]
|
104 | 113 | (when n (format "%.4f%%" (* 100 (parse-double (str n))))))
|
105 | 114 |
|
106 |
| -(def dates #{"14" "15" "16" "17" "30" "34" "51" |
107 |
| - "52" "53" "55" "56" "58" "165" |
108 |
| - "166" "167" "168" "169" "170" "171" "172" |
109 |
| - "173" "174" "175" "176" "177" "178" "179" |
| 115 | +(def dates #{"14" "15" "16" "17" "30" "34" "51" |
| 116 | + "52" "53" "55" "56" "58" "165" |
| 117 | + "166" "167" "168" "169" "170" "171" "172" |
| 118 | + "173" "174" "175" "176" "177" "178" "179" |
110 | 119 | "180" "181" "184" "185" "186" "187"})
|
111 | 120 |
|
112 | 121 | (def times #{"164" "18" "19" "21" "20" "45" "46" "47"})
|
|
159 | 168 |
|
160 | 169 | (defn get-unique-strings
|
161 | 170 | "Get dictionary of all unique strings in the Excel spreadsheet"
|
162 |
| - [filename] |
163 |
| - (let [zf (ZipFile. ^String filename) |
164 |
| - wb (.getEntry zf (str "xl/sharedStrings.xml")) |
165 |
| - ins (.getInputStream zf wb) |
| 171 | + [^ZipFile zipfile] |
| 172 | + (let [wb (.getEntry zipfile (str "xl/sharedStrings.xml")) |
| 173 | + ins (.getInputStream zipfile wb) |
166 | 174 | x (parse-str (slurp ins))]
|
167 | 175 | (->>
|
168 | 176 | (filter #((:text-part tags) (:tag %)) (xml-seq x))
|
|
171 | 179 |
|
172 | 180 | (defn get-styles
|
173 | 181 | "Get styles"
|
174 |
| - [filename] |
175 |
| - (let [zf (ZipFile. ^String filename) |
176 |
| - wb (.getEntry zf (str "xl/styles.xml")) |
177 |
| - ins (.getInputStream zf wb) |
| 182 | + [^ZipFile zipfile] |
| 183 | + (let [wb (.getEntry zipfile (str "xl/styles.xml")) |
| 184 | + ins (.getInputStream zipfile wb) |
178 | 185 | x (parse-str (slurp ins))]
|
179 | 186 | (->> x
|
180 | 187 | xml-seq
|
|
228 | 235 | (map #(rename-keys % h) dx))]
|
229 | 236 | (if (empty? cols) dy (map #(select-keys % cols) dy)))))
|
230 | 237 |
|
| 238 | + |
231 | 239 | (defn get-sheets
|
232 | 240 | "Get all or specified sheet from the excel spreadsheet"
|
233 |
| - ([filename] |
234 |
| - (get-sheets filename {})) |
235 |
| - ([filename options] |
236 |
| - (let [sns (get-sheet-names filename) |
| 241 | + ([file-or-filename] |
| 242 | + (get-sheets file-or-filename {})) |
| 243 | + ([file-or-filename options] |
| 244 | + (let [sns (get-sheet-names file-or-filename) |
237 | 245 | sxs (if (:sheet options) (filter #(= (:sheet options) (:name %)) sns) sns)
|
238 | 246 | res (if (empty? sxs) [{:sheet []}]
|
239 | 247 | (map #(assoc % :sheet
|
240 |
| - (try (get-sheet filename (:name %) options) |
| 248 | + (try (get-sheet file-or-filename (:name %) options) |
241 | 249 | (catch Exception ex [(bean ex)]))) sxs))]
|
242 | 250 | res)))
|
243 | 251 |
|
|
0 commit comments