diff --git a/project.clj b/project.clj index bdb648d..3da2d55 100644 --- a/project.clj +++ b/project.clj @@ -1,8 +1,7 @@ (defproject dk.ative/docjure "1.6.0-SNAPSHOT" :description "Easily read and write Office documents from Clojure." :url "http://github.com/ative/docjure" - :dependencies [[org.clojure/clojure "1.2.0"] - [org.clojure/clojure-contrib "1.2.0"] + :dependencies [[org.clojure/clojure "1.3.0"] [org.apache.poi/poi "3.6"] [org.apache.poi/poi-ooxml "3.6"]] :dev-dependencies [[swank-clojure "1.3.0-SNAPSHOT"] diff --git a/src/dk/ative/docjure/spreadsheet.clj b/src/dk/ative/docjure/spreadsheet.clj index 3338d06..e2dd8c1 100644 --- a/src/dk/ative/docjure/spreadsheet.clj +++ b/src/dk/ative/docjure/spreadsheet.clj @@ -29,7 +29,7 @@ (let [evaluator (.. cell getSheet getWorkbook getCreationHelper createFormulaEvaluator) cv (.evaluate evaluator cell)] - (read-cell-value cv (DateUtil/isCellDateFormatted cell)))) + (read-cell-value cv false))) (defmethod read-cell Cell/CELL_TYPE_BOOLEAN [cell] (.getBooleanCellValue cell)) (defmethod read-cell Cell/CELL_TYPE_NUMERIC [cell] (if (DateUtil/isCellDateFormatted cell) @@ -44,27 +44,27 @@ (defn save-workbook! "Save the workbook into a file." - [filename #^Workbook workbook] + [filename ^Workbook workbook] (assert-type workbook Workbook) (with-open [file-out (FileOutputStream. filename)] (.write workbook file-out))) (defn sheet-seq "Return a lazy seq of the sheets in a workbook." - [#^Workbook workbook] + [^Workbook workbook] (assert-type workbook Workbook) (for [idx (range (.getNumberOfSheets workbook))] (.getSheetAt workbook idx))) (defn sheet-name "Return the name of a sheet." - [#^Sheet sheet] + [^Sheet sheet] (assert-type sheet Sheet) (.getSheetName sheet)) (defn select-sheet "Select a sheet from the workbook by name." - [name #^Workbook workbook] + [name ^Workbook workbook] (assert-type workbook Workbook) (->> (sheet-seq workbook) (filter #(= name (sheet-name %))) @@ -72,7 +72,7 @@ (defn row-seq "Return a lazy sequence of the rows in a sheet." - [#^Sheet sheet] + [^Sheet sheet] (assert-type sheet Sheet) (iterator-seq (.iterator sheet))) @@ -100,7 +100,7 @@ [sheet-or-row] (vec (for [item (iterator-seq (.iterator sheet-or-row))] item))) -(defn- project-cell [column-map #^Cell cell] +(defn- project-cell [column-map ^Cell cell] (let [colname (-> cell .getColumnIndex org.apache.poi.ss.util.CellReference/convertNumToColString @@ -109,7 +109,7 @@ (when new-key {new-key (read-cell cell)}))) -(defn select-columns [column-map #^Sheet sheet] +(defn select-columns [column-map ^Sheet sheet] "Takes two arguments: column hashmap where the keys are the spreadsheet column names as keys and the values represent the names they are mapped to, and a sheet. @@ -139,14 +139,17 @@ (.. format-helper createDataFormat (getFormat format))) (.setCellStyle cell date-style))) -(defn set-cell! [cell value] - (let [converted-value (cond (number? value) (double value) - true value)] - (.setCellValue cell converted-value) - (if (date-or-calendar? value) - (apply-date-format! cell "m/d/yy")))) - -(defn add-row! [#^Sheet sheet values] +(defn set-cell! [^Cell cell value] + (if (nil? value) + (let [^String null nil] + (.setCellValue cell null)) ;do not call setCellValue(Date) with null + (let [converted-value (cond (number? value) (double value) + true value)] + (.setCellValue cell converted-value) + (if (date-or-calendar? value) + (apply-date-format! cell "m/d/yy"))))) + +(defn add-row! [^Sheet sheet values] (assert-type sheet Sheet) (let [row-num (if (= 0 (.getPhysicalNumberOfRows sheet)) 0 @@ -156,7 +159,7 @@ (set-cell! (.createCell row column-index) value)) row)) -(defn add-rows! [#^Sheet sheet rows] +(defn add-rows! [^Sheet sheet rows] "Add rows to the sheet. The rows is a sequence of row-data, where each row-data is a sequence of values for the columns in increasing order on that row." @@ -166,7 +169,7 @@ (defn add-sheet! "Add a new sheet to the workbook." - [#^Workbook workbook name] + [^Workbook workbook name] (assert-type workbook Workbook) (.createSheet workbook name)) @@ -200,7 +203,7 @@ (create-font! wb {:bold true}) " - [#^Workbook workbook options] + [^Workbook workbook options] (let [defaults {:bold false} cfg (merge defaults options)] (assert-type workbook Workbook) @@ -225,9 +228,9 @@ (create-cell-style! wb {:background :yellow}) " - ([#^Workbook workbook] (create-cell-style! workbook {})) + ([^Workbook workbook] (create-cell-style! workbook {})) - ([#^Workbook workbook styles] + ([^Workbook workbook styles] (assert-type workbook Workbook) (let [cs (.createCellStyle workbook) {background :background, font-style :font} styles @@ -245,7 +248,7 @@ "Apply a style to a cell. See also: create-cell-style!. " - [#^Cell cell #^CellStyle style] + [^Cell cell ^CellStyle style] (assert-type cell Cell) (assert-type style CellStyle) (.setCellStyle cell style) @@ -254,7 +257,7 @@ (defn set-row-style! "Apply a style to all the cells in a row. Returns the row." - [#^Row row #^CellStyle style] + [^Row row ^CellStyle style] (assert-type row Row) (assert-type style CellStyle) (dorun (map #(.setCellStyle % style) (cell-seq row))) diff --git a/test/dk/ative/docjure/spreadsheet_test.clj b/test/dk/ative/docjure/spreadsheet_test.clj index d473a05..7ae955d 100644 --- a/test/dk/ative/docjure/spreadsheet_test.clj +++ b/test/dk/ative/docjure/spreadsheet_test.clj @@ -110,7 +110,7 @@ (deftest read-cell-test (let [sheet-data [["Nil" "Blank" "Date" "String" "Number"] - [nil "" (july 1) "foo" 42]] + [nil "" (july 1) "foo" 42.0]] workbook (create-workbook "Sheet 1" sheet-data) sheet (.getSheetAt workbook 0) rows (vec (iterator-seq (.iterator sheet))) @@ -121,7 +121,7 @@ (is (nil? (read-cell nil-cell))) (is (= "" (read-cell blank-cell))) (is (= (july 1) (read-cell date-cell))) - (is (= 42 (read-cell number-cell)))))) + (is (= 42.0 (read-cell number-cell)))))) (deftest sheet-seq-test @@ -205,8 +205,8 @@ (deftest select-columns-test (let [data [["Name" "Quantity" "Price" "On Sale"] - ["foo" 1 42 true] - ["bar" 2 108 false]] + ["foo" 1.0 42 true] + ["bar" 2.0 108 false]] workbook (create-workbook "Sheet 1" data) sheet (first (sheet-seq workbook))] (testing "Find existing columns should create map."