GAS Gmail到云端硬盘:将附件的zip文件保存到Google云端硬盘

时间:2020-06-29 13:53:45

标签: google-apps-script google-drive-api gmail email-attachments unzip

我收到一封包含zip文件的电子邮件。如何使用Google Apps脚本解压缩这些文件并将其保存到Google云端硬盘上的特定文件夹?以下代码部分可与简单的excel / pdf文件完美配合。

var root = DriveApp.getRootFolder();
  for(var i in threads){
    var mesgs = threads[i].getMessages();
    for(var j in mesgs){
      var attachments = mesgs[j].getAttachments();
      for(var k in attachments){
        var attachment = attachments[k];
        var isDefinedType = checkIfDefinedType_(attachment);
        if(!isDefinedType) continue;
        var attachmentBlob = attachment.copyBlob();
                
var existingFile = DriveApp.getFilesByName(attachment.getName());
if (existingFile.hasNext()) {
  var file = existingFile.next();
  file.setTrashed(true);
}
var file = DriveApp.createFile(attachmentBlob);
parentFolder.addFile(file);
root.removeFile(file);

1 个答案:

答案 0 :(得分:1)

答案:

您可以使用Utilities.unzip()将文件添加到文件夹。

代码示例:

var attachmentBlob = attachment.copyBlob();
var files = Utilities.unzip(attachmentBlob);

files.forEach(function(file) {
  DriveApp.createFile(file)
  parentFolder.addFile(file);
  root.removeFile(file);
});

我希望这对您有帮助!

参考文献: