2017-10-16 247 views
0

我有一堆谷歌文檔,我想連接成一個新的新文檔。基本上我想要做的Unix命令相當於:我需要將文本文檔1,2,3連接成4.如何?

貓的1.txt 2.txt 3.txt> 4.txt

我似乎無法找出從做到這一點的最好辦法Apps腳本文檔。會有人碰巧知道嗎?

我試過如下:

// ... code omitted 
var entries = []; 
while (files.hasNext()) { 
    var file = files.next(); 
    var name = file.getName(); 
    if (file.getOwner().getName() != 'My Name') continue 

    var re = /Pattern I am looking for - \d\d/g; 
    if (name.match(re) == null) continue; 

    entries.push({"Name" : name, "File" : file}); 
} 
entries.sort(function(a,b) { return a.Name.localeCompare(b.Name); }); 

// Open the Full.txt file and add each file into it in sequence. 
var fullDoc = DocumentApp.create('Full.txt'); 
entries.forEach(function(e) { 
    var contents = e.File.getAs('text/plain'); 
    // Haven't yet gotten to sticking contents into the target file 
    // because content retrieval itself fails with the message: 
    // "Converting from application/vnd.google-apps.document to text/plain 
    // is not supported. (line 51, file "DocMerge")" 
    return; 
    }); 

感謝。

-av

+0

添加更多關於您的搜索/研究工作的詳細信息。參考[問]。 –

+0

我想試試[DocumentApp](https://developers.google.com/apps-script/reference/document/document-app)[Body Class](https://developers.google.com/apps-script/) (https://developers.google.com/apps-script/reference/document/body#getText())與[DriveApp's](https://開發人員)相關聯,也許可以使用[getText()](https://developers.google.com/apps-script/reference/document/body#getText() google.com/apps-script/reference/drive/drive-app)file [setContent()](https://developers.google.com/apps-script/reference/drive/file#setContent(String)) –

+0

There似乎並不是一種從文件中獲取身體的方法。我擁有的是File對象的列表。 –

回答

0

您需要打開使用DocumentApp的文件,這樣你就可以檢索文檔內容。正如你所看到的,不支持直接轉換爲純文本(儘管這很方便)。

var source_doc = DocumentApp.openById(e.File.getId()); 
var contents = source_doc.getBody().getText(); 

根據你的使用情況,您可能還需要得到頁眉/頁腳部分,你也可以跳過使用的getText(),並使用Body.copy()創建的主體元素的副本,你然後可以插入到其他文檔。這種方法會保留格式。