Mr.*_*. B 6 javascript google-docs google-apps-script
我正在使用 Google Apps 脚本,并尝试检索与下面的 GAS 函数返回的文本字符串中的一个单词超链接的 URL,但我收到了下面列出的错误。
\n\n正如您从我的代码中看到的,我是新手,因此非常感谢任何帮助和“最佳实践”。
\n\nGAS IDE 返回的错误消息
\n\n\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\nfunction 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 }\nRun Code Online (Sandbox Code Playgroud)\n\n上述函数返回的字符串
\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
网址只在这个词上HYPERLINK,而不位于字符串的其余部分。
该文档在这里 - https://docs.google.com/document/d/18zJMjXWoBNpNzrNuPT-nQ_6Us1IbACfDNXQZJqnj1P4/edit#,您可以在表格的第 3 行中看到单词 HYPERLINK 和超链接
\n\n感谢您的帮助!
\n如果我对你问题的理解是正确的,那么这个修改怎么样?
\n\ngetLinkUrl(offset)用于此目的。反映以上几点的脚本如下。当您使用此修改后的脚本时,请将此脚本复制并粘贴到共享 Google 文档的脚本编辑器中,然后运行sample().
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}\nRun Code Online (Sandbox Code Playgroud)\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]\nRun Code Online (Sandbox Code Playgroud)\n\n如果我误解了你的问题,我很抱歉。
\n