Skip to content

Commit

Permalink
Merge remote-tracking branch 'mva/for-upstream'
Browse files Browse the repository at this point in the history
  • Loading branch information
mjul committed Nov 17, 2011
2 parents 9b6be17 + 13ed307 commit 4ce9805
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
3 changes: 1 addition & 2 deletions 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"]
Expand Down
49 changes: 26 additions & 23 deletions src/dk/ative/docjure/spreadsheet.clj
Expand Up @@ -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)
Expand All @@ -44,35 +44,35 @@

(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 %)))
first))

(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)))

Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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."
Expand All @@ -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))

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)))
Expand Down
8 changes: 4 additions & 4 deletions test/dk/ative/docjure/spreadsheet_test.clj
Expand Up @@ -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)))
Expand All @@ -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
Expand Down Expand Up @@ -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."
Expand Down

0 comments on commit 4ce9805

Please sign in to comment.