I am attempting to copy a document in drive, edit it and mail it. Basically like a mail merge.
I take a template document, make a copy, edit the copy and then email it. Unfortunately the edits are not complete before the email code runs, so the email attaches the copied document before the edits have been made. Is there a way around this?
//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)]});
edit regarding possible duplicate: SpreadsheetApp.flush() is not relevant as we are not working with a spreadsheet.