使用 Apps 脚本将部分从 Google 文档复制到另一个文档
我已成功使用此代码将一个文档的整个内容复制到另一个文档中:
const newestFile = DocumentApp.openById("ID").getBody();
const archive = DocumentApp.openById("ID").getBody();
let index = 12;
let el, type;
for (let i = 0; i < newestFile.getNumChildren(); i++){
el = newestFile.getChild(i);
type = el.getType();
switch (type){
case DocumentApp.ElementType.PARAGRAPH:
archive.insertParagraph(index,el.copy());
index++;
break;
case DocumentApp.ElementType.LIST_ITEM:
archive.insertListItem(index,el.copy());
index++;
break;
case DocumentApp.ElementType.TABLE:
archive.insertTable(index,el.copy());
index++;
break;
}
}
但是,我现在需要将文档的部分复制到另一个文档中,但我无法弄清楚。如果我知道如何获取任何元素的主体索引,我可以用同样的方式来做,但我不知道这是否可能。我需要复制的文本总是在特定文本(“当前周”)之前,并在特定文本(“存档”)之前立即结束。
I've successfully used this code to copy the entirety of one doc into another doc:
const newestFile = DocumentApp.openById("ID").getBody();
const archive = DocumentApp.openById("ID").getBody();
let index = 12;
let el, type;
for (let i = 0; i < newestFile.getNumChildren(); i++){
el = newestFile.getChild(i);
type = el.getType();
switch (type){
case DocumentApp.ElementType.PARAGRAPH:
archive.insertParagraph(index,el.copy());
index++;
break;
case DocumentApp.ElementType.LIST_ITEM:
archive.insertListItem(index,el.copy());
index++;
break;
case DocumentApp.ElementType.TABLE:
archive.insertTable(index,el.copy());
index++;
break;
}
}
However, I now need to copy a portion of a doc into another doc, and I can't figure it out. If I knew how to get the body index of any element I could do it the same way, but I don't know if that's even possible. The text I need to copy out will always be preceded by a specific text ("Current Week") and end immediatly before a specific text ("ARCHIVE").
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
描述
这是一个如何在某些文本之间进行复制的简单示例。我只介绍了段落和表格,但可以处理任何其他类型的元素。
测试文档
脚本
参考
Description
Here is a simple example of how to copy between certain text. I've only covered paragraphs and tables but any other type of Element can be handled.
Test Document
Script
Reference