通过电子邮件发送Google Doc作为PDF附件
[email protected]经过一段时间的努力后,我呼吁人群提供帮助。通过电子邮件发送Google Doc作为PDF附件
我想将2个Google文档作为PDF附加到电子邮件中。我看到很多关于Google表格的示例,并且已经成功地将表单的副本作为PDF格式发送给我,但我无法将其转到Docs。
在多种不同情况下,PDF附件要么是我的original template Doc的副本,要么是"One Account. All of Google" sign in page的图像捕获。当我查看生成的链接(Logger.log)并使用它们时,会下载the correct Doc的副本。
下面是我尝试创建电子邮件附件的情景示例。请参阅函数emailDocAsPDF()向底部
在此先感谢有关此问题的任何指导。
function myFunctionUpdated() {
var testTemplateId = searchDriveFile('Test Template');
Logger.log('test template id = ' + testTemplateId);
var fileName = 'Copy of Test Template'
Logger.log(fileName);
DriveApp.getFileById(testTemplateId).makeCopy(fileName);
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = ss.getRange(1, 1, 3, 2).getValues();
var copyNewTemplateId = searchDriveFile(fileName);
var copyTemplate = DocumentApp.openById(copyNewTemplateId);
var copyTemplateBody = copyTemplate.getBody().editAsText();
for (i=0; i<range.length; i++) {
copyTemplateBody.replaceText(range[i][0], range[i][1]);
}
copyTemplate.saveAndClose();
emailDocAsPDF(fileName)
}
// Searched Google Drive for a file name and returns the file ID.
function searchDriveFile(fileName) {
var files = DriveApp.searchFiles(
'title = "'+ fileName +'"');
while (files.hasNext()) {
var file = files.next();
var id = file.getId();
return id;
}
}
// Send document in an email as PDF
function emailDocAsPDF(fileName) {
var staticDoc = 'FILE-ID';
var attachmentDoc = UrlFetchApp.fetch("https://docs.google.com/document/d/" + copyTemplateId + "/export?format=pdf");
Logger.log("https://docs.google.com/document/d/" + copyTemplateId + "/export?format=pdf");
var attachmentStaticDoc = UrlFetchApp.fetch("https://docs.google.com/document/d/" + staticDoc + "/export?format=pdf");
Logger.log("https://docs.google.com/document/d/" + staticDoc + "/export?format=pdf");
var fileBlob = [];
fileBlob[0] = attachmentDoc.getBlob().getAs('application/pdf');
fileBlob[1] = attachmentStaticDoc.getBlob().getAs('application/pdf');
var body = "Bird is the WORD!! <br>" +
"<a href='http://www.example.com'>Visit Example</a>";
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail("[email protected]", "Test Documents Email", body, {
htmlBody: body,
attachments:[fileBlob[0],fileBlob[1]]
});
}
编辑 - 成功更新与夹层提供的代码。
// Send document in an email as PDF
function emailDocAsPDF(fileName) {
var staticDoc = 'FILE-ID';
var copyTemplateId = searchDriveFile(fileName);
var blobs = [];
var doc = DriveApp.getFileById(copyTemplateId);
blobs[0] = doc.getBlob().getAs('application/pdf').setName('MyAttachment1.pdf');
var doc = DriveApp.getFileById(staticDoc);
blobs[1] = doc.getBlob().getAs('application/pdf').setName('MyAttachment2.pdf');
var zipBlob = Utilities.zip(blobs).setName('Documents.zip');
var recipient = '[email protected]';
var subject = 'Test Email';
var body = 'Bird is the WORD';
MailApp.sendEmail(recipient, subject, body, {attachments: [zipBlob]});
}
附加任何类型的文档为PDF格式,在文件中调用getBlob
,与.getAs('application/pdf')
和可选设置一个名称与setName
指定内容类型。这就是:
var doc = DriveApp.getFileById('ID_HERE');
var blob = doc.getBlob().getAs('application/pdf').setName('MyAttachment.pdf');
var recipient = '[email protected]';
var subject = 'Email subject';
var body = 'Text here';
MailApp.sendEmail(recipient, subject, body, {attachments: [blob]});
你正在尝试使用“导出为通过URL查询PDF”,这是一个不良记录的功能,有时是表(设定出口区域的边界)是有用的,但不必要的文档,如果它甚至存在的话。上述方法适用于存储在Google云端硬盘中的任何类型的内容。
顺便说一句,在发送电子邮件时,最好使用MailApp
而不是GmailApp
,因为它保持授权级别:发送电子邮件。使用GmailApp
时,脚本还被授予访问和删除所有现有电子邮件的权限,这对于试验,错误和从Internet复制代码构成的脚本不适用。
非常感谢您通过电子邮件发送文档和您对'MailApp' /'GmailApp'的建议的帮助。这一切都完美现在完美! – onerockscott
此外,我忘了提及'copyTemplate.saveAndClose();'在更新模板文档后需要。这使我能够发送更新的文件。 – onerockscott