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

Mr.*_*. B 6 javascript google-docs google-apps-script

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

\n\n

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

\n\n

GAS IDE 返回的错误消息

\n\n
\n

类型错误:无法在对象HYPERLINK中找到函数 getLinkUrl中找到函数 getLinkUrl到\n \xe2\x80\x9cIntro To Google Documents\xe2\x80\x9d 文档。打开 MrBenrudShared\n 文件夹并创建一个新的空白 Google 文档。将其命名为 \xe2\x80\x9c您的姓名:\n Google 文档介绍\xe2\x80\x9d..(第 19 行,文件“代码”)

\n
\n\n

气体功能

\n\n
function getURLfromHyprlink() {\n  var body = DocumentApp.getActiveDocument().getBody();\n  Logger.log(body.getNumChildren());\n\n  // table is bode child element #1 of 3.\n  var rubricTable = body.getChild(1);\n  Logger.log(rubricTable);\n\n  // Find out about row 3 in table\n  var studentWorkRow = rubricTable.getChild(2);\n  Logger.log(studentWorkRow);\n\n  // Find what is in column2 of hyperlink row\n  var studentHyperlinkCell = studentWorkRow.getChild(1);\n  Logger.log(studentHyperlinkCell); //tells me it is a table cell\n\n  // Returns text from studentHyperlinkCell\n  var hyperlinkText = studentHyperlinkCell.asText().getText();\n  var hyperlinkURL = hyperlinkText.getLinkUrl();\n  Logger.log(hyperlinkURL); \n\n  }\n
Run Code Online (Sandbox Code Playgroud)\n\n

上述函数返回的字符串

\n\n
\n

超级链接指向您的 \xe2\x80\x9cIntro To Google Documents\xe2\x80\x9d 文档的

\n\n

打开 MrBenrudShared 文件夹并创建一个新的空白 Google\n 文档。将其命名为 \xe2\x80\x9c您的姓名:Google 文档简介\xe2\x80\x9d。

\n
\n\n

网址只在这个词上HYPERLINK,而不位于字符串的其余部分。

\n\n

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

\n\n

感谢您的帮助!

\n

Tan*_*ike 5

    \n
  • 您想要检索 Google 文档中文本中的超链接的 URL。
  • \n
  • 在您的情况下,您想要检索的文本位于一个表中,可以在您的共享示例文档中看到该表。
  • \n
\n\n

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

\n\n

修改要点:

\n\n
    \n
  • 检索每个单元格。
  • \n
  • 从每个单元格中检索子项并从子项中检索文本。
  • \n
  • 在你的例子中,它会按单词分割文本。
  • \n
  • 检查每个单词的超链接,并在单词具有链接时检索链接。\n\n
      \n
    • getLinkUrl(offset)用于此目的。
    • \n
  • \n
\n\n

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

\n\n

修改后的脚本:

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

结果:

\n\n

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

\n\n
[\n  {\n    "text": "HYPERLINK  to your \xe2\x80\x9cIntro To Google Documents\xe2\x80\x9d document. ",\n    "linkedWord": "HYPERLINK",\n    "url": "https://docs.google.com/document/d/1HDGUxgqZYVQS5b8gLtiQTNumaXRjP2Ao1fHu2EFqn_U/edit"\n  },\n  {\n    "text": "Video",\n    "linkedWord": "Video",\n    "url": "http://mrbenrud.net/videos/video.php?id=&v=EhnT8urxs_E&title=How to Create a Folder in Google Drive&description="\n  },\n  {\n    "text": "Some instructions will have hyperlinks and other will use different types for formating. ",\n    "linkedWord": "hyperlinks",\n    "url": "https://docs.google.com/document/d/1tS-Pq2aqG7HpsMA5br2NzrjH9DFdiz9oA0S70vejg4c/edit"\n  },\n  {\n    "text": "Video",\n    "linkedWord": "Video",\n    "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"\n  },\n  {\n    "text": "Video",\n    "linkedWord": "Video",\n    "url": "http://mrbenrud.com/index.php/tutorials/project-tutorials/98-how-to-move-a-document-in-google-drive-into-another-folder"\n  },\n  {\n    "text": "Video",\n    "linkedWord": "Video",\n    "url": "http://mrbenrud.com/index.php/tutorials/project-tutorials/96-how-to-search-for-and-filter-through-images-using-google"\n  },\n  {\n    "text": "Video",\n    "linkedWord": "Video",\n    "url": "http://mrbenrud.com/index.php/tutorials/project-tutorials/99-how-to-rename-file-on-a-mac-in-osx"\n  }\n]\n
Run Code Online (Sandbox Code Playgroud)\n\n

笔记:

\n\n
    \n
  • 在此脚本中,检索表中的所有链接。所以如果你想检索特定的单元格,请修改脚本。
  • \n
\n\n

参考:

\n\n\n\n

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

\n