替换多个Google文档中的超链接 [英] Replace Hyperlinks in Multiple Google Docs
问题描述
我输入了ziganotschka在 https://stackoverflow.com/a/57865441/12252776 上发布的以下代码a>:
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")
}
...但是我收到此错误消息:
TypeError: Cannot call method "getElement" of null. (line 11, file "Replace Hyperlinks")
我该如何解决?我该怎么办?
How do I fix this? What do I have to do?
我也只想从文件夹中提取文件,而不是从Google云端硬盘中提取所有文件,所以我替换了
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();
与
var files = DriveApp.getFolderById("insert folder ID").getFiles();
那行得通,对吧?
谢谢你, 劳伦
推荐答案
第一个问题:
- 您要遍历驱动器中/文件夹中的所有文件
- 其中一些文件可能不包含旧链接" http://www.googledoclink1.com "
- 这会引发错误
- 您需要实施一条语句来检查链接是否包含,如果没有,请跳转到下一个文件
- 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
第二个问题:
- 是的,您可以使用
var files = DriveApp.getFolderById("insert folder ID").getFiles();
- 您还可以使用
var files = DriveApp.getFolderById("insert folder ID").getFilesByType(MimeType.GOOGLE_DOCS);
仅检索Google Docs文件
- 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
示例:
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")
}
添加:
解决您的评论
示例:假设我有超链接文本:诊断症状: 带有网址的循证指南3e" accessmedicine.mhmedical.com/book.aspx?bookid=1088.我要更换 带有诊断症状:基于证据"的超链接文本 指南,带有网址的4e" accessmedicine.mhmedical.com/book.aspx?bookid=2715.所以,我想找到 旧的超链接的每个实例,然后用新的替换 超链接.我想知道代码中的所有内容都在哪里... 需要引号吗?
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")
}
这篇关于替换多个Google文档中的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!