替换多个 Google 文档中的超链接
Replace Hyperlinks in Multiple Google Docs
我输入了 ziganotschka 在 上发布的以下代码:
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")
}
...但我收到此错误消息:
TypeError: Cannot call method "getElement" of null. (line 11, file "Replace Hyperlinks")
我该如何解决这个问题?我必须做什么?
我也只是想从文件夹中提取文件,而不是 Google 驱动器中的所有文件,所以我替换了
var files = DriveApp.getFiles();
和
var files = DriveApp.getFolderById("insert folder ID").getFiles();
那行得通,对吗?
谢谢,
劳伦
第一个问题:
- 您正在遍历驱动器上/文件夹中的所有文件
- 其中一些文件可能不包含旧的 link“http://www.googledoclink1.com”
- 这会引发错误
- 您需要执行一个语句来检查是否包含 link,如果不包含 - 跳转到下一个文件
关于你的第二个问题:
- 是的,您可以使用
var files = DriveApp.getFolderById("insert folder ID").getFiles();
- 您也可以使用
var files = DriveApp.getFolderById("insert folder ID").getFilesByType(MimeType.GOOGLE_DOCS);
仅检索 Google 个文档文件
样本:
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")
}
添加:
处理您的评论
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?
示例解决方案:
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")
}
我输入了 ziganotschka 在 上发布的以下代码:
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")
}
...但我收到此错误消息:
TypeError: Cannot call method "getElement" of null. (line 11, file "Replace Hyperlinks")
我该如何解决这个问题?我必须做什么?
我也只是想从文件夹中提取文件,而不是 Google 驱动器中的所有文件,所以我替换了
var files = DriveApp.getFiles();
和
var files = DriveApp.getFolderById("insert folder ID").getFiles();
那行得通,对吗?
谢谢, 劳伦
第一个问题:
- 您正在遍历驱动器上/文件夹中的所有文件
- 其中一些文件可能不包含旧的 link“http://www.googledoclink1.com”
- 这会引发错误
- 您需要执行一个语句来检查是否包含 link,如果不包含 - 跳转到下一个文件
关于你的第二个问题:
- 是的,您可以使用
var files = DriveApp.getFolderById("insert folder ID").getFiles();
- 您也可以使用
var files = DriveApp.getFolderById("insert folder ID").getFilesByType(MimeType.GOOGLE_DOCS);
仅检索 Google 个文档文件
样本:
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")
}
添加:
处理您的评论
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?
示例解决方案:
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")
}