在我的脚本中,我正在尝试创建一个文件夹,在所述文件夹中创建一个带有日期戳的文档,创建一个子文件夹,并将一些文档复制到该子文件夹中。
所有这一切都很有效。当我尝试通过以下任一方法压缩父文件夹时: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
答案 0 :(得分:1)
DocsList服务已被弃用,因此Phil Bozak以前的解决方案不再适用。但是,请参阅another SO question以获取适用于DriveApp类的解决方案。
答案 1 :(得分:0)
这是一个很好的问题。 zip函数似乎没有能力压缩子文件夹。您的脚本不起作用的原因是您只从文件夹中选择文件。
解决方案是压缩每个子文件夹并将其存储在一个压缩文件中。你可以使用我为此写的功能。
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));
我将提交功能请求以允许压缩子文件夹。