2013-02-08 127 views
1

在我的脚本中,我试图创建一个文件夹,在所述文件夹中创建一个带有日期标记的文档,创建一个子文件夹,并将一些文档复制到该子文件夹中。创建zip文件只包含pdf

这一切都很好。当我尝试通过以下任一方法压缩父文件夹时:Creating a zip file inside google drive with apps script - 它创建一个zip文件,其中带有与日期标记文档具有相同名称的唯一PDF文件。压缩的PDF是空白的,子文件夹不在那里。

任何有关为什么会发生这种情况的见解会很好。

var folder = DocsList.createFolder(folderTitle); 
var subFolder = folder.createFolder('Attachments'); 
    subfolder.createFile(attachments[]); //In a loop that creates a file from every 
             //attachment from messages in thread 

var doc = DocumentApp.create(docTitle); //Google Doc 
var docLocation = DocsList.getFileById(doc.getId()); 
    docLocation.addToFolder(folder); 
    docLocation.removeFromFolder(DocsList.getRootFolder()); 
//Everything works fine, I can view file and subfolder, and subfolder's documents 

//This is where the problem is: 
var zippedFolder = DocsList.getFolder(folder.getName()); 
    zippedFolder.createFile(Utilities.zip(zippedFolder.getFiles(), 'newFiles.zip')); 
//this results in a zipped folder containing one blank pdf that has the same title as doc 
+1

你可以发布脚本吗? – UpHelix 2013-02-08 16:02:34

回答

0

这是一个很好的问题。它似乎没有压缩功能有压缩子文件夹的能力。您的脚本无法工作的原因是因为您只从该文件夹中选择文件

解决方法是压缩每个子文件夹并将其存储在一个压缩文件中。你可以使用我为此编写的函数。

function zipFolder(folder) { 
    var zipped_folders = []; 
    var folders = folder.getFolders(); 
    for(var i in folders) 
    zipped_folders.push(zipFolder(folders[i])); 
    return Utilities.zip(folder.getFiles().concat(zipped_folders),folder.getName()+".zip"); 
} 

这将递归拉伸所有子文件夹。然后,你需要创建一些文件类似

DocsList.getFolder("path/to/folder/to/store/zip/file").createFile(zipFolder(folderToZip)); 

我要在功能请求,以允许拉链子文件夹。

+0

它的工作原理!我甚至不必修改结构。非常感谢你的帮助。 – Greg 2013-02-19 22:15:17

+0

你可以在这里找到解决方案:http://stackoverflow.com/a/28018270/3477084 – Hafez 2015-01-19 05:13:08

1

DocsList服务已被弃用,所以Phil Bozak以前的解决方案不再适用。但是,请参阅another SO question,以获取适用于DriveApp类的解决方案。