问题
I entered the following code posted by ziganotschka on https://stackoverflow.com/a/57865441/12252776:
function myFunction() {
var oldLink="http://www.googledoclink1.com";
var newLink="http://www.googledoclinkA.com";
var oldLink2="http://www.googledoclink2.com";
var newLink2="http://www.googledoclinkB.com";
var files = DriveApp.getFiles(); // Note: this gets *every* file in your Google Drive
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
var doc = DocumentApp.openById(file.getId());
var link=doc.getBody().findText(oldLink).getElement().asText();
var link2=doc.getBody().findText(oldLink2).getElement().asText();
link.setLinkUrl(newLink);
doc.replaceText(oldLink, newLink);
link2.setLinkUrl(newLink2);
doc.replaceText(oldLink2, newLink2);
}
Logger.log("Done")
}
...but I get this error message:
TypeError: Cannot call method "getElement" of null. (line 11, file "Replace Hyperlinks")
How do I fix this? What do I have to do?
I also just wanted to pull files from a folder and not all files in my Google Drive, so I replaced
var files = DriveApp.getFiles();
with
var files = DriveApp.getFolderById("insert folder ID").getFiles();
That would work, right?
Thank you, Lauren
回答1:
To your first question:
- You are iterating through all files on your drive /in your folder
- Some of those files might not contain the old link "http://www.googledoclink1.com"
- This throws you the error
- You need to implement a statement that checks either the link is contained, and if not - jump to the next file
To your second question:
- Yes, you can use
var files = DriveApp.getFolderById("insert folder ID").getFiles();
- You can also use
var files = DriveApp.getFolderById("insert folder ID").getFilesByType(MimeType.GOOGLE_DOCS);
to retrieve only Google Docs files
Sample:
function myFunction() {
var oldLink="http://www.googledoclink1.com";
var newLink="http://www.googledoclinkA.com";
var oldLink2="http://www.googledoclink2.com";
var newLink2="http://www.googledoclinkB.com";
var files = DriveApp.getFolderById("insert folder ID").getFilesByType(MimeType.GOOGLE_DOCS);
while (files.hasNext()) {
var file = files.next();
if(file){
var doc = DocumentApp.openById(file.getId());
var link=doc.getBody().findText(oldLink);
var link2=doc.getBody().findText(oldLink2);
if(link){
link=link.getElement().asText();
link.setLinkUrl(newLink);
doc.replaceText(oldLink, newLink);
}
if(link2){
link2=link2.getElement().asText();
link2.setLinkUrl(newLink2);
doc.replaceText(oldLink2, newLink2);
}
}
}
Logger.log("Done")
}
ADDITION:
Addressing your comment
Example: Suppose I had the hyperlink text: "Symptom to Diagnosis: An Evidence-Based Guide, 3e" with the URL accessmedicine.mhmedical.com/book.aspx?bookid=1088. I want to replace that hyperlink text with "Symptom to Diagnosis: An Evidence-Based Guide, 4e" with the URL accessmedicine.mhmedical.com/book.aspx?bookid=2715. So, I want to find every instance of the old hyperlink and replace it with the new hyperlink. I'm wondering where all that belongs in the code...does it need quotation marks?
Sample solution:
function myFunction() {
var oldLinkText="Symptom to Diagnosis: An Evidence-Based Guide, 3e";
var newLinkText="Symptom to Diagnosis: An Evidence-Based Guide, 4e";
var oldLinkURL="accessmedicine.mhmedical.com/book.aspx?bookid=1088";
var newLinkURL="accessmedicine.mhmedical.com/book.aspx?bookid=2715";
var oldLinkText2="XXX";
var newLinkText2="XXY";
var oldLinkURL2="accessmedicine.XXX";
var newLinkURL2="accessmedicine.XXY";
var files = DriveApp.getFolderById("insert folder ID").getFilesByType(MimeType.GOOGLE_DOCS);
while (files.hasNext()) {
var file = files.next();
if(file){
var doc = DocumentApp.openById(file.getId());
var link=doc.getBody().findText(oldLinkText);
var link2=doc.getBody().findText(oldLinkText2);
if(link){
link=link.getElement().asText();
link.setLinkUrl(newLinkURL);
doc.replaceText(oldLinkText, newLinkText);
}
if(link2){
link2=link2.getElement().asText();
link2.setLinkUrl(newLinkURL2);
doc.replaceText(oldLinkText2, newLinkText2);
}
}
}
Logger.log("Done")
}
来源:https://stackoverflow.com/questions/58491380/replace-hyperlinks-in-multiple-google-docs