-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle-apps-script.js
More file actions
264 lines (220 loc) · 7.96 KB
/
Copy pathgoogle-apps-script.js
File metadata and controls
264 lines (220 loc) · 7.96 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* learnif. Newsletter Automation
* Google Apps Script for automated newsletter sending
*
* Features:
* - Sends emails every hour starting from 11:11 AM
* - Sends to batches of 45 subscribers
* - Clears "sent" status at 11:59 PM daily
*/
// Configuration
const CONFIG = {
API_URL: 'https://learnif.16xstudios.space/api/newsletter', // Your API endpoint
BATCH_SIZE: 45, // Number of emails per batch
SPREADSHEET_ID: '1V7RKKDE-RiVH_9mTIgbOTpPf9JsgQI0iY2yzRWq5-lc', // Your Google Sheet ID
LEARNCODE_COLUMN: 'D', // Column D for learncode
};
/**
* Main function to trigger from time-based trigger
*/
function sendNewsletterBatch() {
Logger.log('========================================');
Logger.log('[NEWSLETTER] Starting batch send process');
Logger.log(`[NEWSLETTER] Time: ${new Date()}`);
Logger.log('========================================');
try {
// Call the API
const response = UrlFetchApp.fetch(CONFIG.API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
payload: JSON.stringify({
batchSize: CONFIG.BATCH_SIZE
}),
muteHttpExceptions: true // Don't throw on non-2xx responses
});
const statusCode = response.getResponseCode();
const responseText = response.getContentText();
Logger.log(`[NEWSLETTER] API Response Code: ${statusCode}`);
Logger.log(`[NEWSLETTER] API Response: ${responseText}`);
if (statusCode === 200) {
const result = JSON.parse(responseText);
Logger.log(`[NEWSLETTER] ✓ Success: ${result.message || 'Emails sent successfully'}`);
Logger.log(`[NEWSLETTER] Sent: ${result.sent || 0}, Failed: ${result.failed || 0}`);
} else {
Logger.log(`[NEWSLETTER] ✗ Error: API returned status ${statusCode}`);
Logger.log(`[NEWSLETTER] Response: ${responseText}`);
}
} catch (error) {
Logger.log(`[NEWSLETTER] ✗ Fatal error: ${error.toString()}`);
Logger.log(`[NEWSLETTER] Stack trace: ${error.stack}`);
}
Logger.log('========================================');
Logger.log('[NEWSLETTER] Process completed');
Logger.log('========================================\n');
}
/**
* Clear all "sent" values in the learncode column
* Should be triggered at 11:59 PM daily
*/
function clearLearncodeColumn() {
Logger.log('========================================');
Logger.log('[CLEAR] Starting clear process');
Logger.log(`[CLEAR] Time: ${new Date()}`);
Logger.log('========================================');
try {
const spreadsheet = SpreadsheetApp.openById(CONFIG.SPREADSHEET_ID);
const sheet = spreadsheet.getActiveSheet();
if (!sheet) {
Logger.log('[CLEAR] ✗ No active sheet found');
return;
}
// Get the range for column D (learncode)
const lastRow = sheet.getLastRow();
if (lastRow <= 1) {
Logger.log('[CLEAR] No data rows to clear');
return;
}
// Get all values in column D
const range = sheet.getRange(2, 4, lastRow - 1, 1); // Start from row 2, column D
const values = range.getValues();
let clearedCount = 0;
const newValues = [];
// Clear "sent" values
for (let i = 0; i < values.length; i++) {
if (values[i][0] === 'sent') {
newValues.push(['']); // Empty string instead of sent
clearedCount++;
} else {
newValues.push([values[i][0]]); // Keep other values as is
}
}
// Update the sheet
if (newValues.length > 0) {
range.setValues(newValues);
Logger.log(`[CLEAR] ✓ Cleared ${clearedCount} "sent" values`);
} else {
Logger.log('[CLEAR] No "sent" values to clear');
}
} catch (error) {
Logger.log(`[CLEAR] ✗ Fatal error: ${error.toString()}`);
Logger.log(`[CLEAR] Stack trace: ${error.stack}`);
}
Logger.log('========================================');
Logger.log('[CLEAR] Process completed');
Logger.log('========================================\n');
}
/**
* Setup all time-based triggers
* Run this once to set up the automation
*/
function setupTriggers() {
Logger.log('========================================');
Logger.log('[SETUP] Setting up triggers');
Logger.log('========================================');
try {
// Delete existing triggers first
const triggers = ScriptApp.getProjectTriggers();
triggers.forEach(trigger => {
if (trigger.getHandlerFunction() === 'sendNewsletterBatch' ||
trigger.getHandlerFunction() === 'clearLearncodeColumn') {
ScriptApp.deleteTrigger(trigger);
Logger.log(`[SETUP] Deleted existing trigger: ${trigger.getHandlerFunction()}`);
}
});
// Create hourly trigger starting at 11:11 AM
// We'll create triggers for each hour: 11:11, 12:12, 13:13, etc.
const hours = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]; // 11 AM to 10 PM
hours.forEach((hour, index) => {
const minute = 11; // All at :11 minutes
const trigger = ScriptApp.newTrigger('sendNewsletterBatch')
.timeBased()
.everyDays(1)
.atHour(hour)
.nearMinute(minute)
.create();
Logger.log(`[SETUP] ✓ Created trigger for ${hour}:${String(minute).padStart(2, '0')} (index: ${index})`);
});
// Create trigger to clear learncode at 11:59 PM daily
const clearTrigger = ScriptApp.newTrigger('clearLearncodeColumn')
.timeBased()
.everyDays(1)
.atHour(23)
.nearMinute(59)
.create();
Logger.log('[SETUP] ✓ Created trigger to clear learncode at 23:59');
Logger.log('========================================');
Logger.log('[SETUP] All triggers set up successfully!');
Logger.log('========================================');
// Show summary
Logger.log('\n📧 Newsletter will be sent at:');
hours.forEach(hour => {
Logger.log(` - ${hour}:11`);
});
Logger.log('\n🗑️ learncode column will be cleared at:');
Logger.log(' - 23:59 (11:59 PM)');
} catch (error) {
Logger.log(`[SETUP] ✗ Fatal error: ${error.toString()}`);
Logger.log(`[SETUP] Stack trace: ${error.stack}`);
}
}
/**
* Manual test function
* Use this to test the newsletter sending manually
*/
function manualTest() {
Logger.log('========================================');
Logger.log('[MANUAL TEST] Running manual test');
Logger.log('========================================');
sendNewsletterBatch();
}
/**
* Test clear function manually
*/
function manualTestClear() {
Logger.log('========================================');
Logger.log('[MANUAL TEST] Running clear test');
Logger.log('========================================');
clearLearncodeColumn();
}
/**
* List all current triggers
*/
function listTriggers() {
Logger.log('========================================');
Logger.log('[TRIGGERS] Current triggers in project');
Logger.log('========================================');
const triggers = ScriptApp.getProjectTriggers();
if (triggers.length === 0) {
Logger.log('No triggers found.');
} else {
triggers.forEach((trigger, index) => {
Logger.log(`\nTrigger ${index + 1}:`);
Logger.log(` Function: ${trigger.getHandlerFunction()}`);
if (trigger.getTriggerSourceId()) {
const source = trigger.getTriggerSource();
Logger.log(` Source: ${source}`);
}
});
}
Logger.log('========================================');
}
/**
* Delete all triggers
* Use this if you want to stop the automation
*/
function deleteAllTriggers() {
Logger.log('========================================');
Logger.log('[DELETE] Deleting all triggers');
Logger.log('========================================');
const triggers = ScriptApp.getProjectTriggers();
let deletedCount = 0;
triggers.forEach(trigger => {
ScriptApp.deleteTrigger(trigger);
deletedCount++;
Logger.log(`Deleted trigger: ${trigger.getHandlerFunction()}`);
});
Logger.log(`\n✓ Deleted ${deletedCount} trigger(s)`);
Logger.log('========================================');
}