I would like to create a document and then send it using Gmail. Here is my code:
function myFunction() {
var doc = DocumentApp.create('My document');
doc.getBody().appendParagraph('This is the file that i want to receive');
var file = DriveApp.getFileById(doc.getId());
GmailApp.sendEmail('[email protected]', 'Attachment example', 'Please see the attached file.', {attachments: [file.getAs(MimeType.PDF)],name: 'Automatic Emailer Script'
});
}
The file was created in my Google drive with the appended paragraph, the file id in doc.getId() is correct (using Logger.log() function). The problem is I always receive a blank pdf instead. I only could get the correct pdf file when change the code to manually input the file id in the : file = DriveApp.getFileById('1b-XpCUmmn020Vav6psp4aZnenyzyVCm6tcj1-TlvWyU');
What did i do wrong in the code? Thank you very much for your responses!
Save the file before sending it. Also doc and DriveApp.getFileById(doc.getId()) actually refer to the same thing.
function myFunction() {
var doc = DocumentApp.create('My document');
doc.getBody().appendParagraph('This is the file that i want to receive');
doc.saveAndClose();
GmailApp.sendEmail('[email protected]', 'Attachment example', 'Please see the attached file.', {attachments: [doc.getAs(MimeType.PDF)],name: 'Automatic Emailer Script' });
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With