【问题标题】:Grabbing multiple attachments with a specific file name in Google Appscript在 Google Appscript 中抓取具有特定文件名的多个附件
【发布时间】:2021-01-17 18:13:28
【问题描述】:

在添加“n”个附件方面需要帮助。

代码如下:

    for(var i = 2; i<=lr; i++)
    {
          var Contact_Person = ss.getRange(i,4).getValue();

          var PDFs = ss.getRange(i,6).getValue();

          //The above i am grabbing the PDF name from a column in My sheet.

          //This name is used for generating my Mail Merge File.
    
          var contents = DriveApp.getFolderById('1ouiQE2cERaRMC_rnSftJCqQCpr2B4x3D').getFiles();
             
          PDFs = DriveApp.getFilesByName(Contractor_Name);
          if(PDFs.hasNext())
          {

              var attach = [];

              while(contents.hasNext())
              {

                   var file = contents.next();

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

                       attach.push(file);

                   }

             }
           
  
        GmailApp.sendEmail(currentEmail, Subject, messageBody,
        {

          attachments: [PDFs.next().getAs(MimeType.PDF), attach[0],attach[1],attach[2], attach[3],],

          name: 'Test'

        });
     }

//This section is for me to grab 1 attachment according to the name specified for that row matching

//the Merged file in the google drive folder.

//I am also trying to attach from a separate folder N number of files to this email.

Pdfs 是一个变量,用于保存从我的电子表格中的第 6 列获取值。

每一行的这一列都有一组名称。

当程序运行时,它会执行邮件合并,然后将电子邮件发送给特定行的用户。

例子:

Email           Subject  Reference No.  Contact Person  Contractor Name   PDFs
***@gmail.com    Test    XXXXX          Mr. Dan         XYZ Company       XYZ Company

【问题讨论】:

    标签: google-apps-script google-sheets docx-mailmerge


    【解决方案1】:

    答案:

    您可以将它们全部推送到同一个数组中。

    更多信息:

    来自GmailApp.sendEmail(recipient, subject, body, options)上的文档:

    高级参数

    attachments BlobSource[] 与电子邮件一起发送的文件数组

    因此您可以将它们全部附加到一个数组中。

    代码片段:

    if (PDFs.hasNext()) {
      var attach = [];
      attach.push(PDFs.next().getAs(MimeType.PDF));
    
      while (contents.hasNext()) {
        var file = contents.next();
        if (file.getMimeType() == "application/pdf") {
          attach.push(file);
        }
      }
    
      GmailApp.sendEmail(currentEmail, Subject, messageBody, {
        attachments: attach,
        name: "Test"
      });
    }
    

    参考资料:

    【讨论】:

    • 您好,感谢您的建议。我不想获取所有内容的原因是因为我也为其他用户使用相同的文件夹。这意味着 PDF 变量用于识别确切的用户,然后从不同的文件夹中获取额外的附件以发送电子邮件到个人用户。
    • @KingMichaelKing 你知道'N'是什么吗?
    • 我不太确定你的意思。这会将文件夹PDFs 中的第一个文件和attach 中的所有文件附加到电子邮件中。如果你只想从attach获取N个文件,你怎么知道N是什么?
    • 嗨 - PDFs 实际上是一个存储用户名的变量(第 6 列)。 var PDFs = ss.getRange(i,6).getValue();因此,一旦用户的名称存储在可变 PDF 中,我就会选择与存储在可变 PDF 中的名称匹配的文件。 PDFs = DriveApp.getFilesByName(Contractor_Name);所以这个 N 是由 PDFs 变量决定的。也许我做错了..如果有更好的方法,请告知。 1. 文件夹 1 = 保存不同用户的邮件合并文件,其名称用于命名文件。 2. 文件夹 2 = 保存要附加到所有用户的文件
    • PDFs = DriveApp.getFilesByName(Contractor_Name); PDFs 是Files 的列表。您能否更新您的问题以澄清变量,以便我再看一下?
    猜你喜欢
    • 1970-01-01
    • 2011-01-21
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 2021-10-14
    相关资源
    最近更新 更多