Author Topic: Excel Scripting - Color Conversion  (Read 1754 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Swamp Rat
  • Posts: 812
Excel Scripting - Color Conversion
« on: January 02, 2018, 11:28:58 AM »
Hey guys,
First of all - Happy new 2018!
I've decided to start-off that year with some more excel scripting practice (more specifically dealing with colors).
I was very surprised when I saw that the color pallete in Excel is not the same as in AutoCAD -
Say prompt for an ACI color from AutoCAD and apply it to some cell in excel:

Code - Auto/Visual Lisp: [Select]
  1. (defun C:test ( / *error* clr xlapp xlwbs xlwbk xlsht xlcells xlrng xlint msg )
  2.  
  3.   (defun *error* ( m )
  4.     ; (and xlapp (vl-catch-all-apply 'vlax-invoke-method (list xlapp 'quit)))
  5.     (foreach o (reverse (list clr xlapp xlwbs xlwbk xlsht xlcells xlrng xlint msg)) (and (eq 'VLA-OBJECT (type o)) (vl-catch-all-apply 'vlax-release-object (list o))) )
  6.     (gc) (and m (princ m)) (princ)
  7.   ); defun *error*
  8.  
  9.   (cond
  10.     ( (not (setq clr (acad_colordlg 1 nil))) )
  11.     ( (not (setq xlapp (vlax-get-or-create-object "Excel.Application"))) (prompt "\nUnable to interfere with Excel application.") )
  12.     (
  13.         (setq msg
  14.           (vl-catch-all-apply
  15.             (function
  16.               (lambda ( / )
  17.                 (setq xlwbs (vlax-get-property xlapp 'Workbooks))
  18.                 (setq xlwbk (vlax-invoke-method xlwbs 'Add))
  19.                 (setq xlsht (vlax-get-property  xlapp 'ActiveSheet))
  20.                 (setq xlcells (vlax-get-property xlsht 'Cells))
  21.                 (setq xlrng (vlax-variant-value (vlax-get-property xlcells 'Item 1 1)))
  22.                 (setq xlint (vlax-get-property xlrng 'Interior))
  23.                 (vl-catch-all-apply 'vlax-put-property (list xlint 'ColorIndex (vlax-make-variant clr vlax-vbInteger))) ; vlax-vbLong - no difference
  24.                 (vlax-put-property xlapp 'Visible :vlax-true)
  25.               ); lambda
  26.             ); function
  27.           ); vl-catch-all-apply
  28.         ); setq msg
  29.       ); vl-catch-all-error-p
  30.       (prompt (strcat "\nError: " (vl-catch-all-error-message msg)))
  31.     )
  32.   ); cond
  33.   (*error* nil) (princ)
  34. ); defun C:test

So does anyone know what type of color Excel uses? And how to convert from ACI/RGB to match it?  :rolleyes2:




With VBA that color manipulation task looks very easy (but I guess that it might have the same ColorIndex issue) :
Code: [Select]
Range("A1").Interior.ColorIndex = 37

Range("A1").Interior.ColorIndex = 0

MsgBox Selection.Interior.ColorIndex

Range("A1").Interior.Color = RGB(255, 0, 0)


BTW the Interior object supports Color property aswell - not sure which one would be the right to use, just leaving this for reference:

Code: [Select]
; Interior: nil
; Property values:
;   Application (RO) = #<VLA-OBJECT _Application 0000007c535314b8>
;   Color = 1.67772e+07
;   ColorIndex = -4142
;   Creator (RO) = 1480803660
;   Gradient (RO) = nil
;   InvertIfNegative = Exception occurred
;   Parent (RO) = #<VLA-OBJECT Range 0000007c4bf42688>
;   Pattern = -4142
;   PatternColor = 0
;   PatternColorIndex = -4142
;   PatternThemeColor = -4142
;   PatternTintAndShade = 0.0
;   ThemeColor = -4142
;   TintAndShade = 0.0
; No methods
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Excel Scripting - Color Conversion
« Reply #1 on: January 02, 2018, 12:50:35 PM »
...well the true-color way kinda works, but for some values there is an expection (visual judgement):
Code - Auto/Visual Lisp: [Select]
  1. ; Example for true color prompt:
  2. (defun C:test ( / *error* tmp clr xlapp xlwbs xlwbk xlsht xlcells xlrng xlint msg )
  3.  
  4.   (defun *error* ( m )
  5.     ; (and xlapp (vl-catch-all-apply 'vlax-invoke-method (list xlapp 'quit)))
  6.     (foreach o (reverse (list clr xlapp xlwbs xlwbk xlsht xlcells xlrng xlint msg)) (and (eq 'VLA-OBJECT (type o)) (vl-catch-all-apply 'vlax-release-object (list o))) )
  7.     (gc) (and m (princ m)) (princ)
  8.   ); defun *error*
  9.  
  10.   (cond
  11.     ( (not (setq clr (cdr (assoc 420 (acad_truecolordlg 1))))) )
  12.     ; ( (not (setq clr (acad_truecolordlg 1))) )
  13.     ; ( (progn (setq clr (cond ((setq tmp (cdr (assoc 420 clr))) tmp) ((LM:ACI->TRUE (cdr (assoc 62 clr)))) )) nil) )
  14.     ( (not (setq xlapp (vlax-get-or-create-object "Excel.Application"))) (prompt "\nUnable to interfere with Excel application.") )
  15.     (
  16.         (setq msg
  17.           (vl-catch-all-apply
  18.             (function
  19.               ; (
  20.               (lambda ( / )
  21.                 (setq xlwbs (vlax-get-property xlapp 'Workbooks))
  22.                 (setq xlwbk (vlax-invoke-method xlwbs 'Add))
  23.                 (setq xlsht (vlax-get-property  xlapp 'ActiveSheet))
  24.                 (setq xlcells (vlax-get-property xlsht 'Cells))
  25.                 (setq xlrng (vlax-variant-value (vlax-get-property xlcells 'Item 1 1)))
  26.                 (setq xlint (vlax-get-property xlrng 'Interior))
  27.                 (vl-catch-all-apply 'vlax-put-property (list xlint 'Color (vlax-make-variant clr vlax-vbLong))) ; vlax-vbLong
  28.                 ; (vlax-dump-object xlrng t)
  29.                 ; (vlax-dump-object xlint t)
  30.                 (vlax-put-property xlapp 'Visible :vlax-true)
  31.               ); lambda
  32.               ; )
  33.             ); function
  34.           ); vl-catch-all-apply
  35.         ); setq msg
  36.       ); vl-catch-all-error-p
  37.       (prompt (strcat "\nError: " (vl-catch-all-error-message msg)))
  38.     )
  39.   ); cond
  40.   (*error* nil) (princ)
  41. ); defun C:test

As for the indexcolor, found this color table chart from here:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Excel Scripting - Color Conversion
« Reply #2 on: January 02, 2018, 02:16:35 PM »
Hint: Use my LM:rgb->ole function from here.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Excel Scripting - Color Conversion
« Reply #3 on: January 02, 2018, 02:47:13 PM »
Hint: Use my LM:rgb->ole function from here.

So it was OLE type afterall... worked like a charm, with quick changes:
Code: [Select]
(cond
    ( (not (setq clr (acad_truecolordlg 1))) )
    ( (progn (setq clr (cond ((setq tmp (cdr (assoc 420 clr))) (LM:True->OLE tmp))((LM:ACI->OLE (cdr (assoc 62 clr)))))) nil) )
    ...
    (vl-catch-all-apply 'vlax-put-property (list xlint 'Color (vlax-make-variant clr vlax-vbLong))) ; vlax-vbLong
    ...
Thank you very much, Lee!  :yay!:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg