function sendDailyFollowUpReminder() { const sheetName = 'Leads'; const recipient = Session.getActiveUser().getEmail(); const sheet = SpreadsheetApp.getActive().getSheetByName(sheetName); if (!sheet) throw new Error('Missing sheet: ' + sheetName); const values = sheet.getDataRange().getValues(); const headers = values.shift(); const index = Object.fromEntries(headers.map((name, i) => [String(name).trim(), i])); const today = new Date(); today.setHours(0, 0, 0, 0); const dueRows = values.filter((row) => { const rawDate = row[index.next_follow_up_date]; if (!rawDate) return false; const followUpDate = new Date(rawDate); followUpDate.setHours(0, 0, 0, 0); const stage = String(row[index.lead_stage] || '').toLowerCase(); return followUpDate <= today && !['closed won', 'closed lost', 'not a fit'].includes(stage); }); if (!dueRows.length) return; const lines = dueRows.map((row) => [ row[index.lead_name], row[index.company], row[index.next_action], row[index.next_follow_up_date], row[index.notes], ].filter(Boolean).join(' | ')); MailApp.sendEmail({ to: recipient, subject: 'Lead follow-ups due today', body: 'Follow up on these leads today:\n\n' + lines.join('\n'), }); }