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
CodePudding user response:
Basically it can be done this way:
function linkName() {
var selection = DocumentApp
.getActiveDocument()
.getSelection()
.getRangeElements()[0];
var start = selection.getStartOffset();
var end = selection.getEndOffsetInclusive();
var url = 'https://naming.com/john-smith';
selection
.getElement()
.editAsText()
.setLinkUrl(start, end, url);
DocumentApp.getUi().alert("Link added");
}
But there is another question: how do you suppose to run this script? It's not exactly most convenient way to run it from Script Editor, I think. Probably it's need to add the function to the custom menu or something.
CodePudding user response:
Thanks to @Yuri, I got to where I needed to be!
Below is the code, which also borrows from here: https://stackoverflow.com/questions/16639331/get-user-selected-text
and from here: https://gist.github.com/codeguy/6684588
function onOpen() {
DocumentApp.getUi().createMenu('Names')
.addItem("Link Athlete", 'linkName' )
.addToUi();
}
function linkName () {
var selection = DocumentApp
.getActiveDocument()
.getSelection()
.getRangeElements()[0];
var start = selection.getStartOffset();
var end = selection.getEndOffsetInclusive();
var elements = DocumentApp.getActiveDocument().getSelection().getSelectedElements();
if(elements.length > 1){
}
else {
var element = elements[0].getElement();
var startOffset = elements[0].getStartOffset(); // -1 if whole element
var endOffset = elements[0].getEndOffsetInclusive(); // -1 if whole element
var selectedText = element.asText().getText(); // All text from element
// Is only part of the element selected?
if (elements[0].isPartial())
selectedText = selectedText.substring(startOffset,endOffset 1);
// Google Doc UI "word selection" (double click)
// selects trailing spaces - trim them
selectedText = selectedText.trim();
endOffset = startOffset selectedText.length - 1;
selectedText = selectedText
.toString() // Cast to string (optional)
.normalize('NFKD') // The normalize() using NFKD method returns the Unicode Normalization Form of a given string.
.toLowerCase() // Convert the string to lowercase letters
.trim() // Remove whitespace from both sides of a string (optional)
.replace(/\s /g, '-') // Replace spaces with -
.replace(/[^\w\-] /g, '') // Remove all non-word chars
.replace(/\-\- /g, '-'); // Replace multiple - with single -
}
if (selectedText == "jonny-smith") {
selectedText = "john-smith"
}
if (selectedText == "johnathan-smith") {
selectedText = "john-smith"
}
var url = 'https://naming.com/' selectedText;
selection
.getElement()
.editAsText()
.setLinkUrl(start, end, url);
/*
DocumentApp.getUi().alert("Link added");
DocumentApp.getUi().alert(selectedText);
*/
}
I'm sure there are cleaner ways, but it's working!