LibreOffice Draw -add hyperlinks based on query table

ぃ、小莉子 提交于 2019-12-12 02:59:57

问题


I am using draw to mark up a pdf format index map. So in grid 99, the text hyperlinks to map99.pdf

There are 1000's of grid cells - is there a way for a (macro) to scan for text in a sheet that is like

Text in File | Link to add
99|file:///c:/maps/map99.pdf
100|file:///c:/maps/map100.pdf

and add links to the relevant file whenever the text is found (99,100 etc).

I don't use libre much but happy to implement any programatic solution.


回答1:


Ok, after using xray to drill through enumerated content, I finally have the answer. The code needs to create a text field using a cursor. Here is a complete working solution:

Sub AddLinks
    Dim oDocument As Object
    Dim vDescriptor, vFound
    Dim numText As String, tryNumText As Integer
    Dim oDrawPages, oDrawPage
    Dim oField, oCurs
    Dim numChanged As Integer

    oDocument = ThisComponent
    oDrawPages = oDocument.getDrawPages()
    oDrawPage = oDrawPages.getByIndex(0)
    numChanged = 0
    For tryNumText = 1 to 1000
        vDescriptor = oDrawPage.createSearchDescriptor
        With vDescriptor
            '.SearchString = "[:digit:]+"  'Patterns work in search box but not here?
            .SearchString = tryNumText
        End With
        vFound = oDrawPage.findFirst(vDescriptor)
        If Not IsNull(vFound) Then
            numText = vFound.getString()
            oField = ThisComponent.createInstance("com.sun.star.text.TextField.URL") 
            oField.Representation = numText
            oField.URL = numText & ".pdf"
            vFound.setString("")
            oCurs = vFound.getText().createTextCursorByRange(vFound)
            oCurs.getText().insertTextContent(oCurs, oField, False)
            numChanged = numChanged + 1 
        End If
    Next tryNumText
    MsgBox("Added " & numChanged & " links.")
End Sub

To save relative links, go to File -> Export as PDF -> Links and check Export URLs relative to file system.

I uploaded an example file here that works. For some reason your example file is hanging on my system -- maybe it's too large.

Replacing text with links is much easier in Writer than in Draw. However Writer does not open PDF files.

There is some related code at https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=1401.



来源:https://stackoverflow.com/questions/35391704/libreoffice-draw-add-hyperlinks-based-on-query-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!