Skip to content

Commit

Permalink
Merge pull request #8 from velios/shared-strings-fix
Browse files Browse the repository at this point in the history
Fix parse xlsx without xl/sharedStrings.xml
  • Loading branch information
kbosompem authored Oct 27, 2023
2 parents a81009c + eeeb00a commit 11342a8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ pom.xml.asc
.lsp/
settings.xml
settings2.xml
.DS_Store
.DS_Store
.idea
*.iml
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

0.0.7 / 2023-10-27
------------------
- Changed
- Fix parse xlsx without xl/sharedStrings.xml
- Fix parse cell with number and inlineStr data types

0.0.6 / 2023-09-16
------------------
- Changed
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject com.github.kbosompem/bb-excel "0.0.6"
(defproject com.github.kbosompem/bb-excel "0.0.7"
:description "A Simple Clojure/Babashka Library for Reading Data from Excel Files"
:url "https://github.com/kbosompem/bb-excel"
:license {:name "EPL-2.0"
Expand Down
18 changes: 11 additions & 7 deletions src/bb_excel/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,13 @@
(assoc :x row)
(assoc :y col))]
(cond
;; Possible data types well explained here https://stackoverflow.com/a/18346273
(= (:t u) "s") (dissoc (assoc-in u [:d] (dict (read-string (:d u)))) :t)
(= (:t u) "str") (dissoc u :t)
(= (:t u) "inlineStr") (dissoc u :t)
(= (:t u) "b") (dissoc (assoc-in u [:d] (if (= "1" (:d u)) true false)) :t)
(= (:t u) "e") (assoc-in u [:d] (error-codes (:d u)))
(= (:t u) "n") (assoc u :d (parse-long (:d u)))
(style-check u styles pcts) (assoc-in u [:d] (num2pct (:d u)))
(style-check u styles dates) (assoc-in u [:d] (num2date (:d u)))
(style-check u styles times) (assoc-in u [:d] (num2time (:d u)))
Expand Down Expand Up @@ -169,13 +172,14 @@
(defn get-unique-strings
"Get dictionary of all unique strings in the Excel spreadsheet"
[^ZipFile zipfile]
(let [wb (.getEntry zipfile (str "xl/sharedStrings.xml"))
ins (.getInputStream zipfile wb)
x (parse-str (slurp ins))]
(->>
(filter #((:text-part tags) (:tag %)) (xml-seq x))
(map get-cell-text)
(zipmap (range)))))
(if-let [wb (.getEntry zipfile (str "xl/sharedStrings.xml"))]
(let [ins (.getInputStream zipfile wb)
x (parse-str (slurp ins))]
(->>
(filter #((:text-part tags) (:tag %)) (xml-seq x))
(map get-cell-text)
(zipmap (range))))
{}))

(defn get-styles
"Get styles"
Expand Down
5 changes: 5 additions & 0 deletions test/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
(is (= '({:_r 10 :A "9" :B "TextData"})
(get-range (get-sheet "test/data/Types.xlsx" "Sheet1") "A10:B10")))))

(deftest corner-cases-test
(testing "Without shared files"
(is (= '({:_r 1, :A 1})
(get-sheet "test/data/without_sharedfiles.xlsx" 1)))))

(comment
(run-tests)

Expand Down
Binary file added test/data/without_sharedfiles.xlsx
Binary file not shown.

0 comments on commit 11342a8

Please sign in to comment.