You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Google Apps Script将Gmail中Zip附件解压存至指定Drive文件夹?

如何用Google Apps Script解压邮件中的Zip附件并保存到指定Google Drive文件夹?

我看到你已经有了处理普通附件(Excel、PDF等)的脚本,但要处理Zip压缩包的话,需要额外添加解压逻辑——毕竟Zip不是单一文件,得先把里面的内容提取出来再保存。下面我来帮你改造代码,实现自动解压邮件中的Zip附件并保存到指定Drive文件夹的功能:

// 请先定义你的目标文件夹,比如通过ID获取
var targetFolder = DriveApp.getFolderById("你的目标文件夹ID");
var root = DriveApp.getRootFolder();

function processEmailAttachments() {
  // 这里可以根据需要修改邮件线程的获取条件,比如获取未读邮件
  var threads = GmailApp.getInboxThreads();
  
  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 attachmentName = attachment.getName();
        var attachmentBlob = attachment.copyBlob();
        
        // 检查是否是支持的类型(包括Zip和普通文件)
        var fileType = checkIfDefinedType_(attachment);
        if(!fileType) continue;
        
        if(fileType === "zip") {
          // 处理Zip压缩包:解压并逐个保存文件
          try {
            var unzippedBlobs = Utilities.unzip(attachmentBlob);
            unzippedBlobs.forEach(function(blob) {
              var fileName = blob.getName();
              // 检查目标文件夹中是否已有同名文件,有则删除
              var existingFile = targetFolder.getFilesByName(fileName);
              if(existingFile.hasNext()) {
                existingFile.next().setTrashed(true);
              }
              // 创建文件并直接保存到目标文件夹(无需先存根目录再移动)
              targetFolder.createFile(blob);
              Logger.log(`已解压并保存:${fileName}`);
            });
          } catch(e) {
            Logger.log(`解压Zip失败:${attachmentName},错误信息:${e}`);
            continue;
          }
        } else {
          // 处理普通文件(Excel、PDF等),沿用你原来的逻辑
          var existingFile = DriveApp.getFilesByName(attachmentName); 
          if (existingFile.hasNext()) { 
            var file = existingFile.next(); 
            file.setTrashed(true); 
          } 
          var file = DriveApp.createFile(attachmentBlob); 
          targetFolder.addFile(file); 
          root.removeFile(file);
          Logger.log(`已保存普通附件:${attachmentName}`);
        }
      }
    }
  }
}

// 更新类型检查函数,添加Zip的MIME类型判断
function checkIfDefinedType_(attachment) {
  var mimeTypes = {
    // 普通文件类型,根据你的需求添加或修改
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "excel",
    "application/pdf": "pdf",
    "image/jpeg": "jpg",
    // Zip压缩包的常见MIME类型
    "application/zip": "zip",
    "application/x-zip-compressed": "zip"
  };
  var mimeType = attachment.getContentType();
  return mimeTypes[mimeType] || null;
}

关键修改说明

  • 更新类型检查逻辑:我扩展了checkIfDefinedType_函数,添加了Zip文件的两种常见MIME类型,确保脚本能准确识别Zip附件。
  • 内置Zip解压:使用Google Apps Script自带的Utilities.unzip()方法解压Zip包,无需额外引入工具,直接获取压缩包内所有文件的Blob对象。
  • 优化文件保存流程:解压后的文件直接创建到目标文件夹,省去了先存根目录再移动的步骤;同时保留了你原有的重名文件处理逻辑——自动删除旧文件再保存新文件。
  • 错误防护:添加try-catch块处理损坏Zip包的解压失败场景,避免脚本中途中断。

注意事项

  • 记得把代码中的"你的目标文件夹ID"替换成你实际的Drive文件夹ID(可从文件夹URL中提取)。
  • 若需要过滤特定邮件(比如只处理带指定标签的邮件),可以修改GmailApp.getInboxThreads()为GmailApp.search("label:你的标签名")。
  • 如果Zip包内包含子文件夹,解压后的文件名会带路径(比如"subfolder/file.txt"),脚本会直接以此为文件名保存;若需要自动创建对应子文件夹,可以额外添加路径解析和文件夹创建的逻辑。

内容的提问来源于stack exchange,提问作者Vidomina Sangre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 10:17:43