更新的文档未在gmail中附加为pdf

时间:2016-02-24 13:32:23

标签: javascript google-apps-script gmail

我使用Google Apps Script创建doc文件的副本并将数据从电子表格保存到新文件,然后将其作为PDF附件发送到电子邮件。除了pdf文件显示来自源文件而不是更新值的默认值之外,一切都正常工作。这是我的代码,

var lr = dataSheet.getLastRow();
var dataRange = dataSheet.getRange(lr, 1, 1, 3).getValues();

var firstName = dataRange[0][1];
var lastName = dataRange[0][2];
var email = dataRange[0][3];

var emailText = "Hi "+firstName+",\n\nThank you for signing up.";
var emailSubject = "Test PDF";

var fileTemplate = DriveApp.getFileById("FILE_ID");  // Get Template File Id
var fileCopied = fileTemplate.makeCopy("Doc Copy-"+new Date()).getId();  // Make a copy of the template and get the id of the new file.

var doc = DocumentApp.openById(fileCopied);  // Get destination doc
var dbody = doc.getBody();  // get destination doc's body

// Replace the fields with values from sheet.

dbody.replaceText("First Name", firstName);
dbody.replaceText("Last Name", lastName);
dbody.replaceText("EmailAddress", email);

Utilities.sleep(1000);

// Send Email with PDF attachment
MailApp.sendEmail(email, emailSubject, emailText, {
    attachments: [doc.getAs(MimeType.PDF)]
});

如何将邮件中的更新文档作为PDF附件获取?急需这个帮助!感谢。

1 个答案:

答案 0 :(得分:0)

我认为,如果我们将'文件'视为'blob'就可以了,这就是Google处理Google Docs文件的方式。请参阅此示例,该示例将Google Spreadsheets转换并发送为PDF文件。

line1

此处更多内容:Convert and Email Google Spreadsheets as PDF Files