I'm new to Google Apps Script and have only done a little JS in the past.
I feel like this should be simple, but can't find an answer – maybe it's too obvious!
I would like users to be able to select text, in this case a name, in the document. The script would then turn this selected text into a link.
E.g. Highlight 'John Smith' in the document This becomes a hyperlink with the url as 'https://naming.com/john-smith'
I previously had this working as an MS Word Macro and would like to convert to Google:
Sub LinkName()
Dim Name As String
With Selection
.MoveStartWhile Cset:=" ", Count:=wdForward
.MoveEndWhile Cset:=Chr(13), Count:=wdBackward
.MoveEndWhile Cset:=" ", Count:=wdBackward
End With
Name = LCase(Selection.Text)
Name = Replace(Name, " ", "-")
If Name = "jonny-smith" Then Name = "john-smith"
If Name = "johnathan-smith" Then Name = "john-smith"
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:="https://naming.com/" & Name
End Sub
Unfortunately, I can't even get the following to work before starting to look at how to convert the selected text to a slug as a variable!
function linkName () {
DocumentApp.getActiveDocument()
.getSelection()
.editAsText()
.setLinkUrl(0,0,"https://naming.com/john-smith");
DocumentApp.getUi().alert("Link added")
}
Any help would be much appreciated!
Cheers