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

Memoizing date format creation #80

Merged
merged 1 commit into from
Nov 26, 2018
Merged
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
38 changes: 20 additions & 18 deletions src/dk/ative/docjure/spreadsheet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@
(let [cls (class value)]
(or (isa? cls Date) (isa? cls Calendar))))

(defn- ^:dynamic create-date-format [^Workbook workbook ^String format]
(let [date-style (.createCellStyle workbook)
format-helper (.getCreationHelper workbook)]
(doto date-style
(.setDataFormat (.. format-helper createDataFormat (getFormat format))))))

(defn apply-date-format! [^Cell cell ^String format]
(let [workbook (.. cell getSheet getWorkbook)
date-style (.createCellStyle workbook)
Expand All @@ -231,31 +237,26 @@
(defmulti set-cell! (fn [^Cell cell val] (type val)))

(defmethod set-cell! String [^Cell cell val]
(do
(if (= (.getCellType cell) Cell/CELL_TYPE_FORMULA) (.setCellType cell Cell/CELL_TYPE_STRING))
(.setCellValue cell ^String val)))
(if (= (.getCellType cell) Cell/CELL_TYPE_FORMULA) (.setCellType cell Cell/CELL_TYPE_STRING))
(.setCellValue cell ^String val))

(defmethod set-cell! Number [^Cell cell val]
(do
(if (= (.getCellType cell) Cell/CELL_TYPE_FORMULA) (.setCellType cell Cell/CELL_TYPE_NUMERIC))
(.setCellValue cell (double val))))
(if (= (.getCellType cell) Cell/CELL_TYPE_FORMULA) (.setCellType cell Cell/CELL_TYPE_NUMERIC))
(.setCellValue cell (double val)))

(defmethod set-cell! Boolean [^Cell cell val]
(do
(if (= (.getCellType cell) Cell/CELL_TYPE_FORMULA) (.setCellType cell Cell/CELL_TYPE_BOOLEAN))
(.setCellValue cell ^Boolean val)))
(if (= (.getCellType cell) Cell/CELL_TYPE_FORMULA) (.setCellType cell Cell/CELL_TYPE_BOOLEAN))
(.setCellValue cell ^Boolean val))

(defmethod set-cell! Date [^Cell cell val]
(do
(if (= (.getCellType cell) Cell/CELL_TYPE_FORMULA) (.setCellType cell Cell/CELL_TYPE_NUMERIC))
(.setCellValue cell ^Date val)
(apply-date-format! cell "m/d/yy")))
(if (= (.getCellType cell) Cell/CELL_TYPE_FORMULA) (.setCellType cell Cell/CELL_TYPE_NUMERIC))
(.setCellValue cell ^Date val)
(.setCellStyle cell (create-date-format (.. cell getSheet getWorkbook) "m/d/yy")))

(defmethod set-cell! nil [^Cell cell val]
(let [^String null nil]
(do
(if (= (.getCellType cell) Cell/CELL_TYPE_FORMULA) (.setCellType cell Cell/CELL_TYPE_BLANK))
(.setCellValue cell null))))
(if (= (.getCellType cell) Cell/CELL_TYPE_FORMULA) (.setCellType cell Cell/CELL_TYPE_BLANK))
(.setCellValue cell null)))

(defn add-row! [^Sheet sheet values]
(assert-type sheet Sheet)
Expand All @@ -273,8 +274,9 @@
order on that row."
[^Sheet sheet rows]
(assert-type sheet Sheet)
(doseq [row rows]
(add-row! sheet row)))
(binding [create-date-format (memoize create-date-format)]
(doseq [row rows]
(add-row! sheet row))))

(defn add-sheet!
"Add a new sheet to the workbook."
Expand Down