Jump to content

マクロ/Writer/002

From The Document Foundation Wiki
This page is a translated version of the page Macros/Writer/002 and the translation is 100% complete.


説明

編集 ▸ Links メニューを使用すると、次のようなドキュメント内のリンクを認識できます。

  • ファイルソース( URL)
  • 要素 (範囲)
  • タイプ (soffice)
  • 更新 (自動/手動)

一方、この方法でもナビゲーターでも、関連するテーブルがどれなのか(文書内の 名前位置)を知ることはできません。

次のマクロの目的は、DDEリンクによって作成されたテーブルを検索することです。.[1]

このマクロは、テーブルの最初のセルに、次の内容からなるコメントを挿入します。

  • ソース(CalcスプレッドシートのURLなど)
  • ソースで使用されている範囲。

(マクロによるコメント操作の詳細については、Macros/Writer/001を参照してください。)

コレクション TextFieldMasters は、編集 ▸ Links メニューによって表示された情報のみを取得し、TextTables はそれ以上の情報を提供しないことに注意してください。

テーブルデータのDDE接続は、<office:dde-source>要素に含まれています。[2].

したがって、content.xmlファイルを参照して、そのような要素を検索できます。プログラムは、現在のドキュメントにこのファイルがあることを確認することから開始し、それから読み取りに進みます。

このために、リスナー com.sun.star.xml.sax.XDocumentHandler[3] に関連付けられたサービス com.sun.star.xml.sax.Parser[4] を使用します。

Code

LibreOffice Basicの場合:

Option Explicit

' This library is Copyright (C) 2012 Pierre-Yves Samyn, except otherwise indicated
' Purpose: Frame the selection or create an empty frame if no selection

' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
' You should have received a copy of the GNU General Public License
' along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
Dim oDoc As Object
Dim oDocHandler As Object
Dim oTableName As String


' マクロの説明:

' Context    :  LibreOffice Writer odtドキュメント用
' Objective  :  DDEリンクテーブルを識別する
' Function   :  ソースのコメント(例:スプレッドシートのURL)と範囲を挿入します。

' APIを介したUNOのXML saxパーサーの使用に関する情報:
' http://www.oooforum.org/forum/viewtopic.phtml?t=4907

Sub Comment_DDE_Table()

    Dim oContent As Object
    Dim oFlux As Object
    Dim oSaxParser As Object
    Dim oDocEventsHandler As Object
    Dim oInputSource As Object

    oDoc = ThisComponent

    ' ドキュメントが TextDocumentの場合にのみマクロを実行します
    If oDoc.supportsService("com.sun.star.text.TextDocument") Or _
        oDoc.supportsService("com.sun.star.text.GlobalDocument") Then

        ' content.xmlファイルを読み込みたいので、ThisComponentがそのようなファイルを持つオブジェクトを参照していることを確認します。
        If oDoc.DocumentStorage.HasByName("content.xml") Then
        
            ' ドキュメントが変更されているかどうかをテストします: content.xmlファイルを読み込むため、
            ' 最新のコンテンツで作業できるように保存を求めます。
            If oDoc.ismodified Then
                MsgBox "ドキュメントを最新バージョンで保存する必要があります!(Ctrl+S)", 64, "Comment_DDE_Table()"
            Else 
                oContent = oDoc.DocumentStorage.GetByName("content.xml")
                oFlux = oContent.GetInputStream()   
                        
                ' XML Saxパーサーの作成します
                oSaxParser = createUnoService( "com.sun.star.xml.sax.Parser" )
                
                ' イベントマネージャを作成します。
                ' 次に示す名称は、以下に従った内容を反映しています
                ' http://api.libreoffice.org/common/ref/com/sun/star/xml/sax/XDocumentHandler.html
                oDocHandler = CreateUnoListener( "DocHandler_", "com.sun.star.xml.sax.XDocumentHandler" )
                oSaxParser.setDocumentHandler( oDocHandler )
                   
                ' 以下に従った構造の作成します:
                ' http://api.libreoffice.org/common/ref/com/sun/star/xml/sax/InputSource.html

                oInputSource = createUnoStruct( "com.sun.star.xml.sax.InputSource" )
                oInputSource.aInputStream = oFlux
        
                ' parserにcontent.xmlの内容を読み込むように指示します
                ' 関数(DocHandler_)は、イベントに従って呼び出されます。
                oSaxParser.parseStream( oInputSource )
                oFlux.closeInput()
                        
                MsgBox "処理の終了" , 64, "Comment_DDE_Table()"
            End If
        Else
            MsgBox "このマクロはこのタイプのドキュメントでは機能しません(odt Writerである必要があります)" , 64, "Comment_DDE_Table()"
        End If

        Else
        MsgBox "このマクロはこのタイプのドキュメントでは機能しません(odt Writerである必要があります)" , 64, "Comment_DDE_Table()"
    End If

End Sub ' Comment_DDE_Table


' 以下に、イベントに対応するリスナーの機能を示します。
' http://api.libreoffice.org/common/ref/com/sun/star/xml/sax/XDocumentHandler.html

Sub DocHandler_startDocument()
End Sub

Sub DocHandler_endDocument()
End Sub

Sub DocHandler_startElement( cName As String, oAttributes As com.sun.star.xml.sax.XAttributeList )

    Dim oTable As Object
    Dim oCursor As Object
    Dim oComment As Object

    Dim oDateHour As New com.sun.star.util.DateTime

    ' 要素がテーブルの場合、その名前を格納します
    If cName = "table:table" Then
        oTableName = oAttributes.getValueByName("table:name")
    End If   

    ' OASIS Open Document Format for Office Applicationsを参照
:
    ' http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os.html
    ' テーブルのDDEデータ接続は、<office:dde-source> 要素に含まれます
    ' この要素の使用方法は、スプレッドシートと文書ドキュメントの表では異なります 
    ' 文書ドキュメントの表の場合、要素は表の<table:table>要素内に
    ' 直接含まれます
    ' このため、テーブルがDDEリンクかどうかをテストします。
    If cName = "office:dde-source" Then

        ' テーブルにアクセスします:
        oTable = oDoc.TextTables.getByName(oTableName)

        ' テーブルの最初のセルにカーソルを作成します:
        oCursor = oTable.getCellByPosition(0, 0).createTextCursor

        ' コメントを作成します:
        oComment = oDoc.createInstance("com.sun.star.text.textfield.Annotation")

        ' 現在のタイムスタンプを取得します:
        With oDateHour
            .Minutes = minute(now)
            .Hours = hour(now)
            .Day = day(now)
            .Month = month(now)
            .Year = year(now)
        End With
         
        ' コメントにDDEソースと範囲を入力する
        With oComment
            .Author = "Source: DDE link"
            .Content = oAttributes.getValueByName("office:dde-item") & chr(13) &_
                oAttributes.getValueByName("office:dde-topic")
            .DateTimeValue = oDateHour
        End With

        ' セルにコメントを挿入する
        oComment.attach(oCursor.start)
    End If

End Sub ' DocHandler_startElement


Sub DocHandler_endElement( cName As String )
End Sub

Sub DocHandler_characters( cChars As String )
End Sub

Sub DocHandler_ignorableWhitespace( cWhitespace As String )
End Sub

Sub DocHandler_processingInstruction( cTarget As String, cData As String )
End Sub

Sub DocHandler_setDocumentLocator( oLocator As com.sun.star.xml.sax.XLocator )
End Sub

Notes

  1. DDE: "Dynamic Data Exchange", allows you to link objects by reference to the file, without embedding them.
  2. See the standard OASIS Open Document Format for Office Applications. The use of this element differs between spreadsheet and word processing. For text documents, the element is contained directly inside the element <table:table>.
  3. See the API documentation for com.sun.star.xml.sax.XDocumentHandler
  4. See the API documentation for com.sun.star.xml.sax.Parser