AutoHotkey的Excel.Application操作重點

  • 網頁自動化(使用Selenium WebDriver)擷取網頁內容並新增至Excel工作表

  • 建立COM物件

; 建立新的Excel應用程式物件
excelApp := ComObjCreate("Excel.Application") 

; 建立已有的Excel應用程式物件(Excel已開啟)
excelApp := ComObjActive("Excel.Application") ;creates a handle to your currently active excel sheet
  • 開啟活頁簿(Workbook)
if FileExist(sFilename) {  ; 開啟已存在的活頁簿
  workBook := excelApp.Workbooks.Open(sFilename)   ;opens the existing Excel table
} else {
  isNew := True
  excelApp.Workbooks.Add  ; 新增活頁簿
}
excelApp.Visible := True  ; 執行時顯示Excel內容
  • 儲存格操作
excelApp.Range("A1").Value := "第一格"
workBook.ActiveSheet.Range("B1").Value := "第二格"
  • 儲存格屬性
excelApp.Range("A1").Font.Bold := 1  ; 1=True
excelApp.Range("A1").Font.ColorIndex := 3  ; 3=Red
excelApp.Range("A1").Interior.ColorIndex := 35  ; 背景色
; -4131=Left, -4108=Center, -4152=Right
excelApp.Range("A1").HorizontalAlignment := -4108
excelApp.Range("A1").NumberFormat := "#,##0.00"
excelApp.Range("A3").Formula := "=SUM(A1:A2)"
  • 範圍操作
excelApp.ActiveSheet.Columns("A").ColumnWidth := 20
excelApp.Range("A:Z").Clear()  ; 清除格式與內容
excelApp.Range("A:Z").ClearContents()  ; 清除內容

ColorIndex 屬性 (Excel 圖表)

  • 迴圈一列
Row := "1"
Columns := Object(1,"A",2,"B",3,"C",4,"D",5,"E",6,"F",7,"G",8,"H",9,"I",10,"J",11,"K",12,"L",13,"M",14,"N",15,"O",16,"P",17,"Q") ;array of column letters
For Key, Value In Columns
  XL.Range(Value . Row).Value := value ;set values of each cell in a row
  • 常用物件
物件 功用
Workbooks 活頁簿集合
Worksheets 所有工作表集合
Worksheets("工作表1") 指名的工作表
ActiveWorkbook 作用中的活頁簿
Sheets(n) 第n張工作表
ActiveSheet 作用中的工作表
Columns("c1:c2") c1至c2直欄
Rows("r1:r2") r1至r2橫列
Range("x1:x2") x1至x2的儲存格
Cells(r, c) 第r橫列、第c直欄的儲存格
ActiveCell 目前的儲存格
Selection 目前所選取的物件

相關連結

腳本程式原始碼

  • excel1.ahk

  • excel2.ahk

解說影片

<

p>##

您可能也會有興趣的類似文章

簡睿

服務於軟體業的資訊老兵。興趣廣泛,學習力佳,樂於分享所知所學。

您可能也會喜歡…

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *