简体   繁体   English

使用 Google Apps 脚本从 Google 文档中的文本中检索链接的 URL

[英]Retrieve linked URL from text in a Google Document using Google Apps Script

I'm working with Google Apps Script and I'm trying to retrieve the URL hyperlinked to one word in the text string returned by the GAS function below, but I'm getting the error listed below.我正在使用 Google Apps 脚本,我正在尝试检索超链接到下面 GAS 函数返回的文本字符串中的一个单词的 URL,但我收到了下面列出的错误。

As you can see from my code, I'm a rookie, so any help and 'best practice' is greatly appreciated.正如您从我的代码中看到的那样,我是一个菜鸟,因此非常感谢任何帮助和“最佳实践”。

Error Message Returned By GAS IDE GAS IDE 返回的错误信息

TypeError: Cannot find function getLinkUrl in object HYPERLINK to your “Intro To Google Documents” document. TypeError:在您的“Google 文档简介”文档的对象HYPERLINK 中找不到函数 getLinkUrl。 Open your MrBenrudShared folder and create a new blank Google Document.打开您的 MrBenrudShared 文件夹并创建一个新的空白 Google 文档。 Name it “Your Name: Intro To Google Documents”.. (line 19, file "Code")将其命名为“Your Name: Intro To Google Documents”..(第 19 行,文件“代码”)

GAS Function气体功能

function getURLfromHyprlink() {
  var body = DocumentApp.getActiveDocument().getBody();
  Logger.log(body.getNumChildren());

  // table is bode child element #1 of 3.
  var rubricTable = body.getChild(1);
  Logger.log(rubricTable);

  // Find out about row 3 in table
  var studentWorkRow = rubricTable.getChild(2);
  Logger.log(studentWorkRow);

  // Find what is in column2 of hyperlink row
  var studentHyperlinkCell = studentWorkRow.getChild(1);
  Logger.log(studentHyperlinkCell); //tells me it is a table cell

  // Returns text from studentHyperlinkCell
  var hyperlinkText = studentHyperlinkCell.asText().getText();
  var hyperlinkURL = hyperlinkText.getLinkUrl();
  Logger.log(hyperlinkURL); 

  }

THE STRING RETURNED By The Above Function上述函数返回的字符串

HYPERLINK to your “Intro To Google Documents” document.超级链接到您的“Google 文档简介”文档。

Open your MrBenrudShared folder and create a new blank Google Document.打开您的 MrBenrudShared 文件夹并创建一个新的空白 Google 文档。 Name it “Your Name: Intro To Google Documents”.将其命名为“您的姓名:Google 文档简介”。

The URL is only on the word HYPERLINK , and not on the rest of the string. URL 仅在单词HYPERLINK ,而不在字符串的其余部分上。

The document is here - https://docs.google.com/document/d/18zJMjXWoBNpNzrNuPT-nQ_6Us1IbACfDNXQZJqnj1P4/edit# and you can see the word HYPERLINK in row3 of the table and the hyperlink文档在这里 - https://docs.google.com/document/d/18zJMjXWoBNpNzrNuPT-nQ_6Us1IbACfDNXQZJqnj1P4/edit# ,您可以在表格的第 3 行和超链接中看到单词 HYPERLINK

Thanks for your help!谢谢你的帮助!

  • You want to retrieve URL of the hyperlink in texts in Google Document. 您想要检索Google文档中文本中超链接的URL。
  • In your situation, the texts you want to retrieve are in a table which can be seen at your shared sample Document. 在您的情况下,您要检索的文本位于一个表格中,该表格可以在您的共享样本文档中看到。

If my understanding for your question is correct, how about this modification? 如果我对你的问题的理解是正确的,那么这个修改怎么样?

Modification points: 修改要点:

  • Retrieve each cell. 检索每个单元格。
  • Retrieve children from each cell and retrieve the text from the child. 从每个单元格中检索子项并从子项中检索文本。
  • In your case, it splits the texts every word. 在您的情况下,它会在每个单词中分割文本。
  • Check the hyperlink every word and retrieve the link when the word has the link. 检查每个单词的超链接,并在单词具有链接时检索链接。
    • getLinkUrl(offset) is used for this. getLinkUrl(offset)用于此目的。

The script which reflected above points is as follows. 反映上述要点的脚本如下。 When you use this modified script, please copy and paste this script to the script editor of your shared Google Document, and run sample() . 使用此修改后的脚本时,请将此脚本复制并粘贴到共享Google文档的脚本编辑器中,然后运行sample()

Modified script: 修改后的脚本

function sample() {
  var body = DocumentApp.getActiveDocument().getBody();
  var table = body.getTables()[0];
  var rows = table.getNumRows();
  var result = [];
  for (var i = 0; i < rows; i++) {
    var cols = table.getRow(i).getNumCells();
    for (var j = 0; j < cols; j++) {
      var cell = table.getCell(i, j);
      for (var k = 0; k < cell.getNumChildren(); k++) {
        var child = cell.getChild(k).asText();
        var text = child.getText(); // Retrieve text of a child in a cell.
        var words = text.match(/\S+/g); // Split text every word.
        if (words) {
          var links = words.map(function(e) {return {
            text: text,
            linkedWord: e,
            url: child.getLinkUrl(child.findText(e).getStartOffset()), // Check the link every word.
          }}).filter(function(e) {return e.url != null}); // Retrieve the link when the word has the link.
          if (links.length > 0) result.push(links);
        }       
      }
    }
  }
  result = Array.prototype.concat.apply([], result);
  Logger.log(result)
}

Result: 结果:

When this script is used for your shared sample Document, the following result is retrieved. 将此脚本用于共享示例文档时,将检索以下结果。

[
  {
    "text": "HYPERLINK  to your “Intro To Google Documents” document. ",
    "linkedWord": "HYPERLINK",
    "url": "https://docs.google.com/document/d/1HDGUxgqZYVQS5b8gLtiQTNumaXRjP2Ao1fHu2EFqn_U/edit"
  },
  {
    "text": "Video",
    "linkedWord": "Video",
    "url": "http://mrbenrud.net/videos/video.php?id=&v=EhnT8urxs_E&title=How to Create a Folder in Google Drive&description="
  },
  {
    "text": "Some instructions will have hyperlinks and other will use different types for formating. ",
    "linkedWord": "hyperlinks",
    "url": "https://docs.google.com/document/d/1tS-Pq2aqG7HpsMA5br2NzrjH9DFdiz9oA0S70vejg4c/edit"
  },
  {
    "text": "Video",
    "linkedWord": "Video",
    "url": "http://mrbenrud.com/index.php/tutorials/project-tutorials/94-how-to-share-a-folder-in-google-drive-with-someone-else-so-they-can-edit-it"
  },
  {
    "text": "Video",
    "linkedWord": "Video",
    "url": "http://mrbenrud.com/index.php/tutorials/project-tutorials/98-how-to-move-a-document-in-google-drive-into-another-folder"
  },
  {
    "text": "Video",
    "linkedWord": "Video",
    "url": "http://mrbenrud.com/index.php/tutorials/project-tutorials/96-how-to-search-for-and-filter-through-images-using-google"
  },
  {
    "text": "Video",
    "linkedWord": "Video",
    "url": "http://mrbenrud.com/index.php/tutorials/project-tutorials/99-how-to-rename-file-on-a-mac-in-osx"
  }
]

Note: 注意:

  • In this script, all links in a table are retrieved. 在此脚本中,将检索表中的所有链接。 So if you want to retrieve the specific cell, please modify the script. 因此,如果要检索特定单元格,请修改脚本。

References: 参考文献:

If I misunderstand your question, I'm sorry. 如果我误解你的问题,我很抱歉。

This post helped me a lot to understand the mechanics of Google Docs when dealing with hyperlinks.这篇文章在处理超链接时帮助我理解了 Google Docs 的机制。

Here my solution for a document wide search & replace:这是我的文档范围搜索和替换的解决方案:

https://gist.github.com/vladox/f8cd873571ffa8038fb15175a476f20b https://gist.github.com/vladox/f8cd873571ffa8038fb15175a476f20b

var oldLink = "https://your-old-link-here";
var newLink = "https://your-new-link-here";
var documentId = 'YOUR-GOOGLE-DOCUMENT-ID-HERE';
var doc = DocumentApp.openById(documentId);
var searchType = DocumentApp.ElementType.TEXT;

function findAndReplaceLinks() {
  var body = doc.getBody();
  var text = body.getText();

  var searchResult = null;
  var searchResultTextElement = null;
  var searchResultText = "";
  while (searchResult = body.findElement(searchType, searchResult)) {
    searchResultTextElement = searchResult.getElement().asText();
    searchResultText = searchResultTextElement.getText();
    // Logger.log("TEXT: %s", searchResultText);

    var words = searchResultText.match(/\S+/g);
    if (words) {
      words.map(function (e) {
        // sanitize search terms for regex relevent symbols
        e = e.replaceAll("(", "\\(").replaceAll(")", "\\)").replaceAll("+", "\\+").replaceAll("*", "\\*");
        e = e.replaceAll("[", "\\[").replaceAll("]", "\\]").replaceAll("{", "\\{").replaceAll("}", "\\}");
        // Logger.log("WORD: %s", e);

        var partialElementUrl = null;
        if (e.trim() != "") {
          var partialElement = searchResultTextElement.findText(e);
          var partialElementText = partialElement.getElement().asText();
          var startOffset = partialElement.getStartOffset();
          var endOffsetInclusive = partialElement.getEndOffsetInclusive();
          partialElementUrl = searchResultTextElement.getLinkUrl(partialElement.getStartOffset());
          if (partialElementUrl != null) {
            var updatedUrl = partialElementUrl.replace(oldLink, newLink);
            if (partialElementUrl.includes(oldLink)) {
              Logger.log("REPLACING WORD: %s AT OFFSET: %s", partialElementText.getText().substring(startOffset, endOffsetInclusive + 1), startOffset);
              partialElementText.setLinkUrl(startOffset, endOffsetInclusive, updatedUrl);
            } else if (partialElementUrl.includes(newLink)) {
              Logger.log("ALREADY REPLACED WORD: %s", partialElementUrl);
            }
          }
        }
      });
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM