我正在尝试复制驱动器中的文档,对其进行编辑并邮寄。基本上就像邮件合并。
我拿一个模板文档,制作一个副本,编辑副本,然后通过电子邮件发送。不幸的是,在电子邮件代码运行之前编辑并未完成,因此电子邮件在进行编辑之前附加了复制的文档。有没有解决的办法?
//make a copy of the template
var templateCopy = DriveApp.getFileById(templateID).makeCopy('newFile', DriveApp.getFolderById(targetFolderID));
//select the contents of the template
var copyBody = DocumentApp.openById(templateCopy.getId())
//replace text: set the date
copyBody.replaceText("%DATE%",'today')
//send email - the email that arrives does not have the date substitution, it still contains the %DATE% tag
GmailApp.sendEmail(targetAddress, 'eggs', 'eggs', {attachments:[copyBody.getAs(MimeType.PDF)]});
关于可能重复的编辑:SpreadsheetApp.flush() 不相关,因为我们没有使用电子表格。
在继续之前使用 的saveAndClose()
方法DocumentApp
强制更改。
根据 Apps 脚本文档:
保存当前
Document
. 导致挂起的更新被刷新和应用。该
saveAndClose()
方法在每个打开的可编辑的脚本执行结束时自动调用Document
。
Document
无法编辑已关闭。使用DocumentApp.openById()
重开一个给定的文档进行编辑。
function documentStuff() {
//make a copy of the template
var templateCopy = DriveApp.getFileById(templateID).makeCopy('newFile',
DriveApp.getFolderById(targetFolderID)
);
//select the contents of the template
var copyBody = DocumentApp.openById(templateCopy.getId());
//replace text: set the date
copyBody.replaceText("%DATE%",'today');
copyBody.saveAndClose();
sendMail(targetAddress, DocumentApp.openById(templateCopy.getId()));
}
function sendMail(targetAddress, file) {
//send email - the email that arrives does not have the date substitution
// it still contains the %DATE% tag
GmailApp.sendEmail(targetAddress, 'eggs', 'eggs', {
attachments: [
file.getAs(MimeType.PDF)]
}
);
}
将 Document 和 Gmail 方法拆分为单独的函数也有助于解决此问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句