如何在 GmailApp 中添加 2 个不同 google 驱动器文件夹的所有内容

How do I add all contents of 2 different google drive folders in GmailApp

我正在尝试将 google 驱动器中 2 个不同文件夹的所有内容添加到我的 GmailApp,以便电子邮件包含这两个文件夹中的所有文件。

示例代码:抛出异常异常:

Invalid argument: attachment at sendEmail(Code:180:20)

         var contents = DriveApp.getFolderById('***').getFiles();

         var contentspdf = DriveApp.getFolderById('***').getFiles();
        
            var attach = [];

            while(contents.hasNext()){

                 var file = contents.next();

                 if (file.getMimeType() == "application/pdf"){

                     attach.push(file);

               }
          }
        
          var attachtwo = [];

          while(contentspdf.hasNext()){

              var files = contentspdf.next();

              if (files.getMimeType() == "application/pdf"){

                 attachtwo.push(files);
             
               }
         }

         GmailApp.sendEmail(currentEmail, Subject, messageBody,{
         
               attachments: [attach, attachtwo,],

               name: 'Test'
         });

您可以使用Spread syntax (...)将所有附件放入attachments参数中:

attachments: [...attach, ...attachtwo]


解法:

 var contents = DriveApp.getFolderById('***').getFiles();

 var contentspdf = DriveApp.getFolderById('***').getFiles();

    var attach = [];

    while(contents.hasNext()){

         var file = contents.next();

         if (file.getMimeType() == "application/pdf"){

             attach.push(file);

       }
  }

  var attachtwo = [];

  while(contentspdf.hasNext()){

      var files = contentspdf.next();

      if (files.getMimeType() == "application/pdf"){

         attachtwo.push(files);
     
       }
 }

 GmailApp.sendEmail(currentEmail, Subject, messageBody,{
 
       attachments: [...attach, ...attachtwo],

       name: 'Test'
 });

请确保 V8 Runtime 环境已启用。