繁体   English   中英

Google Apps 脚本上的附件

[英]Attachments on Google Apps Script

当我在 Google Apps 脚本中放置多个附件以通过电子邮件发送时,我遇到了一些问题。

执行此操作的代码部分是

      reportPDF = doc.getAs('application/pdf')
      reportPDF.setName('Attachment1 - '+ rows[0][0] + ".pdf");
      
      var file1 = destinationFolder.createFile(reportPDF);  
      var file2 = DriveApp.getFilesByName("test.pdf");

      DriveApp.getFileById(doc.getId()).setTrashed(true);
      
      emails.forEach(function(email) {
      MailApp.sendEmail(email, "Attachments  - " + rows[0][0], "Hello!", {
                        name: 'Good Practices',
                        attachments: [file.getAs(MimeType.PDF), file2]

    });

但是当我运行它时,我遇到了这个问题:

例外:无效参数:附件(第 151 行,文件“电子邮件”)

我有一个 .doc 文件 1 已填写然后转换为 PDF 和另一个文件 2 已经是 PDF。

当我只使用 file1 运行时,我可以发送 email,但是当我尝试使用 file1 和 file2 时,我遇到了这个错误。 谁能知道可能发生什么?

我运行了很多我在堆栈中阅读的其他建议,但没有一个有效。

解释:

问题是file2不是FILE类型,而是 FileIterator class 的object

另一方面, file1属于FILE类型,这就是它正常工作的原因。

在发送 email 之前检查文件名是否存在也是一个好习惯。

解决方案:

function myFunction() {
  
  reportPDF = doc.getAs('application/pdf')
  reportPDF.setName('Attachment1 - '+ rows[0][0] + ".pdf");

  var file1 = destinationFolder.createFile(reportPDF);  // this is a file
  var folder = DriveApp.getFolderById(folderId); // put here the id of the folder
  var file2 = folder.getFilesByName("test.pdf"); // this is a file iterator

  DriveApp.getFileById(doc.getId()).setTrashed(true);
  
  if (file2.hasNext() ) {
      MailApp.sendEmail(emailAddress, "Attachments  - " + rows[0][0], "Hello!",{
      name: 'Good Practices',                  
      attachments: 
          [
           file1.getAs(MimeType.PDF),
           file2.next().getAs(MimeType.PDF)
          ]    
  })};  
}

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM