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

本教程将介绍使用Google Apps脚本从Google文档的文本中检索链接的URL的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

使用Google Apps脚本从Google文档的文本中检索链接的URL 教程 第1张

问题描述

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

正如您从我的代码中看到的,我是一个新手,因此非常感谢任何帮助和‘最佳实践’。

GAS IDE返回的错误消息

TypeError:在对象中找不到函数getLinkUrl超链接到
你的"谷歌文档简介"文档。打开你的本鲁德先生
文件夹,并创建一个新的空白Google文档。把它命名为"你的名字:
谷歌文档简介"..(第19行,文件"Code")

气体函数

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

  }

上述函数返回的字符串

超链接指向您的"Google Documents简介"文档。

打开您的Mr BenrudShared文件夹并创建一个新的空白Google
文档。将其命名为"您的名字:Google Documents简介"。

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

文档在此处-https://docs.google.com/document/d/18zJMjXWoBNpNzrNuPT-nQ_6Us1IbACfDNXQZJqnj1P4/edit#,您可以在表格的第3行看到单词超级链接和超级链接

谢谢您的帮助!

推荐答案

    您要在Google文档的文本中检索超链接的URL。

    根据您的情况,您要检索的文本位于一个表格中,可以在您的共享示例文档中看到该表格。

如果我对您的问题的理解是正确的,则此修改怎么?

修改点:

    检索每个单元格。

    从每个单元格中检索子项,并从子项中检索文本。

    在您的情况下,它会拆分文本中的每个单词。

    检查每个单词的超链接,如果单词有链接,则检索该链接。

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

注意:

    在此脚本中,将检索表中的所有链接。因此,如果要检索特定单元格,请修改脚本。

引用:

    getLinkUrl(offset)

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

好了关于使用Google Apps脚本从Google文档的文本中检索链接的URL的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。