我正在使用Google Apps脚本,正在尝试检索由以下GAS函数返回的文本字符串中超链接到一个单词的URL,但出现以下错误。
从我的代码中可以看到,我是一个菜鸟,因此非常感谢您提供的任何帮助和“最佳实践”。
GAS IDE返回的错误消息
TypeError:在对象 HYPERLINK 中找不到函数getLinkUrl 您的“ Google文档简介”文档。打开您的MrBenrudShared 文件夹并创建一个新的空白Google文档。命名为“您的名字: Google文档简介”。(第19行,文件“代码”)
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 到“ Google文档简介”文档。
打开您的MrBenrudShared文件夹并创建一个新的空白Google 文献。将其命名为“您的姓名:Google文档介绍”。
URL仅在单词HYPERLINK
上,而不在字符串的其余部分上。
文档在这里-https://docs.google.com/document/d/18zJMjXWoBNpNzrNuPT-nQ_6Us1IbACfDNXQZJqnj1P4/edit#,您可以在表格的第3行和超链接中看到单词HYPERLINK
感谢您的帮助!
答案 0 :(得分:2)
如果我对您的问题的理解是正确的,那么该修改如何?
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"
}
]
如果我误解了你的问题,对不起。