diff --git a/src/dk/ative/docjure/spreadsheet.clj b/src/dk/ative/docjure/spreadsheet.clj index 6235ebb..fb69d62 100644 --- a/src/dk/ative/docjure/spreadsheet.clj +++ b/src/dk/ative/docjure/spreadsheet.clj @@ -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. @@ -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) diff --git a/test/dk/ative/docjure/spreadsheet_test.clj b/test/dk/ative/docjure/spreadsheet_test.clj index bfd5873..793268a 100644 --- a/test/dk/ative/docjure/spreadsheet_test.clj +++ b/test/dk/ative/docjure/spreadsheet_test.clj @@ -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}) @@ -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)))))))) diff --git a/test/dk/ative/docjure/testdata/missing-workbook.xlsx b/test/dk/ative/docjure/testdata/missing-workbook.xlsx new file mode 100644 index 0000000..8204cdf Binary files /dev/null and b/test/dk/ative/docjure/testdata/missing-workbook.xlsx differ