Macros/Writer/002

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


    Other languages:

    Beschrijving

    Met Bewerken ▸ Koppelingen naar externe bestanden... vind je de in het Calc-document aanwezige koppelingen, de details zijn:

    • Bronbestand (URL)
    • Element (HTML-tables)
    • Type (Document)
    • Bijwerken (Automatisch/Handmatig)

    Je ziet op deze manier (en ook niet met de Navigator) wat dan die tabel is.

    Het doel van de macro is om de HTML-table te vinden die door de DDE koppeling wordt aangemaakt.[1]

    De macro gebruikt de eerste cel van een tabel om een notitie toe te voegen, die notitie bevat:

    • bron (URL van bijvoorbeeld een Calc-document)
    • het gebruikte bereik in de bron

    (Meer over gebruik notities in macro's.)

    Met de verzameling TextFieldMasters zouden we alleen de informatie ophalen die we al weten ( via Bewerken ▸ Koppelingen naar externe bestanden... en TextTables), dus daar hebben we weinig aan.

    De DDE connectie van de gegevens in de tabel staat in een element <office:dde-source>[2].

    We kunnen dus bladeren naar het bestand content.xml en naar die elementen zoeken. De macro kijkt eerst even of het huidige document zo'n bestand bevat en leest het daarna.

    Hiervoor wordt gebruik gemaakt van de service com.sun.star.xml.sax.Parser[3] geassocieerd met de listener com.sun.star.xml.sax.XDocumentHandler[4].

    Code

    In 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
    
    
    ' Beschrijving van de macro:
    
    ' Context    :  Voor LibreOffice Writer odt document
    ' Objective  :  Achterhalen van de DDE-linked tables
    ' Function   :  Toevoegen van een notitie over de bron en het bereik
    
    ' Informatie over gebruik van UNO XML sax parser via de API:
    ' 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
    
        ' Macro alleen uitvoeren als het een tekstdocument is
        If oDoc.supportsService("com.sun.star.text.TextDocument") Or _
            oDoc.supportsService("com.sun.star.text.GlobalDocument") Then
    
            ' Lees het bestand content.xml, maakt ThisComponent een referentie naar een object met dat bestand
            If oDoc.DocumentStorage.HasByName("content.xml") Then
            
                ' Kijk of het bestand nog in bewerking is, dan kunnen we beter vragen om het even op te slaan zodat we de meeste recente gegevens gebruiken als we content.xml lezen.
                If oDoc.ismodified Then
                    MsgBox "Sla het document eerst op, zodat de macro actuele gegevens verwerkt! (Ctrl+S)", 64, "Comment_DDE_Table()"
                Else 
                    oContent = oDoc.DocumentStorage.GetByName("content.xml")
                    oFlux = oContent.GetInputStream()   
                            
                    ' Aanmaken XML Sax parser
                    oSaxParser = createUnoService( "com.sun.star.xml.sax.Parser" )
                    
                    ' Aanmaken event manager.
                    ' De onderstaande namen gegeven de inhoud aan volgens de
                    ' 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 )
                       
                    ' Aanmaken van structure volgens:
                    ' http://api.libreoffice.org/common/ref/com/sun/star/xml/sax/InputSource.html
    
                    oInputSource = createUnoStruct( "com.sun.star.xml.sax.InputSource" )
                    oInputSource.aInputStream = oFlux
            
                    ' Laat de parser het bestand content.xml lezen
                    ' De functies (DocHandler_) worden aangeroepen overeenstemmend met de gebeurtenis.
                    oSaxParser.parseStream( oInputSource )
                    oFlux.closeInput()
                            
                    MsgBox "Onderzoek afgerond." , 64, "Comment_DDE_Table()"
                End If
            Else
                MsgBox "Deze macro is alleen bedoeld voor Writer-documenten (odt)" , 64, "Comment_DDE_Table()"
            End If
    
            Else
            MsgBox "Deze macro is alleen bedoeld voor Writer-documenten (odt)" , 64, "Comment_DDE_Table()"
        End If
    
    End Sub ' Comment_DDE_Table
    
    
    ' De functies van de listener, overeenkomend met de gebeurtenissen:
    ' 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
    
        ' Als het element een table is, bewaar de naam
        If cName = "table:table" Then
            oTableName = oAttributes.getValueByName("table:name")
        End If   
    
        ' Lees: OASIS Open Document Format for Office Applications
        ' http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os.html
        ' The DDE data connection of tables is contained in an <office:dde-source> element.
        ' The usage of this element differs between spreadsheet and text document tables. 
        ' For text document tables, the element is contained within the table's <table:table> 
        ' element directly.
        ' Hence test if the table is DDE-linked:
        If cName = "office:dde-source" Then
    
            ' Benader de table:
            oTable = oDoc.TextTables.getByName(oTableName)
    
            ' Maak een cursor in de eerste cel van de table:
            oCursor = oTable.getCellByPosition(0, 0).createTextCursor
    
            ' Maak een notitie:
            oComment = oDoc.createInstance("com.sun.star.text.textfield.Annotation")
    
            ' Tijdstempel maken:
            With oDateHour
                .Minutes = minute(now)
                .Hours = hour(now)
                .Day = day(now)
                .Month = month(now)
                .Year = year(now)
            End With
             
            ' Stel de notitie samen
            With oComment
                .Author = "Source: DDE link"
                .Content = oAttributes.getValueByName("office:dde-item") & chr(13) &_
                    oAttributes.getValueByName("office:dde-topic")
                .DateTimeValue = oDateHour
            End With
    
            ' Zet de notitie in de cel
            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", het kunnen linken naar een object bij referentie, zonder ze te embedden.
    2. Zie: OASIS Open Document Format for Office Applications. Het gebruik van dit element verschilt of het een Calc of een Writer-document betreft. Bij documenten als van Writer wordt het element direct opgenomen binnen het element <table:table>.
    3. Zie: API documentation van com.sun.star.xml.sax.Parser
    4. Zie: API documentation van com.sun.star.xml.sax.XDocumentHandler