The Google Apps Script function sendTodayTalk() automates the process of sending out emails for daily Health and Safety talks. Here's a step-by-step explanation of what the code does:
Sheet Initialization:
It first accesses the active Google Spreadsheet and targets a specific sheet named 'Workers Responses'. Data Retrieval:
The script retrieves a list of email addresses, names, dates, topics, and links from specific ranges within the 'Workers Responses' sheet. Today's Date:
The variable today is initialized to the current date with the time set to midnight. Email Sending Loop:
The script enters a loop to go through the dates listed in the spreadsheet. For each date, it checks if it matches the current date (today). If it doesn't, the script continues to the next date (skipping the current iteration). Matching Today's Date:
If a date from the list matches today, the script retrieves the topic and the link associated with that date. Inner Loop - Email Processing:
Another loop starts to go through each email address in the list. It checks if the email address is not empty or just whitespace. If it's empty, the script skips sending to that address. Email Content Creation:
For each valid email, the script constructs a subject line indicating the topic of the Health and Safety talk. It then constructs the body of the email, which greets the recipient by name and provides details about the Health and Safety talk for the day, reminding the recipient to acknowledge by checking a box at the end of a form. The email body also includes the link related to the topic. Sending the Email:
Finally, the script uses the MailApp.sendEmail method to send an email with the constructed subject line and body to the current email address in the loop. Loop Continuation:
The script continues this process for each email address in the list that corresponds to today's date.
To consider:
Authorization and Permissions: The script requires permissions to access the Google Sheets and send emails on your behalf. You must authorize these permissions the first time you run the script.
Correct Spreadsheet Structure: The script assumes a specific data layout in the 'Workers Responses' sheet. The columns for emails, names, dates, topics, and links must be in the expected positions, or the script will not work correctly.
Valid Email Addresses: The email column in the spreadsheet must contain valid email addresses. Invalid or malformed email addresses may cause the script to fail or skip those entries.
Date Formatting: The dates in the spreadsheet must be in a format that the script can interpret correctly. Ensure that the date cells in the spreadsheet are actually formatted as dates and not as plain text or another format.
Time Zone Settings: The script uses the server's time zone to determine "today's" date. Ensure that the script's time zone (found in the project's properties) matches your local time zone or the time zone of the spreadsheet if they need to align.
Quota Limits: Google Apps Script has daily quota limits on how many emails you can send. Make sure not to exceed these limits, or your emails will not be sent after the limit is reached.
Error Handling: The script currently does not include error handling. Consider adding try-catch blocks to manage errors that may occur during the email sending process.
Script Triggers: If you want this script to run automatically every day, you must set up a time-driven trigger through the Google Apps Script editor.