I'm working with Google Apps Script and I'm trying to retrieve the URL hyperlinked to one word in the text string returned by the GAS function below, but I'm getting the error listed below.
As you can see from my code, I'm a rookie, so any help and 'best practice' is greatly appreciated.
Error Message Returned By GAS IDE
TypeError: Cannot find function getLinkUrl in object HYPERLINK to your “Intro To Google Documents” document. Open your MrBenrudShared folder and create a new blank Google Document. Name it “Your Name: Intro To Google Documents”.. (line 19, file "Code")
GAS Function
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);
}
THE STRING RETURNED By The Above Function
HYPERLINK to your “Intro To Google Documents” document.
Open your MrBenrudShared folder and create a new blank Google Document. Name it “Your Name: Intro To Google Documents”.
The URL is only on the word HYPERLINK
, and not on the rest of the string.
The document is here - https://docs.google.com/document/d/18zJMjXWoBNpNzrNuPT-nQ_6Us1IbACfDNXQZJqnj1P4/edit# and you can see the word HYPERLINK in row3 of the table and the hyperlink
Thanks for your help!
If my understanding for your question is correct, how about this modification?
getLinkUrl(offset)
is used for this.The script which reflected above points is as follows. When you use this modified script, please copy and paste this script to the script editor of your shared Google Document, and run 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)
}
When this script is used for your shared sample Document, the following result is retrieved.
[
{
"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"
}
]
If I misunderstand your question, I'm sorry.