mjul / docjure

Read and write Office documents from Clojure
MIT License
620 stars 129 forks source link

Writing on an empty cell #91

Closed lucasemmoreira closed 4 years ago

lucasemmoreira commented 4 years ago

Hi!

I am struggling to write in an empty cell. I could not find in the documention. Here is my attempt:

(defn write [xl]
  (let [wb (docjure/load-workbook xl)
        sheet (docjure/select-sheet "Sheet1" wb)
        cell (docjure/select-cell "B6" sheet)
        wrte (docjure/set-cell! cell "Teste2")]
    (docjure/save-workbook! xlsx wb)))

In this example, the spreadsheet has a Sheet1 with no value in the cell B6.

Here was my output

       spreadsheet.clj:  240  dk.ative.docjure.spreadsheet/eval419/fn
          MultiFn.java:  234  clojure.lang.MultiFn/invoke
              AFn.java:  156  clojure.lang.AFn/applyToHelper
              AFn.java:  144  clojure.lang.AFn/applyTo
              Var.java:  705  clojure.lang.Var/applyTo
              core.clj:  665  clojure.core/apply
              core.clj:  660  clojure.core/apply
             debug.clj:  524  cider.nrepl.middleware.debug/apply-instrumented-maybe
             debug.clj:  520  cider.nrepl.middleware.debug/apply-instrumented-maybe
                  REPL:   30  ptc.core/eval7003/write
                  REPL:   35  ptc.core/eval7007
                  REPL:   35  ptc.core/eval7007
         Compiler.java: 7177  clojure.lang.Compiler/eval
         Compiler.java: 7132  clojure.lang.Compiler/eval
              core.clj: 3214  clojure.core/eval
              core.clj: 3210  clojure.core/eval
interruptible_eval.clj:   91  nrepl.middleware.interruptible-eval/evaluate/fn
              main.clj:  437  clojure.main/repl/read-eval-print/fn
              main.clj:  437  clojure.main/repl/read-eval-print
              main.clj:  458  clojure.main/repl/fn
              main.clj:  458  clojure.main/repl
              main.clj:  368  clojure.main/repl
           RestFn.java: 1523  clojure.lang.RestFn/invoke
interruptible_eval.clj:   84  nrepl.middleware.interruptible-eval/evaluate
interruptible_eval.clj:   56  nrepl.middleware.interruptible-eval/evaluate
interruptible_eval.clj:  155  nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
              AFn.java:   22  clojure.lang.AFn/run
           session.clj:  190  nrepl.middleware.session/session-exec/main-loop/fn
           session.clj:  189  nrepl.middleware.session/session-exec/main-loop
              AFn.java:   22  clojure.lang.AFn/run
           Thread.java:  834  java.lang.Thread/run

Analysing with the debugger, cell returned nil and broke on set-cell! call.

If anything else is needed from me, let me know.

Regards!

mjul commented 4 years ago

It is designed for a different use case:

Ideally, you would read the data from the spreadsheet into Clojure data structures and then work with it there using the normal Clojure functions before eventually writing it back. This way, there will be very little friction when using the library.

Working on a cell at a time is much more work, and - as you have found out, requires you to consider more edge cases like non-existing cells etc.

I suggest you try the other approach.

mjul commented 4 years ago

PS: the "2.0" design would address your use case better but it needs someone to drive the implementation since the library is feature complete and good enough for most users including myself.

I still think it would be very elegant: if you are interested, see #58