I am trying to generate a draft email on a spreadsheet using Google Scripts. Everything works perfectly fine for generating the PDF (I can see it in my drive folder), and generating the draft email (I can see it in my drafts), but the attachment is not being added to the draft without throwing any errors.
This is the code:
function CreateDraft(invoiceSheet, pdfFile) {
var email = invoiceSheet.getRange(emailRange).getValue().toString();
var subject = invoiceSheet.getRange(subjectRange).getValue().toString();
var body = BuildBodyMessage(invoiceSheet.getRange(bodyRange).getValues());
GmailApp.setCurrentMessageAccessToken(ScriptApp.getOAuthToken());
var gmailDraft = GmailApp.createDraft(email, subject, body, [{
attachments: [pdfFile.getAs(MimeType.PDF)]
}]);
}
What is wrong with it? I have checked many websites and stackoverflow answers and it seems to be correct.
Are there any other ways to generate drafts with attachments without using GmailApp? Maybe some advance service?
SOLUTION: Is in the first comment and it was to remove the square brackets [], so the code ended up like this:
function CreateDraft(invoiceSheet, pdfFile) {
var email = invoiceSheet.getRange(emailRange).getValue().toString();
var subject = invoiceSheet.getRange(subjectRange).getValue().toString();
var body = BuildBodyMessage(invoiceSheet.getRange(bodyRange).getValues());
GmailApp.setCurrentMessageAccessToken(ScriptApp.getOAuthToken());
var gmailDraft = GmailApp.createDraft(email, subject, body, [{
attachments: [pdfFile.getAs(MimeType.PDF)]
}]);
}
question from:
https://stackoverflow.com/questions/65857925/missing-attachment-when-creating-draft-with-google-script 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…