Home » excel » excel – Clojure – Docjure: Method works in REPL but not in File

excel – Clojure – Docjure: Method works in REPL but not in File

Posted by: admin May 14, 2020 Leave a comment

Questions:

I just try to read the content of an excel file in clojure. I use the docjure library. When I use the sample code in the REPL, the output is as I wanted it. But after inserting it into the file I got an Wrong number of args – Error for the spreadsheet/select-sheet method.

Here is the code:

(use 'dk.ative.docjure.spreadsheet)

(->> (load-workbook (str (System/getProperty "user.dir") "/resources/public/xls/test.xls")
                    (select-sheet "menu")
                    (select-columns {:A :number, :D :name})
                    ))

The args for this method are [name ^Workbook workbook]. Why does it only need one argument in the REPL but two in the file?

How to&Answers:

Just as Alex said in comments, you messed with parens.

Right now your code code evaluates into:

(load-workbook (str (System/getProperty "user.dir")
                    "/resources/public/xls/test.xls")
               (select-sheet "menu")
               (select-columns {:A :number, :D :name}))

Here is how your actual code should look like:

(->> "/resources/public/xls/test.xls"
     (str (System/getProperty "user.dir"))    ; prefix it with user.dir
     load-workbook                            ; load .xls workbook
     (select-sheet "menu")                    ; select menu sheet
     (select-columns {:A :number, :D :name})) ; select some columns

Which evaluates into:

(select-columns {:A :number, :D :name}
                (select-sheet "menu"
                              (load-workbook (str (System/getProperty "user.dir")
                                                  "/resources/public/xls/test.xls"))))

As you can see, both select-sheet and select-columns are called with two arguments here.

To better understand how thread-last macro ->> works, see its documentation.