【问题标题】:GAS Gmail to Drive: save attached zip file to Google DriveGAS Gmail to Drive:将附加的 zip 文件保存到 Google Drive
【发布时间】:2020-06-29 13:53:45
【问题描述】:

我收到了包含 zip 文件的电子邮件。如何使用 Google Apps 脚本将这些文件解压缩并保存到 Google Drive 上的特定文件夹?下面的代码部分与简单的 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);

【问题讨论】:

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


    【解决方案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);
    });
    

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-16
      • 2021-04-02
      • 2022-01-18
      • 2021-02-24
      • 2020-06-18
      相关资源
      最近更新 更多