LISP doesn't write to Excel spread sheet

LISP doesn't write to Excel spread sheet

Anonymous
Not applicable
3,671 Views
21 Replies
Message 1 of 22

LISP doesn't write to Excel spread sheet

Anonymous
Not applicable

Hello!  It appears that Excel 2016 won't allow AutoCAD 2016 to input data in the background.  I have a lisp routine that tracks the drawings I work in where it opens an existing spread sheet, writes to it, saves it, and then closed the file.  It has worked for quite sometime when using OpenOffice.  It only worked for a little bit with Excel 2016 then stopped.  The lisp routine opens the file, saves it, and closes it but without adding the data.  It appears that some settings may not be correct in Excel because it works fine writing to a .txt file.  Has anyone else ran it to this type of problem with Excel 2016? Thank you!

0 Likes
3,672 Views
21 Replies
Replies (21)
Message 2 of 22

DGRL
Advisor
Advisor

Pls upload the code you use

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 3 of 22

Anonymous
Not applicable

Here's the portion in question.  Thanks helping!

0 Likes
Message 4 of 22

DGRL
Advisor
Advisor

@Anonymous

Try to write to XLS and not to XLSX

It should work without an issue

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 5 of 22

Anonymous
Not applicable

I changed it to .xlsx this morning to test it, didn't work, had been using .xls originally.  It worked for a few days back in December and then stopped after we installed a new computer using Excel 2016.  This worked fine the last 2 1/2 years while using OpenOffice Calc as my spread sheet application.  I'm in the process of reinstalling OpenOffice Calc and setting that as my default spreadsheet application, I expect that to work fine again.  Thank you for attempting to help!  I appreciate it!

0 Likes
Message 6 of 22

DGRL
Advisor
Advisor

@Anonymous

If you do not want to share your whole code here which is fine but pls PM me the whole code so I can see how to fix
Coz it is absolutely possible to write and read from excel 2016

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 7 of 22

Anonymous
Not applicable

Thank you. I don't mind putting the whole code here, the rest of the acaddoc has nothing to do with the drawing time log. Here's the whole acaddoc and the output excel file.


0 Likes
Message 8 of 22

Anonymous
Not applicable

I changed the output file to a .txt file and it worked as expected.

0 Likes
Message 9 of 22

DGRL
Advisor
Advisor

@Anonymous

 

 

Did you also changed the version of ACAD?

Coz im running ACAD Plant 3D 2016 and it looks like the code loads for about 50%
The other 50% does not even load ( as it looks like )
also what is the command to start the routine?

 

c:zw
c:za
c:zp
c:ze
c:beb
These seems to soom and beb just demolish the current dwg and make screen grey

The redefined saveas command seems to be broken

 

 

The commands F1 and checkeditprops are also missing
And i don't see any code that invokes the "excel engine" needed to open and close xls files

 

maybe you can use this routine in order to get things work for you

http://web2.airmail.net/terrycad/LISP/GetExcel.lsp

 


Maybe my system is not configured correct.
Still trying to see what the issue is and how to fix LOL

 

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 10 of 22

Anonymous
Not applicable
No, still the same version, AutoCAD 2016, even worked with AutoCAD 2014 from which we upgraded from a couple of years ago. The part of the routine that adds a line to the .xls file runs automatically when the "close" command is executed in a drawing other than the default "drawing1.dwg". BEB opens a block called "B" to edit it, that is why the screen appears gray. I can't remember why I redid my save command, has something to do with the drawing log.


0 Likes
Message 11 of 22

DGRL
Advisor
Advisor

Thanks for this info

Will let you know soon 
 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 12 of 22

Anonymous
Not applicable
F1 and checkeditprops are separate lisp routines. Thanks for your help and advice!


0 Likes
Message 13 of 22

DGRL
Advisor
Advisor

@Anonymous

 

 

When opening a dwg while the code of you is saved in my acaddoc.lsp  gives me 2 big blue circles with a big cross (see screenshot dwg)
When closing this dwg without saving it (see screenshot dwg1)

When closing it ends up with a nil so something is broke in the code

 

 

Hope this helps you

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 14 of 22

Anonymous
Not applicable

Yes, I got that same error previously.  The first time I uploaded the .lsp file, it was missing a closing paren after the setq in "defun cadlog-close".  I fixed that and uploaded it again.  Probably just be more simpler for you to add that missing closing paren in that location.

0 Likes
Message 15 of 22

DGRL
Advisor
Advisor

Hi

 

Even after "fixing" this missing parent I keep getting NIL when closing the dwg
Can you check the variables you have in the closing section of the lisp

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 16 of 22

Anonymous
Not applicable

Hi,

 

I couldn't get back with you yesterday, quite busy at work. Anyway, the lisp works fine when I replace the .xls with .txt. Those variables do not cause a nil for me. Sorry for the confusion as I don't have the experience to write routines that work for anyone else, just for me on my PC. But, the top portion of acaddoc.lsp that creates the drawing log can be used independently. I'm certain you can figure out what you'd need to do to do that. I appreciate your interest in assisting.

0 Likes
Message 17 of 22

DGRL
Advisor
Advisor

HI @Anonymous

 

Sorry was not able to answer you last weekend

If I have some time left Ill try to fix it

 

Maybe someone else can assist here also

 

If this was of any help please kudo and/or Accept as Solution
Kind Regards
0 Likes
Message 18 of 22

terrycadd
Enthusiast
Enthusiast

The website http://web2.airmail.net/terrycad that hosted AutoLISP Exchange, GetExcel and Getting Started with DCL Dialogs has been moved to the new domain https://autolisp-exchange.com . All programs and files are free to download and share. Just click on a button to view the program, then right-click and choose Save as...

Here is a direct link to GetExcel.lsp:  https://autolisp-exchange.com/LISP/GetExcel.lsp

After loading GetExcel, here is a small test routine that you can play around with to get some ideas.

Open Excel and save a blank excel file to a folder like C:\Temp\Template.xlsx and close it.

 

(defun c:TestExcel (/ Cnt# DataList@ List@ Range$ XlsFilename$)
  (setq XlsFilename$ "C:\\Temp\\Template.xlsx")
  (OpenExcel XlsFilename$ nil nil)
  ;One cell at a time
  (PutCell "A1" "Info")
  (PutCell "B1" "Goes")
  (PutCell "C1" "Here")
  (setq DataList@
    (list
      (list "One" "Two" "Three")
      (list 12.34 23.45 34.56)
      (list "Add" "More" "Data")
    );list
  );setq
  ;Put in rows a list of data
  (setq Cnt# 2);Start with 2nd row
  (foreach List@ DataList@
    (setq Range$ (strcat "A" (itoa Cnt#)))
    (PutCell Range$ List@)
    (setq Cnt# (1+ Cnt#))
  );foreach
  (CloseExcel XlsFilename$)
  (princ (strcat "\n" XlsFilename$ " file updated."))
  (princ)
);defun

 

 

0 Likes
Message 19 of 22

CAD_Programmer_LHS
Observer
Observer

Hi Terrycad!

 

Thanks for posting the code and links, much appreciated! 

 

It's been an amazing long while since I posted this question, I'm glad you took the time to post some helpful code.  Below is the code in my acaddoc.lsp that currently appends to a .txt file, previous to that, the only change, I believe, was appending to a .xlsx file, which worked for quite a while and then stopped suddenly.  Hence, I inserted the .log file instead.  Here's the code used to append to the .log file:

 

(if (not (wcmatch (getvar 'DWGNAME) "Drawing*.dwg"))
	  (progn

(setq begin_time (getvar "tdusrtimer"))

(vl-load-com)

(princ "\nTime marches on!!!\n")

(if (/= 'VLR-Command-Reactor (type ReactorCommandWillStart))
(setq ReactorCommandWillStart
(vlr-command-reactor nil
'((:vlr-commandWillStart . CallbackCommandWillStart))
)
)
)

(defun CallbackCommandWillStart ( reactor cmdInfo / cmdName )
(cond
( (eq "CLOSE" (setq cmdName (car cmdInfo)))
(CADLOG-close)
)
)
)

(defun cadlog-open ()
(setq open_time (getvar "cdate")
date_lbl-open (strcat (thedate3 open_time "_"))
;log_file (open (strcat "C:\\Users\\jkaufman\\Documents\\dwgslog.log") "a")
);setq
;(write-line
;(strcat "**Opened\t"
;(thedate3 open_time "-")
;"\t"
;(thetime3 open_time nil)
;"\t"
;job_no
;dwg_no
;)
;log_file
;)
;(close log_file)
)

(defun cadlog-close (/ user_name job_no dwg_no close_time log_file)
(setq job_no (getvar "dwgprefix")
dwg_no (getvar "dwgname")
close_time (getvar "cdate")
end_time (getvar "tdusrtimer")
date_lbl-close (strcat (thedate3 close_time "_"))
time_worked (rtos (/ (/ (* (- end_time begin_time) 86400) 60) 60) 2 3)
time_worked2 (/ (/ (* (- end_time begin_time) 86400) 60) 60)
t_w_m (rtos (* (atof time_worked) 60) 2 3)
t_w_m2 (* time_worked2 60)
min-num (fix t_w_m2)
t_w_s (rtos (* (- t_w_m2 min-num) 60) 2 2)
log_file (open (strcat "C:\\Users\\jkaufman\\Documents\\dwgslog.txt") "a")
)
(write-line
(strcat "**Opened\t"
(thedate3 open_time "-")
"\t"
(thetime3 open_time nil)
"\t"
job_no
dwg_no
"\t"
"**Closed\t"
(thedate3 close_time "-")
"\t"
(thetime3 close_time nil)
"\t"
(strcat time_worked " hrs.")
"\t"
(strcat (itoa min-num) " min. " t_w_s " sec.")
)
log_file
)
(close log_file)
(princ "\nTime marched on!!!\n")
)

(defun thedate3 (date1 sep1 /)
;;return the date as a string formatted like 03-06-2000
;;date1 = the date as returned by (getvar "cdate")
;;sep1 = the separator to be used (- of /)
(setq date1 (rtos date1 2 8))
(strcat (substr date1 5 2)
"-"
(substr date1 7 2)
"-"
(substr date1 1 4)
)
) ;_ end defun thedate3

(defun thetime3 (date1 dec1 /)
;;return the time as a string formatted like 14:23:55 or 14:23:55.35
;;date1 = the date as returned by (getvar "cdate")
;;dec1 = if t, include decimals of a second, else hh:mm:ss.
(setq date1 (rtos date1 2 8))
(if dec1
(strcat (substr date1 10 2)
":"
(substr date1 12 2)
":"
(substr date1 14 2)
"."
(substr date1 16 2)
)
(strcat (substr date1 10 2)
":"
(substr date1 12 2)
":"
(substr date1 14 2)
)
) ;_ end if
) ;_ end defun thetime3

(CADLOG-open)

  );end progn
);end if

This is the output in the .txt log file:


"**Opened 04-07-2020 13:12:19 W:\cust\E2055020.dwg **Closed 04-07-2020 13:14:44 0.040 hrs. 2 min. 24.21 sec.
**Opened 04-07-2020 13:18:58 W:\cust\E2055020.dwg **Closed 04-07-2020 13:21:52 0.048 hrs. 2 min. 54.33 sec.
**Opened 04-08-2020 06:29:43 W:\cust\ASSET 367726.dwg **Closed 04-08-2020 06:31:53 0.036 hrs. 2 min. 9.84 sec.
**Opened 04-08-2020 09:19:10 W:\cust\INLET POST B PLATE.dwg **Closed 04-08-2020 09:20:14 0.018 hrs. 1 min. 3.66 sec."  ("cust" would be in place of the actual customer name).

 

How would you adjust your code to have the next line of text for the spreadsheet automatically start in the next row down?

 

Thank you!

Drafter_Joe

 

0 Likes
Message 20 of 22

terrycadd
Enthusiast
Enthusiast

Here is a small routine that I worked up, that shows how to append one line at a time to an excel sheet. You may need to format the excel columns where needed.  Be sure and edit the folder and excel filename in the following example code.

(defun c:AppendExcel (/ Data@ Info1$ Info2$ Info3$ Info4$ Range$ Row# Template@ XlsFilename$)
  (setq XlsFilename$ "C:\\Temp\\Template.xlsx")
  (setq Template@ (GetExcel XlsFilename$ nil nil))
  (setq Row# (1+ (length Template@)))
  (setq Info1$ (getvar "loginname"))
  (setq Info2$ (getvar "dwgname"))
  (setq Info3$ (GetDate))
  (setq Info4$ (GetTime))
  (setq Data@ (list Info1$ Info2$ Info3$ Info4$))
  (setq Range$ (strcat "A" (itoa Row#)))
  (OpenExcel XlsFilename$ nil nil)
  (PutCell Range$ Data@)
  (CloseExcel XlsFilename$)
  (princ (strcat "\n" XlsFilename$ " file updated."))
  (princ)
);defun
(defun GetDate (/ ToDay$)
  (setq ToDay$ (rtos (getvar "cdate") 2 0))
  (strcat (substr ToDay$ 5 2)"/"(substr ToDay$ 7 2)"/"(substr ToDay$ 3 2))
);defun GetDate
(defun GetTime (/ Time$ Hour$ Min$ AM$)
  (setq Time$ (substr (rtos (getvar "cdate") 2 4) 10)
        Hour$ (substr Time$ 1 2)
        Min$ (substr Time$ 3 2)
        AM$ " AM"
  );setq
  (if (and (> (atoi Hour$) 11) (< (atoi Hour$) 24))
    (setq AM$ " PM")
  );if
  (if (< (atoi Hour$) 10)
    (setq Hour$ (substr Hour$ 2 1))
  );if
  (if (> (atoi Hour$) 12)
    (setq Hour$ (itoa (- (atoi Hour$) 12)))
  );if
  (strcat Hour$ ":" Min$ AM$)
);defun GetTime
0 Likes