2017-10-16 30 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()创建的主体元素的副本,你然后可以插入到其他文档。这种方法会保留格式。