我想合并一个文档中的所有文件,所以我尝试了这个功能,我有一个错误>>>服务不可分割:Docs(ligne 19,in" Code" file。我该如何解决?非常感谢:)
function mergeGoogleDocs() {
var folder = DriveApp.getFolderById('1Pd-7GIdm9-PdevjSUr4ZCkYEzSPYWT8U'); // set folder ID were we should look for files to merge
var docIDs = [];
var files = folder.getFiles();
while (files.hasNext()){
file = files.next();
docIDs.push(file.getId());
}
Logger.log(docIDs); // check if we have some ids
var baseDoc = DocumentApp.openById('1G0MPkr5AbgkPpUT6uG5WoJ7RKkIkP_tDWzzns31Q6bU'); // set document id of doc which will contain all merged documents
baseDoc.getBody().clear(); // clear the whole document and start with empty page
var body = baseDoc.getActiveSection();
for (var i = 1; i < docIDs.length; ++i ) {
var otherBody = DocumentApp.openById(docIDs[i]).getActiveSection();
var totalElements = otherBody.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = otherBody.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("Unknown element type: "+type);
}
}
}
编辑:事实上我的错误是我之前没有在我有电子表格的文件夹中检查过所以我必须验证文件的类型MimeType.GOOGLE_DOCS谢谢tehhowch ^^^并且与下面的代码我不再有错误,但最终文件是空的。事实上,对于查看日志,我只有最终合并文件的ID(这是空白的。)如果你有一个想法:)
var types = [MimeType.GOOGLE_DOCS];
for (var t in types) {
var files = DriveApp.getFolderById('1Pd-7GIdm9-PdevjSUr4ZCkYEzSPYWT8U').getFilesByType(types[t]);
while (files.hasNext()) {
var file = files.next();
docIDs.push(file.getId());
Logger.log('File id ' + file.getId());
}
}
最终修改:代码有效!文件空了的原因是因为我已经从我的计算机而不是Google Docs上传了word文档。 :)