1
votes

I use a script to update a Google doc template by making a copy of it and editing its contents.

I then want to send that updated copy via email as a PDF attachment. I managed to get the email and the PDF attachment with the correct file name etc., but the contents of the PDF are of the template. When I go to the doc file in google drive, I see that it is updated but for some reason the one that comes in the email isn't.

I even set a sleep timer for up to 5 seconds to see if maybe drive is taking some time to update the changes in the doc.

I tried the same thing with an older document and the contents were updated so I am guessing it's a matter of time. Is there any way for me to force an update on the document when it is created rather than adding a long sleep timer? Do I need to manually save the new doc in the script before emailing it?

1

1 Answers

2
votes

Try using Document.saveAndClose() before attaching.

From the documentation:

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

So it sounds like your changes might be pending before you attach.

https://developers.google.com/apps-script/reference/document/document.html#saveAndClose()

I would also double check you are indeed attaching the copy and not the original, I've made that mistake before.