Google Apps Script在body中查找url并将其格式化为超链接

Google Apps Script to find URLs in body and format them as hyperlinks

本文关键字:格式化 超链接 url 查找 Script Apps body Google      更新时间:2023-09-26

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

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

我把这篇文章转储到谷歌文档中,与许多人共享(我们运行Python和R课程,并为每个参与者启动一个虚拟机)。

我希望能够将我的输出格式为超链接,这样与会者只需要点击URL,而不是将其复制并粘贴到浏览器中(firstworldproblems)。

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

到目前为止,我所得到的大部分是基于对另一个问题的回答:

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);
  }
}

我的模式和循环工作良好,除了被写入超链接的URL是http://text,如果我在格式链接部分使用foundLink,或http://rangeelement,如果我使用link变量。

我怎么能有脚本设置URL作为文本本身?

(Javascript新手,一直在使用这样的练习来学习它和Google Apps Script)

Update: a-change的注释让我注意到文本元素的getText()方法,因此相关行变为foundLink.setLinkUrl(start, end, foundLink.getText());。然而,这仍然不完全工作,并插入指向about:blank的链接。

如何处理从findText()中提取的文本?

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

你可以把所有的链接写在单独的行上,函数会很好地工作,但文档本身可能看起来不那么好。

因此,这里需要做的是将链接从foundLink.getText()字符串中额外切片。下面是稍微修改过的初始函数:

 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 Script不是完整的JS。

这适用于我,检测http和https链接与末尾的空白。我已经测试了在行/段结束时开始/结束的链接,以及前面和后面的测试(用空白分隔),它们都可以工作。

function makeLinks() {
  var linkRegex = "https?:'/'/[^''s]*";
  // Open active doc
  var body = DocumentApp.getActiveDocument().getBody();
  // Find URLs
  //Logger.log(body.findText("http").getElement().asText().getText());
  var link = body.findText(linkRegex);
  // Loop through the body finding texts matching the search pattern
  while (link != null) {
    // Get the link as an object
    var linkElement = link.getElement().asText();
    // Get the positions of start and end
    var start = link.getStartOffset();
    var end = link.getEndOffsetInclusive();
    //slice only the link out of it
    var correctLink = linkElement.getText().slice(start, end);
//    Logger.log("correctLink " + correctLink);
    // Format link
    linkElement.setLinkUrl(start, end, correctLink);
    // Find next
    link = body.findText(linkRegex, link);
  }
}

我希望它能帮助到别人