Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

javascript - Missing attachment when creating draft with google script

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...