Select a range and call the macro SelectionInsertHyperlink. Or don’t select any, but activate the sheet and call the ActiveSheetInsertHyperlink. All nested links become hyperlinks.
Sub SelectionInsertHyperlink()
Dim oSelection As Object: oSelection = ThisComponent.CurrentSelection
If Not (oSelection.supportsService("com.sun.star.sheet.SheetCellRange") _
Or oSelection.supportsService("com.sun.star.sheet.SheetCellRanges")) Then
MsgBox "The range not selected. Multiple selection allowed." _
, MB_ICONEXCLAMATION, "Selection Error"
Exit Sub
End If
Call ActiveSheetInsertHyperlink(oSelection)
End Sub
Sub ActiveSheetInsertHyperlink(Optional oSelection)
Dim oRanges As Object, oCell As Object
If IsMissing(oSelection) Then
oRanges = ThisComponent.CurrentController.ActiveSheet _
.queryContentCells(com.sun.star.sheet.CellFlags.STRING)
Else
oRanges = oSelection.queryVisibleCells() 'visible only
End If
For Each oCell In oRanges.Cells
Call CellInsertHyperLink(oCell)
Next
End Sub
Sub CellInsertHyperLink(oCell As Object)
''' Remarks:
''' • oCell.Text.insertTextContent(xRange, xContent, bAbsorb)
''' <xRange> specifies the position of insertion.
''' <xContent> is the text content to be inserted.
''' <bAbsorb> specifies whether the text spanned by xRange will be replaced.
''' • If True then the content of xRange will be replaced by xContent,
''' otherwise xContent will be inserted at the end of xRange.
Dim oField As Object
Dim s As String, i As Integer
oField = ThisComponent.createInstance("com.sun.star.text.TextField.URL")
Rem Xray oField 'ScEditFieldObj
s = oCell.String
i = InStr(1, s, "http")
If i > 0 Then
oCell.String = Left(s, i - 1)
oField.URL = Mid(s, i)
oField.Representation = oField.URL
oCell.Text.insertTextContent(oCell.Text.createTextCursor, oField, False)
End If
End Sub
We do everything on the spot, you don’t need a second sheet. You can copy the sheet yourself at any time: before or after conversion.
UPD. Remarks
The cell text is on the left, the link is on the right as in your example. If not, the code needs to be changed.
The BASIC functions here are the same as the sheet functions of the same name, and you should be able to understand what they do.
See:
oCell.String = Left(s, i - 1) 'text
oField.URL = Mid(s, i) 'link
You can traverse the link to the first space and assign the found text fragment to the URL. But then you have to move the cursor.
Either way, this is the way to solve your problem.