2010年1月6日 星期三

學習記錄:OpenOffice Basic (2)

壹、基本認識

在學習 OOoBasic 前,必須了解幾點:

  1. OOoBasic 是建基於 UNO ,而 UNO 是服務於 OOo ,因此 UNO 具有各式各樣的 Moudle 。
  2. UNO 仍在發展中,許多高手就自己的需要不斷修改 Moudle、加入新 Moudle 。
  3. 為了兼容,許多舊  Moudle 仍然被保留著,使得 UNO 不只內容龐大,而且開始重疊、繁瑣。
  4. 為了兼容 VBA ,UNO 的不同 Moudle 又添加了許多功能,當中有些重覆。

當參考網上的範例及說明時,同一功能卻可以用不同的  Moudle 寫出來,看起來很混亂,其實這也是 Open Source 的優點,只要有需要,便可以自己動手加入新功能。結論是:掌握要用的,就夠了。

好了,開始進行正題。

貳、UNO 的物件(Object)

 OOo 的所有物件(Object)都是基於 UNO 的 com.sun.star.sheet.SpreadsheetDocument 服務,供所有程式使用。這樣寫法的好處是統一,但字句太長了。

參閱


三、OOoBasic 的物件(Object)

要注意的是 OOoBasic 是內建的,因此在打開 OpenOffice時,它自動提供另一個 StarDesktop 的全域性(globally)服務。例如:StarDesktop.CurrentComponent

參閱
http://wiki.services.openoffice.org/wiki/ZH/Documentation/BASIC_Guide/StarDesktop

四、StarDesktop

StarDesktop 是 com.sun.star.sheet.SpreadsheetDocument 的簡化版,主要供其它語言調用  OOo 。但當用在Calc「目前使用中的工作表 (ActiveSheet) 」時,StarDesktop 又太笨重了,OOoBasic 又提供了 ThisComponent 模組,例如使用 ThisComponent.CurrentController 時,可直接指令至目前使用中的 Workbook 、Worksheet、cell 等。

以下的一段話正好說明 OOoBasic 的多樣性

StarDesktop and thisComponent are OpenOffice.org Basic commands that refer to the application and currently active document respectively.

Unlike Microsoft Office, OpenOffice.org is one application with different components. When running some code it maybe useful to check which component is currently active. The above code demonstrates how that checking process can be done.

The Desktop that StarDesktop refers to is conceptual (historically it actually existed) and can be thought of as the OpenOffice.org application.

五、小結

這裏要分辨 OOoBasic 與 VBA 用語上的差異。

VBA              oooBasic
---------------------------------------------------------------------------------------------------------------------
Workbook         SpreadsheetDocument
Worksheet           Sheet
Application.ActiveWorkbook    ThisComponent (StarDesktop.CurrentComponent)


了解 SpreadsheetDocument 及 Sheet 的基本結構後,可以進一步看看以下的分別。

Cells             represents a collection of used cells in a spreadsheet document.
SheetCell     represents a single addressable cell in a spreadsheet document.

Spreadsheet     represents a complete spreadsheet in a spreadsheet document.
SpreadsheetDocument     represents a model component which consists of some settings and one or more spreadsheets.

http://api.openoffice.org/docs/common/ref/com/sun/star/table/XCell.html

六、小例子

接著是一個例子作例證,它有著 OOoBasic 與 VBA 的語句。

Sub Test

AAA=ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("B"&"1").Value

msgbox AAA*3


End Sub

七、努力方向

最後,以下另一個起點,學會了這一頁也差不多了。
http://wiki.services.openoffice.org/wiki/Spreadsheet_common

 

沒有留言: