2009-08-02

在命令列呼叫 OpenOffice.Org Basic 巨集

這不只可以用 Tcl/Tk 來做到,只是我最習慣 Tcl/Tk 來做這些事情。

OpenOffice.Org Basic 寫的程式:
REM ***** BASIC *****

Option Explicit

Sub AutoFindGoodMan(cFile)
Dim Doc As Object
Dim Dummy()

Dim Sheet As Object
Dim Cell As Object
Dim Cell2 As Object
Dim Cell3 As Object
Dim Cell4 As Object
Dim Count As Integer

Doc = StarDesktop.loadComponentFromURL(ConvertToUrl(cFile), "_default", 0, Dummy())

Sheet = Doc.Sheets(0)

dim document as object
dim dispatcher as object
rem ---------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(2) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Zoom.Value"
args1(0).Value = 100
args1(1).Name = "Zoom.ValueSet"
args1(1).Value = 28703
args1(2).Name = "Zoom.Type"
args1(2).Value = 0
dispatcher.executeDispatch(document, ".uno:Zoom", "", 0, args1())

For Count = 66 To 841 Step 1
Cell = Sheet.getCellByPosition(0, Count)
IF Cell.Value > 1000 Then
Cell.CellBackColor = RGB(0, 0, 255)
Cell2 = Sheet.getCellByPosition(3, Count)
Cell3 = Sheet.getCellByPosition(5, Count)
Cell4 = Sheet.getCellByPosition(7, Count)

IF Cell3.Type = com.sun.star.table.CellContentType.EMPTY Then
IF Cell4.Value = 0.00 Then
Cell3.CellBackColor = RGB(255, 0, 0)
Cell4.CellBackColor = RGB(255, 0, 0)
ENDIF
ELSE
IF Cell3.Value < 9 AND Cell4.Value > 5 Then
IF Cell2.Value <= 15 Then
Cell2.CellBackColor = RGB(255, 255, 0)
ELSEIF Cell2.Value > 15 AND Cell2.Value <= 40 THEN
Cell2.CellBackColor = RGB(0, 255, 0)
ELSEIF Cell2.Value > 40 THEN
Cell2.CellBackColor = RGB(0, 255, 255)
END IF

Cell3.CellBackColor = RGB(0, 255, 0)
Cell4.CellBackColor = RGB(0, 255, 0)
ELSEIF Cell3.Value >= 80 Then
Cell3.CellBackColor = RGB(0, 255, 255)
End IF
End IF
End IF
Next Count

If (Doc.isModified) Then
If (Doc.hasLocation And (Not Doc.isReadOnly)) Then
Doc.store()
End If
End If


Doc.close(True)

End Sub

而可以用 Tcl/Tk 執行 soffice.exe,來幫我們自動執行這件事:

set DIR [pwd]
set DOCS [glob *.xls]

foreach file $DOCS {
set inputFile $DIR
append inputFile "/" $file
set exeList [list "C:\\Program Files\\OpenOffice.org 3\\program\\soffice.exe" "macro:///Standard.Module1.AutoFindGoodMan($inputFile)"]
exec {*}$exeList
}


但是我還沒有摸索出來在執行的時候不要顯示出來 OpenOffice.Org 文件視窗的方法,不論是使用命令列指定,或者在執行時設定的方式,都是沒有效果的(有可能是因為我沒有設定正確吧……)。