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

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

我正在使用 Google Apps 脚本,我正在尝试检索 URL 超链接到下面 GAS 函数返回的文本字符串中的一个词,但我得到下面列出的错误。

正如您从我的代码中看到的那样,我是一名菜鸟,因此非常感谢您的帮助和 'best practice'。

GAS返回错误信息IDE

TypeError: Cannot find function getLinkUrl in object HYPERLINK to your “Intro To Google Documents” document. Open your MrBenrudShared folder and create a new blank Google Document. Name it “Your Name: Intro To Google Documents”.. (line 19, file "Code")

GAS 函数

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

  }

上述函数返回的字符串

HYPERLINK to your “Intro To Google Documents” document.

Open your MrBenrudShared folder and create a new blank Google Document. Name it “Your Name: Intro To Google Documents”.

URL 仅在单词 HYPERLINK 上,而不在字符串的其余部分。

文档在这里 - https://docs.google.com/document/d/18zJMjXWoBNpNzrNuPT-nQ_6Us1IbACfDNXQZJqnj1P4/edit#,您可以在 table 的第 3 行和超链接中看到单词 HYPERLINK

感谢您的帮助!

  • 您想在 Google 文档的文本中检索 hyperlink 的 URL。
  • 在您的情况下,您要检索的文本位于 table 中,可以在共享示例文档中看到。

如果我对你问题的理解是正确的,那么修改一下怎么样?

修改点:

  • 检索每个单元格。
  • 从每个单元格中检索子元素并从子元素中检索文本。
  • 在您的情况下,它会按每个单词拆分文本。
  • 检查 hyperlink 每个单词,当单词有 link 时检索 link。
    • getLinkUrl(offset)用于此。

反映以上几点的脚本如下。当您使用此修改后的脚本时,请将此脚本复制并粘贴到您共享的 Google 文档和 运行 sample().

的脚本编辑器中

修改后的脚本:

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

结果:

当此脚本用于您共享的示例文档时,将检索到以下结果。

[
  {
    "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"
  }
]

注:

  • 在此脚本中,将检索 table 中的所有 link。所以如果你想检索特定的单元格,请修改脚本。

参考文献:

如果我误解了你的问题,我很抱歉。

这 post 对我理解 Google 文档在处理超链接时的机制有很大帮助。

这是我的文档范围搜索和替换解决方案:

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