Google Docs脚本将选择转换为超链接
我是 Google Apps 脚本的新手,过去只接触过一点 JS。
我觉得这应该很简单,但找不到答案——也许太明显了!
我希望用户能够在文档中选择文本(在本例中是名称)。然后,脚本会将所选文本转换为链接。
例如 在文档中突出显示“John Smith” 这变成了一个超链接,网址为“https://naming.com/john-smith”
我以前将其作为 MS Word 宏使用,并希望转换为 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
不幸的是,我什至无法获取以下内容在开始研究如何将选定的文本转换为 slug 作为变量之前!
function linkName () {
DocumentApp.getActiveDocument()
.getSelection()
.editAsText()
.setLinkUrl(0,0,"https://naming.com/john-smith");
DocumentApp.getUi().alert("Link added")
}
任何帮助将不胜感激!
干杯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上可以这样做:
但是还有另一个问题:您应该如何运行此脚本?我认为,从脚本编辑器运行它并不是最方便的方法。可能需要将功能添加到自定义菜单或其他内容中。
Basically it can be done this way:
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.
感谢@Yuri,我到达了我需要去的地方!
下面是代码,也是从这里借用的: https://stackoverflow.com/questions/16639331/get -user-selected-text
并从这里开始:https://gist.github.com/codeguy/6684588
我确信有更干净的方法,但它有效!
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
I'm sure there are cleaner ways, but it's working!