【问题标题】:Send email with attachment 'is not a function error'发送带有附件的电子邮件“不是功能错误”
【发布时间】:2021-09-22 02:06:42
【问题描述】:

我希望帮助解决执行此功能时出现的错误。

file.next 不是函数错误

function myFunction() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet_bd = ss.getSheetByName('BD');
var numRows = sheet_bd.getLastRow();
var pdf = sheet_bd.getRange(numRows, 3).getValue();
var file = pdf.slice(33, 66);

//console.log(pdf.slice(33, 66));

     MailApp.sendEmail({
     to: 'email@email.com',   
     subject: "test", 
     body:"Test message",
     attachments: [file.next().getAs(MimeType.PDF)],

  });
  
}

【问题讨论】:

标签: javascript email google-apps-script sendmail email-attachments


【解决方案1】:

你有这个字符串:

"drive.google.com/open?id=1XwCesyVWp-WpYm8pNFkOiH663rNqHx2b"

现在所有这些都是字符串,而不是对象或链接。要获取链接后面的文件需要使用Drive service,获取文件,然后获取文件的blob。您需要 blob,因为 sendEmail 方法需要它。

let id = file.match(/id=.+/)[0].slice(3) // This extracts the id from the url.
let driveFile = DriveApp.getFileById(id)
let blob = driveFile.getBlob()

MailApp.sendEmail({
     to: 'email@email.com',   
     subject: "test", 
     body:"Test message",
     attachments: [blob],
  });

参考

【讨论】:

  • tks for answer ...使用 DriveApp.getFileById 解决
【解决方案2】:

在您的 sn-p 中,file 最终来自 getValue(),它不会返回 iterator,因此您不能使用 next() 方法。

如果您正在获取文件的链接,您需要使用它来加载带有getFileById() 的文件。但要做到这一点,你需要extract just the ID of the file from the URL

基于以上,你可以将你的sn-p修改成这样:

function myFunction() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet_bd = ss.getSheetByName('BD');
  var numRows = sheet_bd.getLastRow();
  var filename = sheet_bd.getRange(numRows, 3).getValue();
  var pdf = DriveApp.getFileById(filename.match(/[-\w]{25,}/))

  MailApp.sendEmail({
    to: 'email@email.com',
    subject: "test",
    body: "Test message",
    attachments: [pdf.getAs(MimeType.PDF)],

  });

}

【讨论】:

猜你喜欢
  • 2013-04-14
  • 1970-01-01
  • 2023-03-10
  • 2013-06-27
  • 2015-09-09
相关资源
最近更新 更多