我想为 Google Drive 制作一个脚本。我想每周备份我的文件夹并将它们存储在 Google 云端硬盘中的另一个文件夹中。
关于每周触发,我已经可以了,但是我遇到了问题,因为我找不到压缩整个文件夹的方法。
我要压缩的文件夹有多个子文件夹和文档。我尝试在每个文件夹中进行搜索并制作文件的 zip 文件,但我发现这很复杂,并且最终为一个文件夹提供了许多 zip 文件。到目前为止我的代码是这样的:
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。我怎样才能解决这个问题?
Thanks!
您可以使用此代码:
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)