Reputation: 1
I am trying to search-and-replace linked text from an old url to a new url.
It is not working and I have spent hours and hours. If I remove the "if (found)" it gives me "TypeError: Cannot read property 'getElement' of null" even though my files have text that is linked to this old_url.
Please, help me.
function myFunction() {
var old_url ="http://hurlx1.com";
var new_url ="http://urlxa.com";
var files = DriveApp.getFolderById("my folder id").getFilesByType(MimeType.GOOGLE_DOCS);
while (files.hasNext()) {
var file = files.next();
var doc = DocumentApp.openById(file.getId());
found=doc.getBody().findText(old_url);
if (found) {
var link_element = found.getElement().asText();
var start = found.getStartOffset();
var end = found.getEndOffsetInclusive();
var correct_link = link_element.getText().slice(start, end);
link_element.setLinkUrl(start, end, correct_link);
}
}
}
Upvotes: 0
Views: 942
Reputation: 201358
I believe your situation and goal as follows.
old_url
.old_url
. But the text is different from old_url
.old_url
with new_url
using Google Apps Script.For this, how about this answer?
old_url
is not found in the Google Document with found=doc.getBody().findText(old_url);
, found
becomes null
even when old_url
is set as the hyperlink. Because findText
searches the text on Document body, and that cannot search the hyperlinks set to the texts. I think that this is the reason of your issue.var new_url ="http://urlxa.com";
is used. But when the link is set, correct_link
is used like link_element.setLinkUrl(start, end, correct_link);
. By this, new_url
is not set.http://hurlx1.com
to new_url
of var new_url ="http://urlxa.com";
, it is required to also modify the text.old_url
is updated. If there are several values of old_url
in the Document, it is required to update them using the loop.This modified script can be used for the following patterns.
old_url
.
old_url
is also updated with old_url
.old_url
. But the text is different from old_url
.
old_url
is updated.old_url
in the Google Document.function myFunction() {
var old_url ="http://hurlx1.com";
var new_url ="http://urlxa.com";
var files = DriveApp.getFolderById("my folder id").getFilesByType(MimeType.GOOGLE_DOCS);
while (files.hasNext()) {
var file = files.next();
var doc = DocumentApp.openById(file.getId());
var body = doc.getBody();
// The following script is used for the situation that the text and hyperlink are the same with `old_url`.
var found = body.findText(old_url);
while (found) {
var link_element = found.getElement().asText();
var start = found.getStartOffset();
var end = found.getEndOffsetInclusive();
var correct_link = link_element.getText().slice(start, end);
link_element.setLinkUrl(start, end, new_url).replaceText(old_url, new_url);
found = body.findText(old_url, found);
}
// The following script is used for the situation that although the hyperlink is `old_url`, the text is different from `old_url`.
var text = body.editAsText();
for (var i = 0; i < text.getText().length; i++) {
if (text.getLinkUrl(i) == old_url) {
text.setLinkUrl(i, i + 1, new_url);
}
}
}
}
Upvotes: 4