我需要复制 Google 文档的内容,并将其附加到另一个文档。如果我使用这样的东西:
newDoc.getBody().appendParagraph(template.getText());
...我得到了文本,但丢失了原始文件中的格式。 (Bold, Italic, etc.)
如何将内容和格式复制到新文档中?是否可以将所有内容分配给一个变量,然后将其复制/粘贴到新文档中?
不只使用 1 个变量,您必须迭代文档中的所有元素并一一复制它们。
关于同一主题有多个线程,请尝试以下一个:如何使用 Google Apps 脚本复制文档的一个或多个现有页面 https://stackoverflow.com/questions/19894885/how-to-copy-one-or-more-existing-pages-of-a-document-using-google-apps-script/19907943#19907943
只需仔细阅读代码并添加您应该在文档中遇到的所有内容类型(表格、图像、分页符...)
编辑:这是对这个想法的尝试(首先)
function copyDoc() {
var sourceDoc = DocumentApp.getActiveDocument().getBody();
var targetDoc = DocumentApp.create('CopyOf'+DocumentApp.getActiveDocument().getName());
// var targetDoc = DocumentApp.openById('another doc ID');
var totalElements = sourceDoc.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var body = targetDoc.getBody()
var element = sourceDoc.getChild(j).copy();
var type = element.getType();
if( type == DocumentApp.ElementType.PARAGRAPH ){
body.appendParagraph(element);
}
else if( type == DocumentApp.ElementType.TABLE){
body.appendTable(element);
}
else if( type == DocumentApp.ElementType.LIST_ITEM){
body.appendListItem(element);
}
// ...add other conditions (headers, footers...
}
targetDoc.saveAndClose();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)