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

Allow ignoring of missing workbooks #57

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 9 additions & 6 deletions src/dk/ative/docjure/spreadsheet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
CellValue Drawing CreationHelper)
(org.apache.poi.ss.util CellReference AreaReference)))

(def ^:dynamic *ignore-missing-workbooks* false)

(defmacro assert-type [value expected-type]
`(when-not (isa? (class ~value) ~expected-type)
(throw (IllegalArgumentException.
Expand All @@ -37,12 +39,13 @@
(defmethod read-cell Cell/CELL_TYPE_STRING [^Cell cell] (.getStringCellValue cell))
(defmethod read-cell Cell/CELL_TYPE_FORMULA [^Cell cell]
(let [evaluator (.. cell getSheet getWorkbook
getCreationHelper createFormulaEvaluator)
cv (.evaluate evaluator cell)]
(if (and (= Cell/CELL_TYPE_NUMERIC (.getCellType cv))
(DateUtil/isCellDateFormatted cell))
(.getDateCellValue cell)
(read-cell-value cv false))))
getCreationHelper createFormulaEvaluator)]
(.setIgnoreMissingWorkbooks evaluator *ignore-missing-workbooks*)
(let [cv (.evaluate evaluator cell)]
(if (and (= Cell/CELL_TYPE_NUMERIC (.getCellType cv))
(DateUtil/isCellDateFormatted cell))
(.getDateCellValue cell)
(read-cell-value cv false)))))
(defmethod read-cell Cell/CELL_TYPE_BOOLEAN [^Cell cell] (.getBooleanCellValue cell))
(defmethod read-cell Cell/CELL_TYPE_NUMERIC [^Cell cell]
(if (DateUtil/isCellDateFormatted cell)
Expand Down
14 changes: 13 additions & 1 deletion test/dk/ative/docjure/spreadsheet_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
:1900-based-file "test/dk/ative/docjure/testdata/1900-based-dates.xlsx"
:1904-based-file "test/dk/ative/docjure/testdata/1904-based-dates.xlsx"
:simple "test/dk/ative/docjure/testdata/simple.xlsx"
:save-workbook-location "test/dk/ative/docjure/testdata/saved.xlsx"})
:save-workbook-location "test/dk/ative/docjure/testdata/saved.xlsx"
:missing-workbook "test/dk/ative/docjure/testdata/missing-workbook.xlsx"})

(def datatypes-map {:A :text, :B :integer, :C :decimal, :D :date, :E :time, :F :date-time, :G :percentage, :H :fraction, :I :scientific, :J :date-formulae})
(def formulae-map {:A :formula, :B :expected})
Expand Down Expand Up @@ -935,3 +936,14 @@
(is (= 1.0 (read-cell (select-cell "A2" worksheet))))
(is (= 4.0 (read-cell (select-cell "B2" worksheet))))
(is (= 5.0 (read-cell (select-cell "B3" worksheet))))))))

(deftest can-ignore-missing-workbooks-test
(let [file (config :missing-workbook)
loaded (load-workbook file)
worksheet (first (sheet-seq loaded))]
(testing "throws an exception by default"
(is (thrown? java.lang.RuntimeException
(read-cell (select-cell "A1" worksheet)))))
(testing "retrieves cached value when ignoring missing workbooks"
(binding [*ignore-missing-workbooks* true]
(is (= 6.0 (read-cell (select-cell "A1" worksheet))))))))
Binary file not shown.