Skip to content

Commit e8fc305

Browse files
authored
Merge branch 'main' into PassSheetIdx
2 parents df50c8f + f4ff2c2 commit e8fc305

File tree

2 files changed

+52
-33
lines changed

2 files changed

+52
-33
lines changed

src/bb_excel/core.clj

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
(ns bb-excel.core
22
(:require [clojure.data.xml :refer [parse-str]]
3-
[clojure.java.io :refer [file]]
3+
[clojure.java.io :as io]
44
[clojure.set :refer [rename-keys]])
5-
(:import [java.time LocalDate Month]
5+
(:import [java.io File]
66
[java.text SimpleDateFormat]
7+
[java.time LocalDate Month]
78
[java.time.format DateTimeFormatter]
89
[java.util TimeZone]
9-
[java.util.zip ZipFile])
10+
(java.util.zip ZipFile))
1011
(:gen-class))
1112

1213
(set! *warn-on-reflection* true)
1314

14-
(defonce sdf (SimpleDateFormat. "HH:mm:ss"))
15+
(defonce ^SimpleDateFormat sdf (SimpleDateFormat. "HH:mm:ss"))
1516
(.setTimeZone sdf (TimeZone/getTimeZone "UTC"))
1617

1718
(def error-codes
@@ -66,15 +67,23 @@
6667
:xmlns.http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Frelationships/id
6768
:xmlns.http%3A%2F%2Fpurl.oclc.org%2Fooxml%2FofficeDocument%2Frelationships/id}})
6869

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+
6980
(defn get-sheet-names
7081
"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)
7887
x (parse-str (slurp ins))
7988
y (filter #((:sheet-tag tags) (:tag %)) (xml-seq x))]
8089
(->> y
@@ -103,10 +112,10 @@
103112
[n]
104113
(when n (format "%.4f%%" (* 100 (parse-double (str n))))))
105114

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"
110119
"180" "181" "184" "185" "186" "187"})
111120

112121
(def times #{"164" "18" "19" "21" "20" "45" "46" "47"})
@@ -159,10 +168,9 @@
159168

160169
(defn get-unique-strings
161170
"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)
166174
x (parse-str (slurp ins))]
167175
(->>
168176
(filter #((:text-part tags) (:tag %)) (xml-seq x))
@@ -171,10 +179,9 @@
171179

172180
(defn get-styles
173181
"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)
178185
x (parse-str (slurp ins))]
179186
(->> x
180187
xml-seq
@@ -228,16 +235,17 @@
228235
(map #(rename-keys % h) dx))]
229236
(if (empty? cols) dy (map #(select-keys % cols) dy)))))
230237

238+
231239
(defn get-sheets
232240
"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)
237245
sxs (if (:sheet options) (filter #(= (:sheet options) (:name %)) sns) sns)
238246
res (if (empty? sxs) [{:sheet []}]
239247
(map #(assoc % :sheet
240-
(try (get-sheet filename (:name %) options)
248+
(try (get-sheet file-or-filename (:name %) options)
241249
(catch Exception ex [(bean ex)]))) sxs))]
242250
res)))
243251

test/core_test.clj

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
(ns core-test
2-
(:require [clojure.test :refer [deftest is testing run-tests]]
2+
(:require [clojure.java.io :as io]
3+
[clojure.test :refer [deftest is testing run-tests]]
34
[bb-excel.core :refer [get-sheets get-sheet-names get-sheet
4-
get-range
5+
get-range
56
; get-row get-col get-cells crange
6-
]]))
7+
]])
8+
(:import [java.util.zip ZipFile]))
9+
10+
(deftest zipfile-or-nil-test
11+
(let [zipfile-or-nil #'bb-excel.core/zipfile-or-nil]
12+
(let [file (io/file "test/data/simple.xlsx")]
13+
(is (instance? ZipFile (zipfile-or-nil file))))
14+
(let [filepath "test/data/simple.xlsx"]
15+
(is (instance? ZipFile (zipfile-or-nil filepath))))
16+
(is (nil? (zipfile-or-nil "invalid-file-path")))
17+
(is (nil? (zipfile-or-nil :invalid-type)))))
718

819
(deftest get-sheet-names-test
920
(testing "Get Sheet Names"
@@ -36,11 +47,11 @@
3647
(is (nil? (get-sheet-names nil))
3748
"Filename was not passed in")
3849
(is (= '({:_r 10 :A "9" :B "TextData"})
39-
(get-range (get-sheet "test/data/Types.xlsx" "Sheet1") "A10:B10")))))
50+
(get-range (get-sheet "test/data/Types.xlsx" "Sheet1") "A10:B10")))))
4051

4152
(comment
4253
(run-tests)
43-
54+
4455
(->>
4556
(get-sheets "test/data/Types.xlsx")
4657
second

0 commit comments

Comments
 (0)