OLEオートメーションの使用例

Title
OLEオートメーションの使用例
Type
Tips
Section
その他
; 意味もなく全部のシートに「東西南北」を書き込む
(setq application (ole-create-object "Excel.Application"))
(ole-putprop application 'visible 1)
(setq workbook (ole-method (ole-getprop application 'Workbooks) 'Add))
(setq numbers-of-worksheets
      (ole-getprop
       (ole-getprop workbook 'worksheets)
       'count))
(setq worksheet-index 1)
(while (<= worksheet-index numbers-of-worksheets)
  (setq worksheet
 (ole-getprop workbook 'Worksheets worksheet-index))
  (ole-putprop (ole-method worksheet 'Range "A1:D1")
        'value #("東" "西" "南" "北"))
  (setq worksheet-index (+ worksheet-index 1)))


こんな小細工をしてみたんですけどどうでしょう?

(defmacro $ (obj prop &rest args)
  `(ole-method ,obj ',prop ,@args))

(defsetf $ (obj prop &rest args) (x)
  `(progn
     (ole-putprop ,obj ',prop ,x ,@args)
     ,x))

(let (app workbook worksheet range)
  (setq app (ole-create-object "Excel.Application"))
  (setf ($ app Visible) t)
  (setq workbook ($ ($ app Workbooks) Add))
  (setq worksheet ($ workbook Worksheets 1))
  (setf ($ ($ worksheet Range "A1:D1") Value) '("North" "South" "East" "West"))
  (setf ($ ($ worksheet Range "A2:B2") Value) #(5.2 10))
  (setf ($ ($ worksheet Range "C2") Value) 8)
  (setf ($ ($ worksheet Range "D2") Value) 20)

  (setq range ($ worksheet Range "A1:D2"))
  ($ range Select)
  ($ ($ workbook Charts) Add)
  (sit-for 5)

  (setf ($ workbook saved) t)
  ($ ($ app ActiveWorkbook) Close 0)
  ($ app Quit))