I created a new project and ran the following script. It has deleted my previously existing triggers and even after removing
function createTrigger() {
// Delete all existing triggers to avoid duplicates
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
// Create a time-driven trigger to run the function every Friday at a specific time (e.g., 9:00 AM)
ScriptApp.newTrigger('sendWeeklyEmail')
.timeBased()
.onWeekDay(ScriptApp.WeekDay.FRIDAY)
.atHour(9) // Change the hour as needed
.create();
}
It is still not allowing to add any new triggers.
The error says Loading Data. This may take a few moments. and then Something Went Wrong. Please reload the page to try again
function sendWeeklyEmail() {
var today = new Date();
var dayOfWeek = today.getDay();
// Check if today is Friday (day 5)
if (dayOfWeek !== 5) {
return; // Exit if it's not Friday
}
var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0);
var lastFriday = lastDayOfMonth.getDate() - lastDayOfMonth.getDay() + 5;
// Check if today is the last Friday of the month
if (today.getDate() === lastFriday) {
return; // Don't send email on the last Friday of the month
}
// Format the current month and year as "MMM-YYYY" (e.g., "Mar-2025")
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var sheetName = monthNames[today.getMonth()] + '-' + today.getFullYear();
// Get the Google Sheet data from the current month sheet
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
// If the sheet doesn't exist, return
if (!sheet) {
Logger.log("Sheet for " + sheetName + " not found.");
return;
}
// Assuming the defaulter data is in column A starting from row 2
var dataRange = sheet.getRange('A2:A'); // Adjust the range if needed
var values = dataRange.getValues();
// Calculate the sum of the defaulters' count
var sum = 0;
for (var i = 0; i < values.length; i++) {
sum += values[i][0] || 0;
}
// Define email content
var emailSubject = 'Weekly Defaulter Count for ' + sheetName;
var emailBody = 'Dear Manager,\n\nThe count of defaulters for this week is: ' + sum + '.\n\nPlease review the Google Sheet report to take further action.\n\nBest regards,\nYour Name';
// Set email recipient (Manager) and CC (HR)
var recipient = '[email protected]'; // Change this to your manager's email
var ccEmail = '[email protected]'; // Change this to HR's email address
// Send the email with HR in CC
MailApp.sendEmail({
to: recipient,
cc: ccEmail,
subject: emailSubject,
body: emailBody
});
}
function createTrigger() {
// Delete all existing triggers to avoid duplicates
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
// Create a time-driven trigger to run the function every Friday at a specific time (e.g., 9:00 AM)
ScriptApp.newTrigger('sendWeeklyEmail')
.timeBased()
.onWeekDay(ScriptApp.WeekDay.FRIDAY)
.atHour(9) // Change the hour as needed
.create();
}