Javascript 使用谷歌应用程序脚本从谷歌文档中的文本检索链接URL

Javascript 使用谷歌应用程序脚本从谷歌文档中的文本检索链接URL,javascript,google-apps-script,google-docs,Javascript,Google Apps Script,Google Docs,我正在使用GoogleApps脚本,我正在尝试检索超链接到下面的GAS函数返回的文本字符串中的一个单词的URL,但是我得到了下面列出的错误 正如您从我的代码中看到的,我是一名新手,因此非常感谢您的帮助和“最佳实践” 气体IDE返回的错误消息 TypeError:在对象超链接中找不到函数getLinkUrl 你的“谷歌文档简介”文档。打开你的MRB 文件夹并创建一个新的空白Google文档。“命名”您的名字: 谷歌文档简介“。。(第19行,文件“代码”) 气体功能 function getURLf

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

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

气体IDE返回的错误消息

TypeError:在对象超链接中找不到函数getLinkUrl 你的“谷歌文档简介”文档。打开你的MRB 文件夹并创建一个新的空白Google文档。“命名”您的名字: 谷歌文档简介“。。(第19行,文件“代码”)

气体功能

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

  }
上述函数返回的字符串

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

  }
超链接到您的“谷歌文档简介”文档

打开你的共享文件夹,创建一个新的空白谷歌 文件。把它命名为“你的名字:谷歌文档简介”

URL仅在单词
超链接上,而不在字符串的其余部分

文档在这里-您可以在表的第3行中看到单词HYPERLINK和超链接

谢谢你的帮助

  • 您想在Google文档中检索文本中超链接的URL
  • 在您的情况下,要检索的文本位于一个表中,可以在共享示例文档中看到
如果我对你的问题的理解是正确的,那么这次修改怎么样

修改点:
  • 检索每个单元格
  • 从每个单元格中检索子项并从子项中检索文本
  • 在您的情况下,它会将文本拆分为每个单词
  • 检查每个单词的超链接,并在单词具有链接时检索链接。
    • getLinkUrl(offset)
      用于此操作
反映上述要点的脚本如下。使用此修改后的脚本时,请将此脚本复制并粘贴到共享Google文档的脚本编辑器中,然后运行
sample()

修改脚本: 注:
  • 在此脚本中,检索表中的所有链接。因此,如果要检索特定单元格,请修改脚本
参考资料:

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

刚请你在Google上与我共享权限,刚尝试了getLinkUrl(0),但没有成功。塔奈克,谢谢你的努力!!两个问题:第一,我是否必须将段落分解成单独的单词才能让getLinkUrl()工作?第二,如果我只是想从段落字符串的第一个字母中获取链接的URL,该怎么办?我不能使用getLinkUrl([0])吗?@B先生谢谢您的回复。A1:你也可以搜索每个角色。在你的情况下,我发现超链接包含在文本中的每个单词中。所以我认为把每一个单词分开可能是合适的。A2:例如,在这种情况下,您可以使用
cell.getChild(k).asText().getLinkUrl(n)
检索它。在的情况下,可以使用
var paragraphText=paration.asText().getLinkUrl(2)
.A2!非常感谢你!不开玩笑,我已经在这方面工作了四个多小时,唯一认为我缺少的是
.asText()
。。。再次感谢@B先生,欢迎。我很高兴你的问题解决了。我也可以从你的问题中学习。我也谢谢你。是的。我现在正在为这个问题更新GAS。我怎么才能相信你的回答呢?如果不是你的帮助,我不会得到它。
[
  {
    "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"
  }
]