autolisp-excel cell reading

autolisp-excel cell reading

Anonymous
Not applicable
6,161 Views
6 Replies
Message 1 of 7

autolisp-excel cell reading

Anonymous
Not applicable

i hv one excel file containing 4 column values. I can select first cell by this ' setq a (read-line c b),

but, i couldn't select next cell (RHS) by lisp. what is the program for that? any tab option will work? pls help me

0 Likes
Accepted solutions (3)
6,162 Views
6 Replies
Replies (6)
Message 2 of 7

devitg
Advisor
Advisor
Accepted solution

It is easy , if you save your XLS 4 columns as CSV , then you can read it easy .

0 Likes
Message 3 of 7

roland.r71
Collaborator
Collaborator

You are lucky to get anything.

 

(read-line [file-desc]) is NOT ment/suited/capable for reading Excel files. (.XLS or XLSX)

Besides that, it only takes 1 argument, not 2. (setq a (read-line c b)) ??? what's that "b" for? it only takes an open file for argument ...)

 

As the name suggests, it reads a line from a (text) file, until the EOL character. ([Enter]=EOL, End Of Line)

Which has nothing to do with cells.

 

Either you should use a ODBC connection to read the file, or save it as a .CSV file (like @devitg already suggested). You can easily do it like that, just keep in mind that a LINE will have ALL columns in it. (so you still need to parse it into cell values...)

 

Here's an example of a function to read a CSV file into a list of lists:

   (defun readCSV ( / file_r record values csvLst)

      ; Read settings from csv file
      ; Returns list of lists with values (records with values)

      (setq file_r (open csvfile "r")
            csvLst nil
      )
      (while (setq record (read-line file_r))
         (setq values nil)
         (while (> (setq pos (vl-string-position (ascii "\t") record)) 0)
            (setq values (append values (list (substr record 1 pos)))
                  record (substr record (+ 2 pos))
            )
         )
         (if (> (strlen record) 0)
            (setq values (append values (list record)))
         )
         (if values (setq csvLst (append csvLst (list values))))
      )
      csvLst
   )

 

 

This will turn your 4 columns into a list of 'records' with each 4 'values'.

same as:

(list

   (list 1 2 3 4)

   (list 1 2 3 4)

)

Message 4 of 7

Anonymous
Not applicable

thank you very much. Actually my purpose is, i have to connect excel file with autocad and if i am putting some values as x and y it should draw a curve/graph on the screen. As i am instructed by my boss, i have to use excel file for this. is it possible in excel to read cell values as we are giving cell number by using lisp??

0 Likes
Message 5 of 7

roland.r71
Collaborator
Collaborator
Accepted solution

Then you'll need something like this: GetCells

0 Likes
Message 6 of 7

Anonymous
Not applicable

Hi,

 I tried getcell function. But it is not working in my system. is it require any extra software or something? please give me a solution.

0 Likes
Message 7 of 7

roland.r71
Collaborator
Collaborator
Accepted solution

It works for me (without anything extra installed)

...but it doesn't check any inputs. Supply a wrong filename (or path), incorrect sheetname, use a1 instead of A1 and it fails.

& it fails hard: ; error: bad argument type: VLA-OBJECT #<%catch-all-apply-error%>

 

But:

getcells

- browse to file . . . . . . . . . . . . C:\\lisp\\getcel.xlsx

- supply sheetname . . . . . . . . Sheet1

- supply fields . . . . . . . . . . . . . A1

Returns the cells value for me.

The value of address A1 is: a

 

So does: (getCellsFunction "C:\\lisp\\getcel.xlsx" "Sheet1" "A1:D1")

("a" "b" "c" "d")

0 Likes