1

In my script I am trying to create a folder, create a date-stamped-document in said folder, create a sub folder, and copy some documents into that sub folder.

All of this works great. When I try to zip the parent folder via either of the methods found here: Creating a zip file inside google drive with apps script - it creates a zip file with a sole PDF file that has the same name as the date-stamped-document. The zipped PDF is blank, and the subfolder isn't there.

Any insight about why this is happening would be great.

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
Community
  • 1
  • 1
Greg
  • 697
  • 1
  • 8
  • 22

2 Answers2

1

The DocsList service has been deprecated so Phil Bozak's previous solution no longer works. However, refer to another SO question for solution that works with DriveApp class.

Community
  • 1
  • 1
Bart
  • 530
  • 5
  • 11
0

This is a great question. It does not seem that the zip function has the ability to zip sub folders. The reason that your script doesn't work is because you only select the files from the folder.

A solution would be to zip each of the subfolders and store that in the one zipped file. You can use the function that I wrote for this.

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");
}

This recursively zips all subfolders. Then you need to create the file with something like

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

I will put in a feature request to allow subfolders to be zipped.

Phil Bozak
  • 2,792
  • 1
  • 19
  • 27
  • It works! I didn't even have to modify the structure. Thanks so much for your help. – Greg Feb 19 '13 at 22:15