Skip to content

Commit

Permalink
Merge pull request #7 from velios/FixWrongBranchMerge
Browse files Browse the repository at this point in the history
Fix after unsuccessful branch merge
  • Loading branch information
kbosompem authored Sep 16, 2023
2 parents 897eb1f + 5b795b3 commit a81009c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 49 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Changelog

0.0.6 / 2023-09-16
------------------
- Changed
- Added possibility to pass file object
- Added possibility use sheet index

0.0.5 / 2023-08-04
------------------
- Changed
- Fixed parse-range

Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ In the first example below we pull data from a specific sheet within the workboo
#!/usr/bin/env bb
(require '[babashka.deps :as deps])
(deps/add-deps
'{:deps {com.github.kbosompem/bb-excel {:mvn/version "0.0.5"}}})
'{:deps {com.github.kbosompem/bb-excel {:mvn/version "0.0.6"}}})

(ns demo
(:require [clojure.pprint :refer [print-table pprint]]
(:require [clojure.java.io :as io]
[clojure.pprint :refer [print-table pprint]]
[bb-excel.core :refer [get-sheet get-sheets]]))

;; To specify file can use either filename or file object
;; To specify sheet can use either sheetname or sheet index(started form 1)
(get-sheet "test/data/simple.xlsx" "Shows" )
(get-sheet (io/file "test/data/simple.xlsx") 1)

;=>
({:_r 2, :A 1.0, :B "Sesame Street"}
Expand Down
4 changes: 2 additions & 2 deletions bb-excel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(require '[babashka.deps :as deps])

(deps/add-deps '{:deps {com.github.kbosompem/bb-excel {:mvn/version "0.0.5"}}})
(deps/add-deps '{:deps {com.github.kbosompem/bb-excel {:mvn/version "0.0.6"}}})

(ns bb-excel
(:require [bb-excel.core :refer [get-sheets get-range get-sheet]]
Expand Down Expand Up @@ -115,7 +115,7 @@
(defn help
"Command line options"
[summary]
(->> ["bb-excel 0.0.5"
(->> ["bb-excel 0.0.6"
""
"Usage: bb-excel input-file options"
""
Expand Down
4 changes: 2 additions & 2 deletions bbexcel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(require '[babashka.deps :as deps])

(deps/add-deps '{:deps {com.github.kbosompem/bb-excel {:mvn/version "0.0.5"}}})
(deps/add-deps '{:deps {com.github.kbosompem/bb-excel {:mvn/version "0.0.6"}}})

(ns bbexcel
(:require [bb-excel.core :refer [get-sheets get-range get-sheet]]
Expand Down Expand Up @@ -115,7 +115,7 @@
(defn help
"Command line options"
[summary]
(->> ["bbexcel 0.0.5"
(->> ["bbexcel 0.0.6"
""
"Usage: bbexcel input-file options"
""
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.5"
(defproject com.github.kbosompem/bb-excel "0.0.6"
: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
86 changes: 44 additions & 42 deletions src/bb_excel/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -192,48 +192,50 @@
(mapv (comp :numFmtId :attrs)))))

(defn get-sheet
"Get sheet from file"
([filename]
(get-sheet filename 1 {}))
([filename sheetname-or-idx]
(get-sheet filename sheetname-or-idx {}))
([filename sheetname-or-idx options]
(let [opts (merge defaults options)
row (:row opts)
hdr (:hdr opts)
row (if (and hdr (zero? row)) 1 row)
rows (:rows opts)
fxn (:fxn opts)
cols (map fxn (:columns opts))
sheetid (cond
(string? sheetname-or-idx)
(:idx (first (filter #(= sheetname-or-idx (:name %)) (get-sheet-names filename))))

(and (integer? sheetname-or-idx) (pos? sheetname-or-idx))
sheetname-or-idx

:else
(let [message (format "Attr 'sheetname-or-idx' can only be string or positive number, but passed '%s'" sheetname-or-idx)]
(throw (ex-info message {}))))
zf (ZipFile. ^String filename)
wb (.getEntry zf (str "xl/worksheets/sheet" sheetid ".xml"))
ins (.getInputStream zf wb)
dict (get-unique-strings filename)
styles (get-styles filename)
xx (slurp ins)
x (parse-str xx)
d (->> x :content
(filter #((:sheet-data tags) (:tag %)))
first :content
(map :content)
(take rows)
(map (partial process-row dict styles)))
dx (remove #(= row (:_r %)) d)
h (when hdr (merge (update-vals (first (filter #(= (:_r %) row) d)) fxn) {:_r :_r}))
dy (if (pos? rows)
(take rows (map #(rename-keys % h) dx))
(map #(rename-keys % h) dx))]
(if (empty? cols) dy (map #(select-keys % cols) dy)))))
"Get sheet from file or filename"
([file-or-filename]
(get-sheet file-or-filename 1 {}))
([file-or-filename sheetname-or-idx]
(get-sheet file-or-filename sheetname-or-idx {}))
([file-or-filename sheetname-or-idx options]
(if-let [^ZipFile zipfile (zipfile-or-nil file-or-filename)]
(let [opts (merge defaults options)
row (:row opts)
hdr (:hdr opts)
row (if (and hdr (zero? row)) 1 row)
rows (:rows opts)
fxn (:fxn opts)
cols (map fxn (:columns opts))
sheetid (cond
(string? sheetname-or-idx)
(:idx (first (filter #(= sheetname-or-idx (:name %)) (get-sheet-names file-or-filename))))

(and (integer? sheetname-or-idx) (pos? sheetname-or-idx))
sheetname-or-idx

:else
(let [message (format "Attr 'sheetname-or-idx' can only be string or positive number, but passed '%s'" sheetname-or-idx)]
(throw (ex-info message {}))))
wb (.getEntry zipfile (str "xl/worksheets/sheet" sheetid ".xml"))
ins (.getInputStream zipfile wb)
dict (get-unique-strings zipfile)
styles (get-styles zipfile)
xx (slurp ins)
x (parse-str xx)
d (->> x :content
(filter #((:sheet-data tags) (:tag %)))
first :content
(map :content)
(take rows)
(map (partial process-row dict styles)))
dx (remove #(= row (:_r %)) d)
h (when hdr (merge (update-vals (first (filter #(= (:_r %) row) d)) fxn) {:_r :_r}))
dy (if (pos? rows)
(take rows (map #(rename-keys % h) dx))
(map #(rename-keys % h) dx))]
(if (empty? cols) dy (map #(select-keys % cols) dy)))
(let [message (format "Attr 'file-or-filename' contains value not suitable for creating ZipFile: '%s'" file-or-filename)]
(throw (ex-info message {}))))))


(defn get-sheets
Expand Down

0 comments on commit a81009c

Please sign in to comment.