编辑:添加了一个答案,因为编辑会很长(见答案2)
在之前关于文档合并的帖子之后,我最终得到了一个工作脚本(谢谢 Henrique ;)但我仍然有一个小问题:最终的“合并”文档有时包含我想删除的空白页面(取决于其他文档内容) . 我找不到一个简单的方法来做到这一点。脚本是这样的:
function mergeDocs(docIDs) { // parameter docIDs is an array of Doc IDs
var baseDocname = DocumentApp.openById(docIDs[0]).getName();// typical name = IMPRESSION_page_07_07-06-2012__20:57
var modelDoc = DocsList.getFileById(docIDs[0]);
var newmodelName=baseDocname.substr(0,11)+'multipage'+baseDocname.substring(18);
var baseDocId = DocsList.copy(modelDoc,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
var baseDoc = DocumentApp.openById(baseDocId)
var body = baseDoc.getActiveSection();
//
for( var i = 0; i < docIDs.length; ++i ) {
var otherCopy = DocumentApp.openById(docIDs[i]).getActiveSection();
var totalElements = otherCopy.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = otherCopy.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);
else
throw new Error("According to the doc this type couldn't appear in the body: "+type);
}
body.appendPageBreak(); // if content length is too short avoids breaking page layout
}
}
“PageBreak”(有时)会导致空白页,我知道(!),但有必要保持完美的页面布局(我正在使用此文档打印标签)。这是一个典型示例的链接