function sendApprovedEmails() { const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data"); // Change if your sheet name differs const data = sheet.getDataRange().getValues(); const now = new Date();
for (let i = 1; i < data.length; i++) { const [to, subject, body, cc, approval, sentFlag] = data[i];
// Skip if not approved or already sent
if (approval !== "Approved" || sentFlag === "Sent") continue;
// Send email
try {
GmailApp.sendEmail(to, subject, body, {
cc: cc || "",
});
sheet.getRange(i + 1, 6).setValue("Sent"); // Mark as Sent in Column F
} catch (error) {
Logger.log("Error sending email for row " + (i + 1) + ": " + error);
}
// Schedule the next email in 5 minutes using a trigger
if (i + 1 < data.length) {
ScriptApp.newTrigger("sendApprovedEmails")
.timeBased()
.after(5 * 60 * 1000) // 5 minutes
.create();
break; // Exit loop to prevent multiple emails in one run
}
} }
I need to change in the above scrept