Author Topic: Change color of Excel Cell based on duplicate tag value- help!  (Read 2437 times)

0 Members and 1 Guest are viewing this topic.

Bhull1985

  • Guest
Hello all, I'm in the middle of constructing a routine that will take all attribute values from specific blocks in autocad and place them into excel, formatted.
I'm progressing well but in handling the issue of duplicate tags, we've decided to flag the user and make a count of the number of duplicates, but not to remove them programmatically.
I believe that coloring the excel cells in which there are duplicate tags would be an effective way to accomplish this, and in scouring the internet and cad forums for "change excel cell color" I've found little that would work with what I've got so far.
I've pieced together an application from GetExcel.lsp and various Lee Mac snippets, and it's working pretty well- I just am a bit confused on how to interact with an existing excel file, because all of the routines for coloring excel cells have the lines (vlax-get-or-create-object) to create the excel object. In my routine the excel file will already exist and have information put into it, so i'd need to establish a correct pointer to my open excel file, and then color the cells in which there are duplicate tag values.
I've yet to construct the logic in order to find the duplicate tag values but I believe I could handle that part, I just am hoping for some assistance with coloring of the cells WITH the routine I've got so far. Here it is....and thanks in advance!!

Code: [Select]
;-------------------------------------------------------------------------------
; Program Name: GetExcel.lsp [GetExcel R4]
; Created By:   Terry Miller (Email: terrycadd@yahoo.com)
;               (URL: http://web2.airmail.net/terrycad)
; Date Created: 9-20-03
; Function:     Several functions to get and put values into Excel cells.
;-------------------------------------------------------------------------------
; Revision History
; Rev  By     Date    Description
;-------------------------------------------------------------------------------
; 1    TM   9-20-03   Initial version
; 2    TM   8-20-07   Rewrote GetExcel.lsp and added several new sub-functions
;                     including ColumnRow, Alpha2Number and Number2Alpha written
;                     by Gilles Chanteau from Marseille, France.
; 3    TM   12-1-07   Added several sub-functions written by Gilles Chanteau
;                     including Cell-p, Row+n, and Column+n. Also added his
;                     revision of the PutCell function.
; 4    GC   9-20-08   Revised the GetExcel argument MaxRange$ to accept a nil
;                     and get the current region from cell A1.
;-------------------------------------------------------------------------------
; Overview of Main functions
;-------------------------------------------------------------------------------
; GetExcel - Stores the values from an Excel spreadsheet into *ExcelData@ list
;   Syntax:  (GetExcel ExcelFile$ SheetName$ MaxRange$)
;   Example: (GetExcel "Y:\\Bhull\\Attributes.csv" "Sheet1" "B49")
; GetCell - Returns the cell value from the *ExcelData@ list
;   Syntax:  (GetCell Cell$)
;   Example: (GetCell "H15")
; Function example of usage:
; (defun c:Get-Example ()
;   (GetExcel "C:\\Folder\\Filename.xls" "Sheet1" "L30");<-- Edit Filename.xls
;   (GetCell "H21");Or you can just use the global *ExcelData@ list
; );defun
;-------------------------------------------------------------------------------
; OpenExcel - Opens an Excel spreadsheet
;   Syntax:  (OpenExcel ExcelFile$ SheetName$ Visible)
;   Example: (OpenExcel "C:\\Folder\\Filename.xls" "Sheet1" nil)
; PutCell - Put values into Excel cells
;   Syntax:  (PutCell StartCell$ Data$) or (PutCell StartCell$ DataList@)
;   Example: (PutCell "A1" (list "GP093" 58.5 17 "Base" "3'-6 1/4\""))
; CloseExcel - Closes Excel session
;   Syntax:  (CloseExcel ExcelFile$)
;   Example: (CloseExcel "C:\\Folder\\Filename.xls")
; Function example of usage:
; (defun c:Put-Example ()
;   (OpenExcel "C:\\Folder\\Filename.xls" "Sheet1" nil);<-- Edit Filename.xls
;   (PutCell "A1" (list "GP093" 58.5 17 "Base" "3'-6 1/4\""));Repeat as required
;   (CloseExcel "C:\\Folder\\Filename.xls");<-- Edit Filename.xls
;   (princ)
; );defun
;-------------------------------------------------------------------------------
; Note: Review the conditions of each argument in the function headings
;-------------------------------------------------------------------------------
; GetExcel - Stores the values from an Excel spreadsheet into *ExcelData@ list
; Arguments: 3
;   ExcelFile$ = Path and filename
;   SheetName$ = Sheet name or nil for not specified
;   MaxRange$ = Maximum cell ID range to include or nil to get the current region from cell A1
; Syntax examples:
; (GetExcel "C:\\Temp\\Temp.xls" "Sheet1" "E19") = Open C:\Temp\Temp.xls on Sheet1 and read up to cell E19
; (GetExcel "C:\\Temp\\Temp.xls" nil "XYZ123") = Open C:\Temp\Temp.xls on current sheet and read up to cell XYZ123
;-------------------------------------------------------------------------------
(defun GetExcel (ExcelFile$ SheetName$ MaxRange$ / Column# ColumnRow@ Data@ ExcelRange^
  ExcelValue ExcelValue ExcelVariant^ MaxColumn# MaxRow# Range$ Row# Worksheet)
  (if (= (type ExcelFile$) 'STR)
    (if (not (findfile ExcelFile$))
      (progn
        (alert (strcat "Excel file " ExcelFile$ " not found."))
        (exit)
      );progn
    );if
    (progn
      (alert "Excel file not specified.")
      (exit)
    );progn
  );if
  (gc)
  (if (setq *ExcelApp% (vlax-get-object "Excel.Application"))
    (progn
      (alert "Close all Excel spreadsheets to continue!")
      (vlax-release-object *ExcelApp%)(gc)
    );progn
  );if
  (setq ExcelFile$ (findfile ExcelFile$))
  (setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))
  (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Open ExcelFile$)
  (if SheetName$
    (vlax-for Worksheet (vlax-get-property *ExcelApp% "Sheets")
      (if (= (vlax-get-property Worksheet "Name") SheetName$)
        (vlax-invoke-method Worksheet "Activate")
      );if
    );vlax-for
  );if
  (if MaxRange$
    (progn
      (setq ColumnRow@ (ColumnRow MaxRange$))
      (setq MaxColumn# (nth 0 ColumnRow@))
      (setq MaxRow# (nth 1 ColumnRow@))
    );progn
    (progn
      (setq CurRegion (vlax-get-property (vlax-get-property
        (vlax-get-property *ExcelApp% "ActiveSheet") "Range" "A1") "CurrentRegion")
      );setq
      (setq MaxRow# (vlax-get-property (vlax-get-property CurRegion "Rows") "Count"))
      (setq MaxColumn# (vlax-get-property (vlax-get-property CurRegion "Columns") "Count"))
    );progn
  );if
  (setq *ExcelData@ nil)
  (setq Row# 1)
  (repeat MaxRow#
    (setq Data@ nil)
    (setq Column# 1)
    (repeat MaxColumn#
      (setq Range$ (strcat (Number2Alpha Column#)(itoa Row#)))
      (setq ExcelRange^ (vlax-get-property *ExcelApp% "Range" Range$))
      (setq ExcelVariant^ (vlax-get-property ExcelRange^ 'Value))
      (setq ExcelValue (vlax-variant-value ExcelVariant^))
      (setq ExcelValue
        (cond
          ((= (type ExcelValue) 'INT) (itoa ExcelValue))
          ((= (type ExcelValue) 'REAL) (rtosr ExcelValue))
          ((= (type ExcelValue) 'STR) (vl-string-trim " " ExcelValue))
          ((/= (type ExcelValue) 'STR) "")
        );cond
      );setq
      (setq Data@ (append Data@ (list ExcelValue)))
      (setq Column# (1+ Column#))
    );repeat
    (setq *ExcelData@ (append *ExcelData@ (list Data@)))
    (setq Row# (1+ Row#))
  );repeat
  (vlax-invoke-method (vlax-get-property *ExcelApp% "ActiveWorkbook") 'Close :vlax-False)
  (vlax-invoke-method *ExcelApp% 'Quit)
  (vlax-release-object *ExcelApp%)(gc)
  (setq *ExcelApp% nil)
  *ExcelData@
);defun GetExcel
;-------------------------------------------------------------------------------
; GetCell - Returns the cell value from the *ExcelData@ list
; Arguments: 1
;   Cell$ = Cell ID
; Syntax example: (GetCell "E19") = value of cell E19
;-------------------------------------------------------------------------------
(defun GetCell (Cell$ / Column# ColumnRow@ Return Row#)
  (setq ColumnRow@ (ColumnRow Cell$))
  (setq Column# (1- (nth 0 ColumnRow@)))
  (setq Row# (1- (nth 1 ColumnRow@)))
  (setq Return "")
  (if *ExcelData@
    (if (and (>= (length *ExcelData@) Row#)(>= (length (nth 0 *ExcelData@)) Column#))
      (setq Return (nth Column# (nth Row# *ExcelData@)))
    );if
  );if
  Return
);defun GetCell
;-------------------------------------------------------------------------------
; OpenExcel - Opens an Excel spreadsheet
; Arguments: 3
;   ExcelFile$ = Excel filename or nil for new spreadsheet
;   SheetName$ = Sheet name or nil for not specified
;   Visible = t for visible or nil for hidden
; Syntax examples:
; (OpenExcel "C:\\Temp\\Temp.xls" "Sheet2" t) = Opens C:\Temp\Temp.xls on Sheet2 as visible session
; (OpenExcel "C:\\Temp\\Temp.xls" nil nil) = Opens C:\Temp\Temp.xls on current sheet as hidden session
; (OpenExcel nil "Parts List" nil) =  Opens a new spreadsheet and creates a Part List sheet as hidden session
;-------------------------------------------------------------------------------
(defun OpenExcel (ExcelFile$ SheetName$ Visible / Sheet$ Worksheet)
  (if (= (type ExcelFile$) 'STR)
    (if (findfile ExcelFile$)
      (setq *ExcelFile$ ExcelFile$)
      (progn
        (alert (strcat "Excel file " ExcelFile$ " not found."))
        (exit)
      );progn
    );if
    (setq *ExcelFile$ "")
  );if
  (gc)
  (if (setq *ExcelApp% (vlax-get-object "Excel.Application"))
    (progn
;      (alert "Close all Excel spreadsheets to continue!")
      (vlax-release-object *ExcelApp%)(gc)
    );progn
  );if
  (setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))
  (if ExcelFile$
    (if (findfile ExcelFile$)
      (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Open ExcelFile$)
      (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Add)
    );if
    (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Add)
  );if
  (if Visible
    (vla-put-visible *ExcelApp% :vlax-true)
  );if
  (if (= (type SheetName$) 'STR)
    (progn
      (vlax-for Sheet$ (vlax-get-property *ExcelApp% "Sheets")
        (setq Sheets@ (append Sheets@ (list (vlax-get-property Sheet$ "Name"))))
      );vlax-for
      (if (member SheetName$ Sheets@)
        (vlax-for Worksheet (vlax-get-property *ExcelApp% "Sheets")
          (if (= (vlax-get-property Worksheet "Name") SheetName$)
            (vlax-invoke-method Worksheet "Activate")
          );if
        );vlax-for
        (vlax-put-property (vlax-invoke-method (vlax-get-property *ExcelApp% "Sheets") "Add") "Name" SheetName$)
      );if
    );progn
  );if
  (princ)
);defun OpenExcel
;-------------------------------------------------------------------------------
; PutCell - Put values into Excel cells
; Arguments: 2
;   StartCell$ = Starting Cell ID
;   Data@ = Value or list of values
; Syntax examples:
; (PutCell "A1" "PART NUMBER") = Puts PART NUMBER in cell A1
; (PutCell "B3" '("Dim" 7.5 "9.75")) = Starting with cell B3 put Dim, 7.5, and 9.75 across
;-------------------------------------------------------------------------------
(defun PutCell (StartCell$ Data@ / Cell$ Column# ExcelRange Row#)
  (if (= (type Data@) 'STR)
    (setq Data@ (list Data@))
  )
  (setq ExcelRange (vlax-get-property *ExcelApp% "Cells"))
  (if (Cell-p StartCell$)
    (setq Column# (car (ColumnRow StartCell$))
          Row# (cadr (ColumnRow StartCell$))
    );setq
    (if (vl-catch-all-error-p
          (setq Cell$ (vl-catch-all-apply 'vlax-get-property
            (list (vlax-get-property *ExcelApp% "ActiveSheet") "Range" StartCell$))
          );setq
        );vl-catch-all-error-p
        (alert (strcat "The cell ID \"" StartCell$ "\" is invalid."))
        (setq Column# (vlax-get-property Cell$ "Column")
              Row# (vlax-get-property Cell$ "Row")
        );setq
    );if
  );if
  (if (and Column# Row#)
    (foreach Item Data@
      (vlax-put-property ExcelRange "Item" Row# Column# (vl-princ-to-string Item))
      (setq Column# (1+ Column#))
    );foreach
  );if
  (princ)
);defun PutCell
;-------------------------------------------------------------------------------
; CloseExcel - Closes Excel spreadsheet
; Arguments: 1
;   ExcelFile$ = Excel saveas filename or nil to close without saving
; Syntax examples:
; (CloseExcel "C:\\Temp\\Temp.xls") = Saveas C:\Temp\Temp.xls and close
; (CloseExcel nil) = Close without saving
;-------------------------------------------------------------------------------
(defun CloseExcel (ExcelFile$ / Saveas)
  (if ExcelFile$
    (if (= (strcase ExcelFile$) (strcase *ExcelFile$))
      (if (findfile ExcelFile$)
        (vlax-invoke-method (vlax-get-property *ExcelApp% "ActiveWorkbook") "Save")
        (setq Saveas t)
      );if
      (if (findfile ExcelFile$)
        (progn
          (vl-file-delete (findfile ExcelFile$))
          (setq Saveas t)
        );progn
        (setq Saveas t)
      );if
    );if
  );if
  (if Saveas
    (vlax-invoke-method (vlax-get-property *ExcelApp% "ActiveWorkbook")
      "SaveAs" ExcelFile$ -4143 "" "" :vlax-false :vlax-false nil
    );vlax-invoke-method
  );if
  (vlax-invoke-method (vlax-get-property *ExcelApp% "ActiveWorkbook") 'Close :vlax-False)
  (vlax-invoke-method *ExcelApp% 'Quit)
  (vlax-release-object *ExcelApp%)(gc)
  (setq *ExcelApp% nil *ExcelFile$ nil)
  (princ)
);defun CloseExcel
;-------------------------------------------------------------------------------
; ColumnRow - Returns a list of the Column and Row number
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Cell$ = Cell ID
; Syntax example: (ColumnRow "ABC987") = '(731 987)
;-------------------------------------------------------------------------------
(defun ColumnRow (Cell$ / Column$ Char$ Row#)
  (setq Column$ "")
  (while (< 64 (ascii (setq Char$ (strcase (substr Cell$ 1 1)))) 91)
    (setq Column$ (strcat Column$ Char$)
          Cell$ (substr Cell$ 2)
    );setq
  );while
  (if (and (/= Column$ "") (numberp (setq Row# (read Cell$))))
    (list (Alpha2Number Column$) Row#)
    '(1 1);default to "A1" if there's a problem
  );if
);defun ColumnRow
;-------------------------------------------------------------------------------
; Alpha2Number - Converts Alpha string into Number
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Str$ = String to convert
; Syntax example: (Alpha2Number "ABC") = 731
;-------------------------------------------------------------------------------
(defun Alpha2Number (Str$ / Num#)
  (if (= 0 (setq Num# (strlen Str$)))
    0
    (+ (* (- (ascii (strcase (substr Str$ 1 1))) 64) (expt 26 (1- Num#)))
       (Alpha2Number (substr Str$ 2))
    );+
  );if
);defun Alpha2Number
;-------------------------------------------------------------------------------
; Number2Alpha - Converts Number into Alpha string
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Num# = Number to convert
; Syntax example: (Number2Alpha 731) = "ABC"
;-------------------------------------------------------------------------------
(defun Number2Alpha (Num# / Val#)
  (if (< Num# 27)
    (chr (+ 64 Num#))
    (if (= 0 (setq Val# (rem Num# 26)))
      (strcat (Number2Alpha (1- (/ Num# 26))) "Z")
      (strcat (Number2Alpha (/ Num# 26)) (chr (+ 64 Val#)))
    );if
  );if
);defun Number2Alpha
;-------------------------------------------------------------------------------
; Cell-p - Evaluates if the argument Cell$ is a valid cell ID
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Cell$ = String of the cell ID to evaluate
; Syntax examples: (Cell-p "B12") = t, (Cell-p "BT") = nil
;-------------------------------------------------------------------------------
(defun Cell-p (Cell$)
  (and (= (type Cell$) 'STR)
    (or (= (strcase Cell$) "A1")
      (not (equal (ColumnRow Cell$) '(1 1)))
    );or
  );and
);defun Cell-p
;-------------------------------------------------------------------------------
; Row+n - Returns the cell ID located a number of rows from cell
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 2
;   Cell$ = Starting cell ID
;   Num# = Number of rows from cell
; Syntax examples: (Row+n "B12" 3) = "B15", (Row+n "B12" -3) = "B9"
;-------------------------------------------------------------------------------
(defun Row+n (Cell$ Num#)
  (setq Cell$ (ColumnRow Cell$))
  (strcat (Number2Alpha (car Cell$)) (itoa (max 1 (+ (cadr Cell$) Num#))))
);defun Row+n
;-------------------------------------------------------------------------------
; Column+n - Returns the cell ID located a number of columns from cell
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 2
;   Cell$ = Starting cell ID
;   Num# = Number of columns from cell
; Syntax examples: (Column+n "B12" 3) = "E12", (Column+n "B12" -1) = "A12"
;-------------------------------------------------------------------------------
(defun Column+n (Cell$ Num#)
  (setq Cell$ (ColumnRow Cell$))
  (strcat (Number2Alpha (max 1 (+ (car Cell$) Num#))) (itoa (cadr Cell$)))
);defun Column+n
;-------------------------------------------------------------------------------
; rtosr - Used to change a real number into a short real number string
; stripping off all trailing 0's.
; Arguments: 1
;   RealNum~ = Real number to convert to a short string real number
; Returns: ShortReal$ the short string real number value of the real number.
;-------------------------------------------------------------------------------
(defun rtosr (RealNum~ / DimZin# ShortReal$)
  (setq DimZin# (getvar "DIMZIN"))
  (setvar "DIMZIN" 8)
  (setq ShortReal$ (rtos RealNum~ 2 8))
  (setvar "DIMZIN" DimZin#)
  ShortReal$
);defun rtosr
;-------------------------------------------------------------------------------
(princ);End of GetExcel.lsp


;;--------------------------------
(defun List2String  (Alist)
  (setq NumStr (length Alist))
  (foreach Item  AList
    (if (= Item (car AList))
      ;;first item
      (setq LongString (car AList))
      (setq LongString (strcat LongString ";" Item))
      )
    )
  LongString
  ) ;defun
;;--------------------------------
(defun Dxf  (code pairs)
  (cdr (assoc code pairs))
  )
;;--------------------------------



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;string increment subfunction;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun String_ (In_Str / OutStr AddFlg DecFlg TmpLst)
  (setq OutStr
    (vl-list->string
      (foreach ForElm (reverse (vl-string->list In_Str))
        (if AddFlg
          (setq TmpLst (cons ForElm TmpLst))
          (cond
            ( (= 57 ForElm)    ;(chr 57)=> "9"
              (setq DecFlg T  TmpLst (cons 48 TmpLst))
            )
            ( (> 57 ForElm 47) ;"8"->-"0"  (chr 47)=> "/"
              (setq AddFlg T  TmpLst (cons (1+ ForElm) TmpLst))
            )
            ( (if DecFlg                          ; (chr  49)=> "1"
                (setq AddFlg T  TmpLst (cons ForElm (cons 49 TmpLst)))
                (setq TmpLst (cons ForElm TmpLst))
              )
            )
          )
        )
      )
    )
  )
  (if AddFlg OutStr (strcat OutStr "1"))
);defun




Note: the top of that routine is the full GetExcel.lsp, which shows the pointers to the excel object......the code that I've added is the second half of the file.

Bhull1985

  • Guest
Re: Change color of Excel Cell based on duplicate tag value- help!
« Reply #1 on: March 11, 2014, 09:31:59 AM »
And here is that second half:
(was too large for one post)

Code: [Select]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;PUTEXCELLS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun putexcells ( vallist / c data adata bdata fdata cell)
;;main subfunction that will process the selection set gathered by C:Putex
;;this subfunction will place tag strings into excel columns and rows
;;then will number all of the items in excel in column "A", listing only
;;a number that increments for each tag in the list.
;;the program will then put the drawing number beside the tag values within excel


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;COL "B";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;do-tag# subfunction;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;putex-->tag#-->name;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq cell (strcat "B" @excel_row))
;;starting cell value to leave one line from the top for a column header
;;subfunction (string_ cell) will increment to "A2" and then
;;"A3", incrementing each pass of the repeat loop until the end of the list.


(setq c 0)
(setq len (length vallist))
(repeat len
(setq data (nth c vallist))
(setq adata (cadr data))
(setq bdata (car data))
(if (not fdata)
(setq fdata (strcat adata "-" bdata)))
(setq cell (string_ cell))
(putcell cell fdata)
(setq fdata nil)
(setq c (1+ c))
);repeat

(if (= c len)(doname len))
;;passes number of items entered to next subroutine to enter
;;information into the next column of excel. This time it's
;;the dwg-name, which should be the same per dwg.
);defun





;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;COL "E";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;do-name subfunction;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;tag#-->name-->item;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun doname (len / c cell namedata)
(setq c 0)
(setq cell (strcat "E" @excel_row))
(setq cell (string_ cell))
(repeat len
(setq namedata (getname))
(putcell cell namedata)
(setq cell (string_ cell))
(setq c (1+ c))
);repeat
(if (= c len)(doitem len))
;;goto item subfunction
);defun doname


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;COL "A";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;do-item subfunction;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;name-->item-->type;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun doitem ( len / itemcell itemdata c)
(setq c 0)
(setq itemcell (strcat "A" @excel_row))
(setq itemcell (string_ itemcell))
(setq itemdata "00")
(repeat len
(setq itemdata (string_ itemdata))
(putcell itemcell itemdata)
(setq itemcell (string_ itemcell))
(setq c (1+ c))
);repeat
(if (= c len)(dotype len))
;;goto type subfunction
);defun doitem


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;COL "C";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;do-type subfunction;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;item-->type-->tag;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun dotype ( len / cell c data adata)
(setq c 0)
(setq cell (strcat "C" @excel_row))
(setq cell (string_ cell))
(repeat len
(setq data (nth c vallist))
(setq adata (cadr data))
(putcell cell adata)
(setq cell (string_ cell))
(setq c (1+ c))
);repeat
(if (= c len)(dotag len))
;;goto tag subfunction
);defun dotype



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;COL "D";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;do-tag subfunction;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;type-->tag-->end;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun dotag ( len / c cell data adata)
(setq c 0)
(setq cell (strcat "D" @excel_row))
(setq cell (string_ cell))
(repeat len
(setq data (nth c vallist))
(setq adata (car data))
(putcell cell adata)
(setq cell (string_ cell))
(setq c (1+ c))
);repeat
(princ "\n....Finished!")
(princ)
;;end of excel input, routine will alert to user that it is complete
);defun dotype



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;getname subfunction for col "E";;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun getname (/ dwgname tempname1 dwgname1)
;;subfunction that returns simply the last 5 characters of a dwg name before the ".dwg"
;;ex output on "213035-PIPE-PID-00000500-00.DWG" returns "00500"
;;bhull 2/27/14
(setq dwgname (getvar "dwgname"))
(setq tempname1 (substr dwgname (+ -7 (vl-string-search ".dwg" dwgname))))
(setq dwgname1 (substr tempname1 1 (- (strlen tempname1) 7)))
);defun



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;put-excel main function;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun C:putex (/)
;;custom routine to search drawing for listed tags in listed blocks
;;and write the values to an instrument index in excel as a .csv

;(openexcel @excel_file "INDEX" T)
(openexcel "Y:\\Bhull\\213035-INST-IND-00000001-00.xls" "INDEX" T)
;;one and final call to (openexcel) that opens "attributes.csv" for
;;programatically editing with this routine

(setq TagList '("TAG00" "TAG10"))
(setq ss (ssget "_X" (list (cons 0 "INSERT")(cons 2 "CE001,CE007,CE013,CE023,CE021"))))
(setq Root (getvar "DWGPREFIX"))
(setq i -1)

(repeat (sslength ss)
 (setq TagRow nil
       ValRow nil)
   (setq Edata (entget (setq e (ssname ss (setq i (1+ i))))))
   (while (/= (Dxf 0 Edata) "SEQEND")
      (if
        (and
          (= (Dxf 0 Edata) "ATTRIB")
          (member (dxf 2 Edata) TagList)
          ;;if tag is on list
           ) ;and
         (progn
           (setq TagRow (cons (Dxf 2 Edata) TagRow))
           (setq valRow (cons (Dxf 1 Edata) ValRow))
           ) ;progn
)
      (setq Edata (entget (setq e (entnext e))))
      ) ;while
(setq vallist (cons valrow vallist))
) ;repeat

(foreach sublist vallist
  (setq templist (list (vl-string-trim " " (car sublist)) (vl-string-trim " " (cadr sublist))))
  (if (not (equal templist '("" ""))); had other than space(s)-only content
    (setq revlist (cons templist revlist))
  ); if
); foreach
(setq vallist (reverse revlist))
;;code is to remove empty strings and spaces from the items within vallist


(princ)
(if vallist
(putexcells vallist)
;;command to execute next part of routine, putexcells, to put all items
;;in vallist into excel spreadsheet as .csv file
);if
(setq vallist nil)
;;THIS RESETS THE VALLIST VARIABLE TO BECOME EMPTY AT END OF ROUTINE
;;SO IT RUNS PROPERLY THE FOLLOWING TIME


);defun


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;browse-for-folder function;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun folderdia ()
;; Browse for Folder  -  Lee Mac
;; Displays a dialog prompting the user to select a folder.
;; msg - [str] message to display at top of dialog
;; dir - [str] [optional] root directory (or nil)
;; bit - [int] bit-coded flag specifying dialog display settings
;; Returns: [str] Selected folder filepath, else nil.

(defun LM:browseforfolder ( msg dir bit / err fld shl slf )
    (setq err
        (vl-catch-all-apply
            (function
                (lambda ( / app hwd )
                    (if (setq app (vlax-get-acad-object)
                              shl (vla-getinterfaceobject app "shell.application")
                              hwd (vl-catch-all-apply 'vla-get-hwnd (list app))
                              fld (vlax-invoke-method shl 'browseforfolder (if (vl-catch-all-error-p hwd) 0 hwd) msg bit dir)
                        )
                        (setq slf (vlax-get-property fld 'self)
                              @pth (vlax-get-property slf 'path)
                              @pth (vl-string-right-trim "\\" (vl-string-translate "/" "\\" @pth))
                        )
                    )
                )
            )
        )
    )
    (if slf (vlax-release-object slf))
    (if fld (vlax-release-object fld))
    (if shl (vlax-release-object shl))
    (if (vl-catch-all-error-p err)
        (prompt (vl-catch-all-error-message err))
        @pth
    )
(setq @excel_file @pth)
(princ @pth)
(princ)
)
(LM:browseforfolder "Select Excel File:" "Z:" 16384)
);defun



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;run-put-excel;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;dcl driven version of routine to allow for user selection of blocks in one dwg
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun runputexcel (ss / i sset )

(setq i -1)
(repeat (sslength ss)
 (setq TagRow nil
       ValRow nil)
   (setq Edata (entget (setq e (ssname ss (setq i (1+ i))))))
   (while (/= (Dxf 0 Edata) "SEQEND")
      (if
        (and
          (= (Dxf 0 Edata) "ATTRIB")
          (member (dxf 2 Edata) TagList)
          ;;if tag is on list
           ) ;and
         (progn
           (setq TagRow (cons (Dxf 2 Edata) TagRow))
           (setq valRow (cons (Dxf 1 Edata) ValRow))
           ) ;progn
)
      (setq Edata (entget (setq e (entnext e))))
      ) ;while
(setq vallist (cons valrow vallist))
    ) ;repeat


(if vallist
(putexcells vallist)
;;command to execute next part of routine, putexcells, to put all items
;;in vallist into excel spreadsheet as .csv file
);if
(setq vallist nil)
;;THIS RESETS THE VALLIST VARIABLE TO BECOME EMPTY AT END OF ROUTINE
;;SO IT RUNS PROPERLY THE FOLLOWING TIME
);defun






;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;DCL coding;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


 (defun c:MyToggles (/ Dcl_Id% Toggle1$ Toggle2$ Return# ptx pty)
   (princ "\nGetExcel")(princ)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Set Default Variables
(if (not *MyRadios@) ;Unique global variable name to store dialog info
    (setq *MyRadios@ (list nil "0" "0"))) ;if
(if (not @excel_row) ;start excel row
    (setq @excel_row "9")) ;if
(if (not @excel_col) ;start excel col
    (setq @excel_col "A")) ;if
(if (not @excel_file) ;start excel file name
    (setq @excel_file ;

"Y:\\bhull\\_LispBox\\_In_Works\\213035-INST-IND-00000001-00.xls"

       ));if
(setq Radio1$ (nth 1 *MyRadios@)
      Radio2$ (nth 2 *MyRadios@)
ptx "9"
pty "A")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Done Set Default Variables




   
   (setq Dcl_Id% (load_dialog "GetExcel.dcl"))
   (new_dialog "GetExcel" Dcl_Id%)

    ; Set Dialog Initial Settings
   (set_tile "Title" " GetExcel")
   (set_tile "Dir" @excel_file)
   (set_tile "Rad101" Radio1$)
   (set_tile "Rad102" Radio2$)
   (set_tile "eb1" @excel_row)
   (set_tile "eb2" @excel_col)
   (set_tile "dir" @excel_file)
   (mode_tile "eb1" 2)
   (mode_tile "eb1" 3)
   (if (= (get_tile "Rad100") "Rad101")(mode_tile "But102" 1) );if


    ; Dialog Actions
   (action_tile "Rad101" "(mode_tile \"But102\" 1)")
   (action_tile "Rad102" "(mode_tile \"But102\" 0)")
   (action_tile "eb1" "(setq ptx (atoi $value))")
   (action_tile "eb2" "(setq pty (atoi $value))")
   (action_tile "dir" "(setq @excel_file (atoi $value))")
   (action_tile "accept"
(strcat
"(progn
(setq @Excel_COL (get_tile \"eb2\"))"
"(setq @Excel_ROW (get_tile \"eb1\"))"
" (done_dialog 1))"
);strcat
);action tile
   (action_tile "cancel" "(done_dialog 0)(setq result nil)")
   (action_tile "But102" "(GET_CURRENT_STATE)(done_dialog 3)")
   (action_tile "Browse" "(folderdia)(done_dialog 5)")
   (setq Return# (start_dialog))


 


    ; Unload Dialog
   (unload_dialog Dcl_Id%)
   (setq *MyRadios@ (list nil Radio1$ Radio2$))
   (setq *start_excel@ (list nil ptx pty))
   (if (= Return# 0) (exit))
   (if (= Return# 1) (C:putex));Next
(if (= return# 4)
(progn
(princ "\nSelect Instruments")
(setq ss (ssget "_X" (list (cons 0 "INSERT"))))
(if ss
    (runputexcel ss)
    (princ "\nSelected Items are not blocks.")
);if
);progn
);if
(if (= return# 5)
(putex @excel_file)
);if



   (princ)
 );defun c:MyToggles


Bhull1985

  • Guest
Re: Change color of Excel Cell based on duplicate tag value- help!
« Reply #2 on: March 11, 2014, 09:33:56 AM »
I believe I can use the code already constructed that creates the excel object in the GetExcel.lsp first function..
Code: [Select]
  (setq ExcelFile$ (findfile ExcelFile$))
  (setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))
  (vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Open ExcelFile$)
  (if SheetName$
    (vlax-for Worksheet (vlax-get-property *ExcelApp% "Sheets")
      (if (= (vlax-get-property Worksheet "Name") SheetName$)
        (vlax-invoke-method Worksheet "Activate")
      );if
    );vlax-for
  );if

but i'd need to know how to create a subfunction that I can simply tell the column/row and color in order to change the color for that cell. So that I can call it upon finding duplicate tags, once I work out the logic to do that......

Bhull1985

  • Guest
Re: Change color of Excel Cell based on duplicate tag value- help!
« Reply #3 on: March 11, 2014, 10:43:24 AM »
Okay. Got it via
Code: [Select]
(vl-load-com)


(DEFUN PutColor (RNG COL)
  (VLAX-PUT-PROPERTY
    (VLAX-GET-PROPERTY
      (VLAX-GET-PROPERTY (VLAX-GET-OR-CREATE-OBJECT "Excel.Application") "Range" RNG)
      "Interior"
    )
    "Colorindex"
    (VLAX-MAKE-VARIANT COL)
  )
)
Thanks!

danallen

  • Guest
Re: Change color of Excel Cell based on duplicate tag value- help!
« Reply #4 on: March 11, 2014, 11:55:00 AM »
but i'd need to know how to create a subfunction that I can simply tell the column/row and color in order to change the color for that cell. So that I can call it upon finding duplicate tags, once I work out the logic to do that......

can you create a conditional format in excel that will highlight the duplicate values for you? lots of examples in the net for that, but I have no idea how you would define and apply the format programatically