Need help with macro to convert part of cell to hyperlink

I have around 3000 cells with a string comprising words in each. I am trying to create a macro which converts the first word into a hyperlink (which still works after exporting to pdf), and leaves the rest of the text as is, but am struggling to get it to work. Note that the built in hyperlink formula does not appear as a clickable link after converting to pdf, but dispatcher.executeDispatch(document, “.uno:SetHyperlink”, “”, 0, args2()) does.

Any help would be greatly appreciated.

My macro so far is listed below. It finds the first word and converts it to a hyperlink. I then want to append the rest of the text as a string, but this just overwrites the hyperlink. I can do what I want manually, but not with this macro.

sub HyperLink

rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
dim linktext as String
dim appendedtext as String
dim pos
dim contents as String

rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
ThisCell = ThisComponent.CurrentSelection

rem ----------------------------------------------------------------------
rem get data from selected cell
contents = ThisCell.String
pos = Instr(contents," ") 'get position of space character

rem ----------------------------------------------------------------------

    ThisCell.clearContents(4)
    linktext = Mid(contents, 1, pos-1)
    appendedtext = Mid(contents, pos)
    dim args1(0) as new com.sun.star.beans.PropertyValue
    args1(0).Name = "ToPoint"
    args1(0).Value = ThisComponent.CurrentSelection.AbsoluteName
    
    dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())
    
    rem ----------------------------------------------------------------------
    dim args2(5) as new com.sun.star.beans.PropertyValue
    args2(0).Name = "Hyperlink.Text"
    args2(0).Value = linktext '"link"
    args2(1).Name = "Hyperlink.URL"
    args2(1).Value = "https://www.someurl.php?link=" & linktext
    args2(2).Name = "Hyperlink.Target"
    args2(2).Value = ""
    args2(3).Name = "Hyperlink.Name"
    args2(3).Value = ""
    args2(4).Name = "Hyperlink.Type"
    args2(4).Value = 1
    args2(5).Name = "Hyperlink.ReplacementText"
    args2(5).Value = ""
    
    dispatcher.executeDispatch(document, ".uno:SetHyperlink", "", 0, args2())
    
    'Don't know how to now append text without deleting the hyperlink

end sub

Welcome!
Please edit your question and add some example texts of the cells you are trying to convert.

Also, try to explain what your goal is when you convert only part of a cell into a hyperlink? Why not the entire cell, completely? For a user, Ctrl+clicking a cell or clicking the first word in a cell will not make any difference.

If I could see how to attach an image, I could send a screenshot of what I am trying to do. That’s the only way I can give you an example. I don’t want the entire cell converting to a link, since the underlining would make the appearance of the cell very untidy. I want the appended text to appear as is.

Well, they usually suggest using this button when editing a message


I just press Ctrl+V and the image from the clipboard goes exactly where it is needed.

Another problem awaits us here. Calc will still try to open the link as he understands it.
For example, a cell contains a formula
=HYPERLINK("https://ask.libreoffice.org/t/calc-convert-text-to-link/1945/2";"https://ask.libreoffice.org/t/calc-convert-text-to-link/1945/2") & " jump to solution"
or just with short HYPERLINK(), without second param
=HYPERLINK("https://ask.libreoffice.org/t/calc-convert-text-to-link/1945/2") & " jump to solution"
In a cell it will look like this
image
You see? All cell text is treated as a link address. Indeed, after Ctrl+click, the browser will try to open an https://ask.libreoffice.org/t/calc-convert-text-to-link/1945/2%20jump%20to%20solution page that never existed.
In other words, jump will be performed without errors for a cell with the formula
=HYPERLINK("https://ask.libreoffice.org/t/calc-convert-text-to-link/1945/2";"https://ask.libreoffice.org/t/calc-convert-text-to-link/1945/2"&" jump to solution")
or just
=HYPERLINK("https://ask.libreoffice.org/t/calc-convert-text-to-link/1945/2";"jump to solution")
I used the HYPERLINK() function to demonstrate the problem for you. The same will most likely apply to a “real” hyperlink that you paste using Ctrl-K. Trying to achieve a pretty display will confuse the user of your spreadsheet.

hl example

I have tried the HYPERLINK() function. However, after exporting to pdf, the link disappears (the ultimate destination for this project will be a pdf file). No doubt there are things I don’t understand wrt remedying this! This is not the case with ctrl-k or with uno:SetHyperlink which both export into a pdf file correctly.

Maybe I could have the whole cell as a link, but change the character formatting for the appended text, but that just opens up a different set of questions, not least of which is how I handle the new lines in the cell…

In that case, maybe you should consider using Writer instead of Calc? There’s a lot more formatting options there.

Hallo

The following works for me, I did test with some content like:

105022 __blah
105023 __blah
105024 __blah
105025 __blah

select the range[s] which should be converted into links and run:

def create_links_in_selection(*_):
    BASEURL = "https://ask.libreoffice.org/t/" #change me
    doc = XSCRIPTCONTEXT.getDocument()
    selection = doc.CurrentSelection
    for cell in selection.queryContentCells(4).Cells: # 4 ⇒ Text-Content
        content = cell.String
        link = doc.createInstance( "com.sun.star.text.textfield.URL" )
        thread = link.Representation = content.split(" ",1)[0]
        link.URL = f"{BASEURL}{thread}"
        text = cell.getText()
        cell.String = content
        cursor = text.createTextCursor()
        cursor.gotoStart(False)
        cursor.goRight( len(thread), True )
        text.insertTextContent(cursor, link, True)

its python, may be you need apso.oxt to manage your python-scripts

ps.: mostly copy&paste from https://forum.openoffice.org/en/forum/viewtopic.php?p=215533#p215533

Thanks. I’ll take a look later and let you know how I get on.

Absolutely brilliant! Thanks. Your Python script worked.