Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parse xlsx without xl/sharedStrings.xml #8

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
kbosompem marked this conversation as resolved.
Show resolved Hide resolved
(= (: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"))]
kbosompem marked this conversation as resolved.
Show resolved Hide resolved
(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.