0

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.

  • 1
    Possible duplicate of [Why do we use SpreadsheetApp.flush();?](https://stackoverflow.com/questions/41175326/why-do-we-use-spreadsheetapp-flush) – TheMaster Oct 24 '19 at 15:46
  • @TheMaster Isn't `SpreadsheetApp.flush()` only applicable to spreadsheets? OP's code snippet does not reference any google sheets... – TheAddonDepot Oct 24 '19 at 16:49
  • You could put a `copyBody.saveAndClose()` after the `replaceText`. That might be what @TheMaster is getting at. – Cooper Oct 24 '19 at 17:29
  • Great suggestions thanks. Flush won't work as it's not in sheets, but saveAndClose() is doing the job. Thanks very much. – user1412705 Oct 25 '19 at 11:35

1 Answers1

1

Answer:

Use the saveAndClose() method of DocumentApp to force changes before continuing.

More Information:

As per the Apps Script documentation:

Saves the current Document. Causes pending updates to be flushed and applied.

The saveAndClose() method is automatically invoked at the end of script execution for each open editable Document.

A closed Document can't be edited. Use DocumentApp.openById() to reopen a given document for editing.

Implementation:

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)]
    }
  );
}

Splitting the Document and Gmail methods into separate functions also can help with this issue.

References:

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
  • Thanks Rafa. That was what I needed saveAndClose before attaching slides to email, otherwise they were just empty slides. – Boris Gafurov Mar 11 '20 at 15:49