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

Migration to POI 4.0.1 #81

Merged
merged 7 commits into from
Jun 17, 2020
Merged
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
21 changes: 21 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{:deps
{org.clojure/clojure {:mvn/version "1.10.0"},
org.apache.poi/poi {:mvn/version "4.1.0"},
org.apache.poi/poi-ooxml {:mvn/version "4.1.0"}}

:aliases
{:test
{:extra-paths ["test"],
:extra-deps {org.clojure/test.check {:mvn/version "RELEASE"}}},
:runner
{:extra-deps
{com.cognitect/test-runner
{:git/url "https://github.com/cognitect-labs/test-runner",
:sha "76568540e7f40268ad2b646110f237a60295fa3c"}},
:main-opts ["-m" "cognitect.test-runner" "-d" "test"]},
:1.5 {:extra-deps {org.clojure/clojure {:mvn/version "1.5.1"}}}
:1.6 {:extra-deps {org.clojure/clojure {:mvn/version "1.6.0"}}}
:1.7 {:extra-deps {org.clojure/clojure {:mvn/version "1.7.0"}}}
:1.8 {:extra-deps {org.clojure/clojure {:mvn/version "1.8.0"}}}
:1.9 {:extra-deps {org.clojure/clojure {:mvn/version "1.9.0"}}}
:1.10 {:extra-deps {org.clojure/clojure {:mvn/version "1.10.0"}}}}}
14 changes: 7 additions & 7 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
(defproject dk.ative/docjure "1.14.0-SNAPSHOT"
(defproject dk.ative/docjure "1.14.1-SNAPSHOT"
:description "Easily read and write Office documents from Clojure."
:url "http://github.com/mjul/docjure"
:license {:name "MIT License"
:url "http://http://en.wikipedia.org/wiki/MIT_License"}
:dependencies [[org.clojure/clojure "1.9.0"]
[org.apache.poi/poi "3.17"]
[org.apache.poi/poi-ooxml "3.17"]]
:dependencies [[org.clojure/clojure "1.10.0"]
[org.apache.poi/poi "4.1.0"]
[org.apache.poi/poi-ooxml "4.1.0"]]
:plugins [[lein-difftest "2.0.0"]]
:profiles {:1.5 {:dependencies [[org.clojure/clojure "1.5.1"]]}
:1.6 {:dependencies [[org.clojure/clojure "1.6.0"]]}
:1.7 {:dependencies [[org.clojure/clojure "1.7.0"]]}
:1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]}
:1.9 {:dependencies [[org.clojure/clojure "1.9.0"]]}
:test {:global-vars {*warn-on-reflection* false}
:dependencies [[com.cemerick/pomegranate "0.3.0"]]}}
:aliases {"all" ["with-profile" "1.5:1.6:1.7:1.8:1.9"]}
:1.10 {:dependencies [[org.clojure/clojure "1.10.0"]]}
:test {:global-vars {*warn-on-reflection* false}}}
:aliases {"all" ["with-profile" "1.5:1.6:1.7:1.8:1.9:1.10"]}
:global-vars {*warn-on-reflection* true})

36 changes: 18 additions & 18 deletions src/dk/ative/docjure/spreadsheet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(java.util Date Calendar)
(org.apache.poi.xssf.usermodel XSSFWorkbook)
(org.apache.poi.hssf.usermodel HSSFWorkbook)
(org.apache.poi.ss.usermodel Workbook Sheet Cell Row
(org.apache.poi.ss.usermodel Workbook Sheet Cell Row CellType
Row$MissingCellPolicy
HorizontalAlignment
VerticalAlignment
Expand All @@ -27,33 +27,33 @@
(.formatAsString (CellReference. (.getRowIndex cell) (.getColumnIndex cell))))

(defmulti read-cell-value (fn [^CellValue cv date-format?] (.getCellType cv)))
(defmethod read-cell-value Cell/CELL_TYPE_BOOLEAN [^CellValue cv _] (.getBooleanValue cv))
(defmethod read-cell-value Cell/CELL_TYPE_STRING [^CellValue cv _] (.getStringValue cv))
(defmethod read-cell-value Cell/CELL_TYPE_NUMERIC [^CellValue cv date-format?]
(defmethod read-cell-value CellType/BOOLEAN [^CellValue cv _] (.getBooleanValue cv))
(defmethod read-cell-value CellType/STRING [^CellValue cv _] (.getStringValue cv))
(defmethod read-cell-value CellType/NUMERIC [^CellValue cv date-format?]
(if date-format?
(DateUtil/getJavaDate (.getNumberValue cv))
(.getNumberValue cv)))
(defmethod read-cell-value Cell/CELL_TYPE_ERROR [^CellValue cv _]
(defmethod read-cell-value CellType/ERROR [^CellValue cv _]
(keyword (.name (FormulaError/forInt (.getErrorValue cv)))))

(defmulti read-cell #(when % (.getCellType ^Cell %)))
(defmethod read-cell Cell/CELL_TYPE_BLANK [_] nil)
(defmethod read-cell CellType/BLANK [_] nil)
(defmethod read-cell nil [_] nil)
(defmethod read-cell Cell/CELL_TYPE_STRING [^Cell cell] (.getStringCellValue cell))
(defmethod read-cell Cell/CELL_TYPE_FORMULA [^Cell cell]
(defmethod read-cell CellType/STRING [^Cell cell] (.getStringCellValue cell))
(defmethod read-cell CellType/FORMULA [^Cell cell]
(let [evaluator (.. cell getSheet getWorkbook
getCreationHelper createFormulaEvaluator)
cv (.evaluate evaluator cell)]
(if (and (= Cell/CELL_TYPE_NUMERIC (.getCellType cv))
(if (and (= CellType/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]
(defmethod read-cell CellType/BOOLEAN [^Cell cell] (.getBooleanCellValue cell))
(defmethod read-cell CellType/NUMERIC [^Cell cell]
(if (DateUtil/isCellDateFormatted cell)
(.getDateCellValue cell)
(.getNumericCellValue cell)))
(defmethod read-cell Cell/CELL_TYPE_ERROR [^Cell cell]
(defmethod read-cell CellType/ERROR [^Cell cell]
(keyword (.name (FormulaError/forInt (.getErrorCellValue cell)))))


Expand Down Expand Up @@ -214,7 +214,7 @@
(apply merge)))))

(defn string-cell? [^Cell cell]
(= Cell/CELL_TYPE_STRING (.getCellType cell)))
(= CellType/STRING (.getCellType cell)))

(defn- date-or-calendar? [value]
(let [cls (class value)]
Expand All @@ -237,25 +237,25 @@
(defmulti set-cell! (fn [^Cell cell val] (type val)))

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

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

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

(defmethod set-cell! Date [^Cell cell val]
(if (= (.getCellType cell) Cell/CELL_TYPE_FORMULA) (.setCellType cell Cell/CELL_TYPE_NUMERIC))
(if (= (.getCellType cell) CellType/FORMULA) (.setCellType cell CellType/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]
(if (= (.getCellType cell) Cell/CELL_TYPE_FORMULA) (.setCellType cell Cell/CELL_TYPE_BLANK))
(if (= (.getCellType cell) CellType/FORMULA) (.setCellType cell CellType/BLANK))
(.setCellValue cell null)))

(defn add-row! [^Sheet sheet values]
Expand Down
9 changes: 2 additions & 7 deletions test/dk/ative/docjure/spreadsheet_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns dk.ative.docjure.spreadsheet-test
(:use [dk.ative.docjure.spreadsheet] :reload-all)
(:use [clojure.test])
(:require [cemerick.pomegranate :as pomegranate]
(:require
[clojure.java.io :as io])
(:import (org.apache.poi.ss.usermodel Workbook Row
Row$MissingCellPolicy
Expand Down Expand Up @@ -802,12 +802,6 @@
(let [loaded (load-workbook stream)]
(test-loaded-workbook loaded)))))

(deftest load-workbook-from-resource-integration-test
(let [[dir file] (path->dir-and-file (config :datatypes-file))
_ (pomegranate/add-classpath dir)
loaded (load-workbook-from-resource file)]
(test-loaded-workbook loaded)))

(deftest save-workbook!-test
(testing "Should fail on invalid parameter types."
(is (thrown-with-msg? IllegalArgumentException #"workbook.*" (save-workbook! "filename.xlsx" "not-a-workbook"))))
Expand All @@ -823,6 +817,7 @@
stream (io/output-stream file)
workbook (create-workbook "Sheet 1" [["A1" "B1" "C1"]])
_ (save-workbook! stream workbook)
_ (.close stream)
loaded (load-workbook file)
_ (io/delete-file file)]
(test-loaded-workbook loaded))))
Expand Down