Example of Email Sent? column with timestamp |
'getUi' allows us to access the spreadsheet's user-interface so that we can then create the 'alert' popup which displays the customised Yes/No buttons to run the sendEmails function or do nothing. The sendEmails function itself gets all of the sheet data and sets a variable to '0'. This variable is used as a flag to inform the script of any emails that have been sent, to then update the master spreadsheet, otherwise the master spreadsheet would be updated every time the sendEmails function was run (even if no new ones had been sent).
The script then creates a loop to go through each student and checks there is an email address present and an empty Email Sent? column (the latter meaning the function can be ran more than once without repeatedly sending emails to every student). The relevant pieces of data are assigned to variables, after which the construction of the email begins.
When it comes to sending the email I use a try/catch to prevent a script error from an invalid email address. This means the log will catch the error in addition to allowing me to set a variable which decides if a timestamp should be written into the Email Sent? column for that student. The timestamp is formatted via 'formatDate' and informs the administrator when the email was sent for a particular Group, as well as setting the flag variable to '1' to inform new emails have been sent.
With the flag as '1' we 'split' the filename to get the Group ID which can be subsequently searched for in the master spreadsheet and 'setValue' alongside the relevant Group with the same timestamp as above. Remember that there are 36+ Groups in the project I worked on so this master spreadsheet provided a single place for administrators to oversee them all (including checking that emails have been sent for that Group).
No comments:
Post a Comment