Google Apps脚本:创建一个新文档并使用Gmail发送 [英] Google apps script: Create a new document and send it using Gmail
问题描述
我想创建一个文档,然后使用Gmail发送.这是我的代码:
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('address@gmail.com', 'Attachment example', 'Please see the attached file.', {attachments: [file.getAs(MimeType.PDF)],name: 'Automatic Emailer Script'
});
}
该文件是在我的Google驱动器中使用附加段落创建的,doc.getId()中的文件ID是正确的(使用Logger.log()函数).问题是我总是收到空白的pdf .更改代码以在手动输入文件ID时,我只能得到正确的pdf文件: 文件= DriveApp.getFileById('1b-XpCUmmn020Vav6psp4aZnenyzyVCm6tcj1-TlvWyU');
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!
推荐答案
保存文件,然后再发送.另外,doc
和DriveApp.getFileById(doc.getId())
实际上是指同一件事.
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('address@gmail.com', 'Attachment example', 'Please see the attached file.', {attachments: [doc.getAs(MimeType.PDF)],name: 'Automatic Emailer Script' });
}
这篇关于Google Apps脚本:创建一个新文档并使用Gmail发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!