-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
73 lines (59 loc) · 2.07 KB
/
Copy pathCode.gs
File metadata and controls
73 lines (59 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function sendExpiryAlerts() {
const config = {
sheetNames: ['Food', 'Medicine'],
thresholdDays: 30,
recipient: Session.getActiveUser().getEmail(),
dateHeader: 'Expiry Date',
nameHeader: 'Item Name'
};
const ss = SpreadsheetApp.getActiveSpreadsheet();
const now = new Date();
const MS_PER_DAY = 24 * 60 * 60 * 1000;
const sections = [];
config.sheetNames.forEach(sheetName => {
const sheet = ss.getSheetByName(sheetName);
if (!sheet) return;
const data = sheet.getDataRange().getValues();
if (!data.length) return;
const headers = data[0].map(h => String(h).trim());
const nameIdx = headers.indexOf(config.nameHeader);
const expiryIdx = headers.indexOf(config.dateHeader);
if (nameIdx === -1 || expiryIdx === -1) return;
const lines = [];
for (let i = 1; i < data.length; i++) {
const itemName = data[i][nameIdx];
const rawDate = data[i][expiryIdx];
if (!itemName || !rawDate) continue;
const expiryDate = rawDate instanceof Date ? rawDate : new Date(rawDate);
if (isNaN(expiryDate)) continue;
const daysToExpire = Math.floor((expiryDate - now) / MS_PER_DAY);
if (daysToExpire >= 0 && daysToExpire <= config.thresholdDays) {
lines.push(`- ${itemName} (Expires: ${expiryDate.toDateString()} • in ${daysToExpire} day${daysToExpire !== 1 ? 's' : ''})`);
}
}
if (lines.length) {
sections.push(`**${sheetName} items expiring within ${config.thresholdDays} days:**\n${lines.join('\n')}`);
}
});
if (!sections.length) {
// Nothing to send today
return;
}
const message = sections.join('\n\n');
MailApp.sendEmail({
to: config.recipient,
subject: `Items Expiring Soon (within ${config.thresholdDays} days)`,
body: message
});
}
/**
* Optional helper to schedule a daily email at ~08:00.
* Run once to create the trigger, or configure via UI.
*/
function createDailyTrigger() {
ScriptApp.newTrigger('sendExpiryAlerts')
.timeBased()
.atHour(8) // Adjust to your preferred hour (0–23)
.everyDays(1)
.create();
}