Edit some excel cells with clojure docjure
I am trying to edit some cells in an excel sheet with clojure docjure ( https://github.com/mjul/docjure ).
The docjure documentation shows how to create a new table, but I don't want to. What I am trying to do is read some information of an excel sheet, calculate some new values and then write them back to specific cells of the same sheet.
Well, the first two steps work. I just don't know how to write the values back.
How can i do this?
+3
source to share
1 answer
It is often helpful to take a look at tests as they can document code as well. I found a file that looked relevant: https://github.com/mjul/docjure/blob/master/test/dk/ative/docjure/spreadsheet_test.clj#L126
And in this file I found:
(deftest set-cell!-test
(let [sheet-name "Sheet 1"
sheet-data [["A1"]]
workbook (create-workbook sheet-name sheet-data)
a1 (-> workbook (.getSheetAt 0) (.getRow 0) (.getCell 0))]
(testing "set-cell! for Date"
(testing "should set value"
(set-cell! a1 (july 1))
(is (= (july 1) (.getDateCellValue a1))))
(testing "should set nil"
(let [^java.util.Date nil-date nil]
(set-cell! a1 nil-date))
(is (= nil (.getDateCellValue a1)))))
(testing "set-cell! for String"
(testing "should set value"
(set-cell! a1 "foo")
(is (= "foo" (.getStringCellValue a1)))))
(testing "set-cell! for boolean"
(testing "should set value"
(set-cell! a1 (boolean true))
(is (.getBooleanCellValue a1))))
(testing "set-cell! for number"
(testing "should set int"
(set-cell! a1 (int 1))
(is (= 1.0 (.getNumericCellValue a1))))
(testing "should set double"
(set-cell! a1 (double 1.2))
(is (= 1.2 (.getNumericCellValue a1)))))))
I can't test it now, but is this what you were looking for?
+2
source to share