Creating and Sending sheets in the same function

Google Apps Script apply changes made on Google Sheet in batches:

Scripts commonly need to read in data from a spreadsheet, perform calculations, and then write out the results of the data to a spreadsheet. Google Apps Script already has some built-in optimization, such as using look-ahead caching to retrieve what a script is likely to get and write caching to save what is likely to be set.

Use SpreadsheetApp.flush() to "force" the application of the changes that your script made before sending the sheets by email.

Related

  • Why do we use SpreadsheetApp.flush();?