2014-10-10 74 views
0

我試圖做的是以下幾點:我想合併三個不同的谷歌文檔的身體(與格式,表,圖像等)。我在網上搜索了幾個小時,但沒有找到我能夠開展工作的解決方案。這裏有人能幫助我嗎?通過谷歌腳本合併三個谷歌文檔的身體

預先感謝您。

最佳,菲爾

回答

0

看到最好answer of this post,代碼是這樣的:

function mergeDocs() { 
    var docIDs = ['list-of','documents','ids','you should have somehow']; 
    var baseDoc = DocumentApp.openById(docIDs[0]); 
    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("According to the doc this type couldn't appear in the body: "+type); 
    } 
    } 
} 

如果某些元素類型不在列表中,您可以輕鬆地添加他們遵循同樣的邏輯。

+0

非常感謝。這個爲我工作! – user1582830 2014-10-10 14:16:19