Ask Your Question
0

Converting text to hyperlink

asked 2019-01-02 17:32:33 +0100

Neill_R gravatar image

I wish to write a CALC Basic macro that will do the same as F2(edit cell) <end key=""> <space key=""> <backspace key=""> <enter key="">

The result is to create a hyperlink as the text in the cell is an email address.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

answered 2019-01-02 20:30:49 +0100

Lupp gravatar image

I don't know precisely what you want to achieve. Doing exactly as you describe doesn't seem to be possible.
If you want to apply the "macro" to single selected cells you can assure for that they contain correct mail addresses, you can use the code below.
What good for?

Sub convertCellContentToLinkForSingleSelectedCell(Optional pEvent As Object, Optional pPre As String)
theDoc         = ThisComponent
If NOT theDoc.supportsService("com.sun.star.sheet.SpreadsheetDocument") Then Exit Sub
theSelC        = theDoc.CurrentSelection
If NOT theSelC.supportsService("com.sun.star.sheet.SheetCell") Then Exit Sub
If NOT (theSelC.Type=2) Then Exit Sub
content        = theSelC.String
If IsMissing(pPre) Then pPre = "mailto:"
If NOT isUrlSyntax(content, pPre) Then Exit Sub
UrlField       = theDoc.createInstance("com.sun.star.text.TextField.URL")
UrlField.Representation = theSelC.String
UrlField.URL   = pPre & content
theDoc.UndoManager.EnterUndoContext("Linkify")
theSelC.String = ""
tCur           = theSelC.createTextCursorByRange(theSelC.Start)
theSelC.insertTextContent(tCur, UrlField, False)
theDoc.UndoManager.LeaveUndoContext
End Sub

Function isUrlSyntax(pText As String, pPre As String) As Boolean
REM This Function does not check the content of the cell for the syntax of mail addresses!
REM Write a respective body for the function if needed.
isUrlSyntax = (pText<>"")
End Function
edit flag offensive delete link more

Comments

Thank you very much

Neill_R gravatar imageNeill_R ( 2019-01-02 23:24:17 +0100 )edit
0

answered 2019-01-02 23:22:59 +0100

Neill_R gravatar image
Sub convertCellContentToLinkForSingleSelectedCell(Optional pEvent As Object, Optional pPre As String)

document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

theDoc         = ThisComponent
If NOT theDoc.supportsService("com.sun.star.sheet.SpreadsheetDocument") Then Exit Sub
theSelC        = theDoc.CurrentSelection
If NOT theSelC.supportsService("com.sun.star.sheet.SheetCell") Then Exit Sub
If  (theSelC.Type=2) Then
    content        = theSelC.String
    If IsMissing(pPre) Then pPre = "mailto:"
    If isUrlSyntax(content, pPre) Then
        UrlField       = theDoc.createInstance("com.sun.star.text.TextField.URL")
        UrlField.Representation = theSelC.String
        UrlField.URL   = pPre & content
        theDoc.UndoManager.EnterUndoContext("Linkify")
        theSelC.String = ""
        tCur           = theSelC.createTextCursorByRange(theSelC.Start)
        theSelC.insertTextContent(tCur, UrlField, False)
        theDoc.UndoManager.LeaveUndoContext
    end if
end if
dim args3(1) as new com.sun.star.beans.PropertyValue
args3(0).Name = "By"
args3(0).Value = 1
args3(1).Name = "Sel"
args3(1).Value = false

dispatcher.executeDispatch(document, ".uno:GoDown", "", 0, args3())

End Sub

Function isUrlSyntax(pText As String, pPre As String) As Boolean REM This Function does not check the content of the cell for the syntax of mail addresses! REM Write a respective body for the function if needed. isUrlSyntax = (pText<>"") End Function

sub doit while true

convertCellContentToLinkForSingleSelectedCell

wend end sub

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-01-02 17:32:33 +0100

Seen: 49 times

Last updated: Jan 02