2010年1月8日 星期五

學習記錄:OpenOffice Basic (3)

  
Calc basic 中的 StarDesktop、StarDesktop.CurrentComponent 與 ThisComponent ,這三個名詞,一直以為懂了,但一寫程式,就糊塗。用了兩天,又好像明白了。決定記下來,希望不會錯得太離譜。

一、StarDesktop 是什麼?

  1. SUN 開發 StarOffice 時,以 UNO 作為接口,供不同程式語言使用。開源後,ooO 亦繼承這一特徵。

  2. 統一的 UNO 接口很有遠見。問題是大部份 ooO 用家都只是打打字,計些加減數,進階的只是錄製些 Marco 來修改。UNO 的設計變得太複雜太笨重。例如,要求開新檔案時,頁首加上公司LOGO 及今天時間,我要從 UNO 的 com.sun.star 來找 API ,甚至要懂 C++、JAVA 才寫出來,太扯吧。

  3. 為了與 Ms Office 競爭,ooO Basic 推出了一個 StarDesktop 的 「捷徑」物件來簡化程式。StarDesktop 集合了常用的 API ,當中有:

          com.sun.star.frame.Desktop
            建立、開啟或匯入文件
      
          com.sun.star.document.OfficeDocument
            儲存、匯出或列印文件。

    明白了,假如沒有 StarDesktop ,程式要開一個檔或儲存,必須分別使用 .frame. 及 .document.,又繁雜又難記了。

    就我了解,StarDesktop = com.sun.star.frame.Desktop。屬於全域全功能的 API 。


    StarDesktop 伴隨著 ooO 啟動,以 com.sun.star.frame.XComponentLoader 做介面,隨時呼叫與 ooO 有關的 API。因此,記一個 StarDesktop.xx.yy 就好。電腦的工作多了,但人就輕鬆了,真係以人為本。

     
    二、StarDesktop 應用例子:


    Dim Doc As Object
    Dim Url As String
    Dim Dummy()    'An (empty) array of PropertyValues

      Url = "file:///C:/test.odt"
      Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy)


    三、StarDesktop.CurrentComponent 是什麼?

    1. 它是對應於 VBA 的 Application.ActiveWorkbook 功能。
    2. StarDesktop.xx.yy 的服務,是服務所有 ooO 元件(Writee / Calc / Draw / Impress / Base)。它提供共同的建立、開啟、匯入文件、儲存、匯出或列印文件。

    3. StarDesktop.CurrentComponent 很好用,問題是它設計原意是供其它語言從外面連入ooO 。StarDesktop.CurrentComponent.XX 完成工作後,它會自動回到原來執行前的 Document。但當我們用 ooO 內置 IDE 跑 Macro 後,只會返回 IDE,這時 StarDesktop 會「報錯」及中斷,造成無法測試。

    4. 另外有不少人反映 StarDesktop.CurrentComponent 並不穩定,存在問題。

    四、ThisComponent 是什麼?

    1. ThisComponent = StarDesktop ,差別只是 ThisComponent 只能對應現時的 Document。

    2. ThisComponent 與 StarDesktop.CurrentComponent 大部份內容相同。

    3. 當使用 Basic IDE 測試時,ThisComponent 完成後會返回 IDE ,不會如 StarDesktop.CurrentComponent 「報錯」。

    五、整合的一個例子:

    參考自:Accessing the UNO API


    Sub Main
       MsgBox StarDesktop.Dbg_SupportedInterfaces
        ' is the same as
       Dim oDesktop
       oDesktop = CreateUnoService( "com.sun.star.frame.Desktop" )
       MsgBox oDesktop.Dbg_SupportedInterfaces
       ' is the same as
       MsgBox ThisComponent.Dbg_SupportedInterfaces
    End Sub















    六、 小結

    一句話,三者都用來「建立、開啟、匯入文件、儲存、匯出或列印文件。
      
     
      

    沒有留言: