Google apps script 在1 Zip中压缩多个文件夹-谷歌驱动器脚本

Google apps script 在1 Zip中压缩多个文件夹-谷歌驱动器脚本,google-apps-script,google-api,google-drive-api,Google Apps Script,Google Api,Google Drive Api,我想为谷歌驱动器做一个脚本。我想每周备份我的文件夹,并将它们存储在Google Drive的另一个文件夹中。 关于每周触发,我已经知道了,但我有问题,因为我找不到一种方法来压缩整个文件夹。 我要压缩的文件夹有多个子文件夹和文档。我试着在每个文件夹中搜索并制作一个文件压缩包,但我发现它很复杂,最后一个文件夹中有很多压缩包。到目前为止,我掌握的代码如下: function zipFolder(pathFolder, filename, destinyPath) { var date = Util

我想为谷歌驱动器做一个脚本。我想每周备份我的文件夹,并将它们存储在Google Drive的另一个文件夹中。 关于每周触发,我已经知道了,但我有问题,因为我找不到一种方法来压缩整个文件夹。 我要压缩的文件夹有多个子文件夹和文档。我试着在每个文件夹中搜索并制作一个文件压缩包,但我发现它很复杂,最后一个文件夹中有很多压缩包。到目前为止,我掌握的代码如下:

function zipFolder(pathFolder, filename, destinyPath) {
  var date = Utilities.formatDate(new Date(), "GMT", "ddMMyyyy");
  var destiny = DocsList.getFolder(destinyPath);
  var folder = DriveApp.getFolderById(DocsList.getFolder(pathFolder).getId());
  var zip = Utilities.zip(folder, filename+date+'.zip');
  destiny.createFile(zip);
}
我收到一个错误消息,它不能压缩文件夹,它必须是一个blob。我怎样才能解决这个问题

谢谢

您可以使用以下代码:

function zipFolder(pathFolder, filename, destinyPath) {
  var date = Utilities.formatDate(new Date(), "GMT", "ddMMyyyy");
  var destiny = DocsList.getFolder(destinyPath);
  var folder = DriveApp.getFolderById(DocsList.getFolder(pathFolder).getId());
  var zip = Utilities.zip(getBlobsPath(folder, ''), filename+date+'.zip');
  destiny.createFile(zip);
}

function getBlobsPath(reFolder, path) {
  var blobs = [];
  var files = reFolder.getFiles();
  while (files.hasNext()) {
    var file = files.next().getBlob();
    file.setName(path+file.getName());
    blobs.push(file);
  }
  var folders = reFolder.getFolders();
  while (folders.hasNext()) {
    var folder = folders.next();
    var fPath = path+folder.getName()+'/';
    blobs.push(Utilities.newBlob([]).setName(fPath)); //comment/uncomment this line to skip/include empty folders
    blobs = blobs.concat(getBlobsPath(folder, fPath));
  }
  return blobs;
}