Google Apps脚本可查找正文中的URL并将其格式化为超链接 [英] Google Apps Script to find URLs in body and format them as hyperlinks

查看:49
本文介绍了Google Apps脚本可查找正文中的URL并将其格式化为超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从命令行脚本生成的文本块,该脚本旋转了许多虚拟机.文本输出包含有关如何访问虚拟机上的Web应用程序的说明,例如:

I have a block of text generated from a command line script which spins up a number of virtual machines. The text output contains instructions on how to access webapps on the virtual machines, so something like:

TrainingMachine01
Username: [user] 
Password: [pass] 
iPython: http://ip/ 
RStudio: http://ip:8787/

我将这段文本存储到与许多人共享的Google文档中(我们使用Python和R进行课程,并为每个参与者启动虚拟机).

I take this text and dump it into a Google Doc which is shared with a number of people (we run courses in Python and R, and spin up a VM for each attendee).

我希望能够将输出格式设置为超链接,以便与会者只需要单击URL即可,而无需将其复制并粘贴到浏览器中(firstworld问题).

I would like to be able to format my output as hyperlinks, so that the attendees only need to click on the URL rather than copy it and paste it into a browser (firstworldproblems).

在研究了将文本粘贴到Google文档中的方法之后,我认为没有比Google Apps脚本更简单的解决方案了,它可以简单地找到与URL匹配的模式,并使其成为超链接.

After investigating ways of pasting text into Google Docs, I don't think there's a simpler solution than a Google Apps script, which would simply find patterns matching a URL, and make them hyperlinks.

这是我到目前为止的内容,主要基于这个答案对另一个问题:

Here's what I have so far, based in large part on this answer to another question:

function updateLinks() {
  // Open active doc
  var body = DocumentApp.getActiveDocument().getBody();
  // Find URLs
  var link = body.findText("http:\/\/.*\/");

  // Loop through
  while (link != null) {
    // Get the link as an object
    var foundLink = link.getElement().asText();

    // Get the positions of start and end
    var start = link.getStartOffset();
    var end =link.getEndOffsetInclusive();

    // Format link
    foundLink.setLinkUrl(start, end, foundLink);

    // Find next
    link = body.findText("http:\/\/.*\/", link);
  }
}

我的模式和循环工作正常,但如果我在格式"链接部分中使用 foundLink ,则写到超链接中的URL要么是 http://text http://rangeelement (如果我使用 link 变量).

My pattern and loop are working fine, except the URL that's being written into the hyperlink is either http://text if I use foundLink in the Format link section, or http://rangeelement if I use the link var.

如何让脚本将URL设置为文本本身?

How can I have the script set the URL as the text itself?

(JavaScript的新手,并且一直在使用类似的练习来学习它和Google Apps脚本)

(New to Javascript and have been using exercises like this to learn it and Google Apps Script)

更新:更改后的注释将我指向文本元素的 getText()方法,因此相关行变为 foundLink.setLinkUrl(start,最后,foundLink.getText()); .但是,这仍然不太有效,并且正在插入指向 about:blank 的链接.有什么想法如何清理从 findText()中提取的文本吗?

Update: a-change's comment pointed me to the getText() method on text elements, so that the relevant line becomes foundLink.setLinkUrl(start, end, foundLink.getText());. However this is still not quite working, and is inserting links pointing to about:blank. Any ideas how to sanitise the text extracted from findText()?

推荐答案

更详细地进行了研究.如果您记录 foundLink.getText()的值,您会看到它实际上包含在该行上找到的所有字符串,即 RStudio:http://ip:8787/,而不只是 http://ip:8787/.这可能是因为 link.getElement()返回包含找到的文本的范围的整个元素.

Looked into it in more detail. If you log the value of foundLink.getText() you'll see that it actually contains all the string found on that line, i.e. RStudio: http://ip:8787/ instead of just http://ip:8787/. This probably happens because link.getElement() returns the whole element of the range containing the found text.

您可以将所有链接写在单独的行上,该功能可以很好地工作,但是文档本身看起来可能不那么好.

You could write all your links on separate lines and the function would work nicely but the doc itself wouldn't probably look that fine.

因此,这里您需要做的是将链接另外切成 foundLink.getText()字符串.这是稍作修改的初始函数:

So what you need to do here is to additionally slice the link out of the foundLink.getText() string. Here's the slightly modified initial function:

 function updateLinks() {
  // Open active doc
  var body = DocumentApp.getActiveDocument().getBody();
  // Find URLs
  //Logger.log(body.findText("http").getElement().asText().getText());
  var link = body.findText("http:\/\/.*\/");
  // Loop through
  while (link != null) {
    // Get the link as an object
    var foundLink = link.getElement().asText();
    // Get the positions of start and end
    var start = link.getStartOffset();
    var end = link.getEndOffsetInclusive();
    //check the value of foundLink if needed
    //Logger.log(foundLink.getText());
    //slice only the link out of it
    var correctLink = foundLink.getText().slice(start, end);
    // Format link
    foundLink.setLinkUrl(start, end, correctLink);
    // Find next
    link = body.findText("http:\/\/.*\/", link);
  }
}

这篇关于Google Apps脚本可查找正文中的URL并将其格式化为超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆