1

I have a Google Apps Script that, onFormSubmit, copies a template spreadsheet and populates it with values from the form. It then takes the new spreadsheet, converts it to PDF and e-mails it (as a blob attachment) to the submitter.

The problem I am experiencing is, the data in the PDF attachment is somewhat stale (some stuff is correct, other stuff is not). If I open the new spreadsheet directly, everything is correct. I have tried inserting a Utilities.sleep() function into the script to give the spreadsheet more time to populate but to no avail.

I have searched extensively but have been unable to find any answers. Any suggestions are greatly appreciated!

1

1 Answer 1

0

The relevant script method, as said in a comment, is

SpreadsheetApp.flush();

Which makes sure that any pending changes to the spreadsheet are actually applied before the execution continues. For example, to make sure that new data is actually entered (and not just batched, awaiting its CPU time) before the contents are emailed.

Documentation explains:

Spreadsheet operations are sometimes bundled together to improve performance, such as when doing multiple calls to Range.getValue(). However, sometimes you may want to make sure that all pending changes are made right away, for instance to show users data as a script is executing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.