-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
503 lines (406 loc) · 14.9 KB
/
Code.gs
File metadata and controls
503 lines (406 loc) · 14.9 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
* PDF Downloader for Google Sheets
* Automates batch downloading of PDFs from URLs, with automatic URL detection
* and execution time management for large datasets.
*/
// ============================================================================
// CONFIGURATION CONSTANTS
// ============================================================================
/** Shared Drive folder ID where PDFs will be saved (from folder URL) */
const SHARED_DRIVE_FOLDER_ID = '1In9KsvISrVgIOMGkhbwQOXnyky4qsLmP';
/** Column containing source URLs (wrapper pages or direct PDF links) */
const URL_COLUMN = 'C';
/** Column for extracted/direct PDF URLs */
const PDF_LINK_COLUMN = 'D';
/** Column for Google Drive shareable links */
const DRIVE_LINK_COLUMN = 'E';
/** First row to process (2 = skip header) */
const START_ROW = 2;
/** Max execution time (5 min) before creating continuation trigger */
const MAX_EXECUTION_TIME_MS = 5 * 60 * 1000;
/** Delay (10 sec) before continuation trigger fires */
const CONTINUATION_DELAY_MS = 10 * 1000;
/** Function name for continuation triggers */
const TRIGGER_FUNCTION_NAME = 'downloadPDFs';
/** Property key for storing continuation trigger IDs */
const CONTINUATION_TRIGGER_PROPERTY_KEY = 'PDF_DOWNLOADER_CONTINUATION_TRIGGERS';
/** Property key for storing active sheet name between executions */
const SHEET_NAME_PROPERTY_KEY = 'PDF_DOWNLOADER_ACTIVE_SHEET_NAME';
/**
* Direct mode override (default: false for automatic detection)
* true = treat all URLs as direct PDFs, false = auto-detect URL type
*/
const DIRECT_MODE = false;
// ============================================================================
// MENU SETUP
// ============================================================================
/**
* Creates custom menu when spreadsheet opens.
*/
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('PDF Downloader')
.addItem('Download PDFs', 'downloadPDFs')
.addToUi();
}
// ============================================================================
// MAIN FUNCTION
// ============================================================================
/**
* Main function to download PDFs from URLs in the spreadsheet.
* Supports automatic URL detection, incremental processing, and time-based continuation.
*/
function downloadPDFs() {
const startTime = Date.now();
Logger.log('Starting PDF download process at: ' + new Date(startTime).toISOString());
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = getOrRestoreSheet(spreadsheet);
if (!sheet) return;
const lastRow = sheet.getLastRow();
if (lastRow < START_ROW) {
Logger.log('No URLs to process. Sheet has no data starting from row ' + START_ROW);
cleanupAndExit();
return;
}
const urlColumnIndex = columnLetterToIndex(URL_COLUMN);
const pdfLinkColumnIndex = columnLetterToIndex(PDF_LINK_COLUMN);
const driveLinkColumnIndex = columnLetterToIndex(DRIVE_LINK_COLUMN);
const numRows = lastRow - START_ROW + 1;
const urlValues = sheet.getRange(START_ROW, urlColumnIndex, numRows, 1).getValues();
const driveLinkValues = sheet.getRange(START_ROW, driveLinkColumnIndex, numRows, 1).getValues();
Logger.log('Processing ' + numRows + ' rows starting from row ' + START_ROW);
for (let i = 0; i < urlValues.length; i++) {
if (!shouldContinueProcessing(startTime)) {
logContinuation(START_ROW + i);
createContinuationTrigger();
return;
}
const currentRow = START_ROW + i;
const url = urlValues[i][0];
const existingDriveLink = driveLinkValues[i][0];
if (!url || url.toString().trim() === '') {
Logger.log('Row ' + currentRow + ': Skipping empty URL');
continue;
}
if (existingDriveLink && existingDriveLink.toString().trim() !== '') {
Logger.log('Row ' + currentRow + ': Skipping already processed row');
continue;
}
Logger.log('Row ' + currentRow + ': Processing URL: ' + url);
try {
const pdfUrl = getPDFUrl(url, currentRow, sheet, pdfLinkColumnIndex);
if (!pdfUrl) {
writeError(sheet, currentRow, driveLinkColumnIndex, 'Failed to extract PDF URL from page');
continue;
}
const pdfBlob = downloadPDF(pdfUrl);
if (!pdfBlob) {
writeError(sheet, currentRow, driveLinkColumnIndex, 'Failed to download PDF');
continue;
}
const savedFile = savePDFToSharedDrive(pdfBlob, pdfUrl, SHARED_DRIVE_FOLDER_ID);
if (!savedFile) {
writeError(sheet, currentRow, driveLinkColumnIndex, 'Failed to save PDF to Shared Drive');
continue;
}
const shareableLink = savedFile.getUrl();
sheet.getRange(currentRow, driveLinkColumnIndex).setValue(shareableLink);
SpreadsheetApp.flush();
Logger.log('Row ' + currentRow + ': Successfully saved PDF and updated Drive link');
} catch (error) {
writeError(sheet, currentRow, driveLinkColumnIndex, error.message);
Logger.log('ERROR: Row ' + currentRow + ' - ' + error.message);
}
if (!shouldContinueProcessing(startTime)) {
logContinuation(currentRow);
createContinuationTrigger();
return;
}
}
Logger.log('All rows processed successfully');
cleanupAndExit();
}
/**
* Converts column letter to column index (1-based).
* Example: 'A' -> 1, 'B' -> 2, 'Z' -> 26, 'AA' -> 27
*/
function columnLetterToIndex(letter) {
let column = 0;
const length = letter.length;
for (let i = 0; i < length; i++) {
column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
}
return column;
}
/**
* Gets or restores the active sheet using PropertiesService.
* Returns null if stored sheet name is invalid.
*/
function getOrRestoreSheet(spreadsheet) {
const properties = PropertiesService.getScriptProperties();
const storedSheetName = properties.getProperty(SHEET_NAME_PROPERTY_KEY);
if (storedSheetName) {
Logger.log('Resuming from trigger - retrieving sheet: ' + storedSheetName);
const sheet = spreadsheet.getSheetByName(storedSheetName);
if (!sheet) {
Logger.log('ERROR: Stored sheet "' + storedSheetName + '" not found');
properties.deleteProperty(SHEET_NAME_PROPERTY_KEY);
return null;
}
return sheet;
}
const sheet = spreadsheet.getActiveSheet();
properties.setProperty(SHEET_NAME_PROPERTY_KEY, sheet.getName());
Logger.log('Stored sheet name for continuation: ' + sheet.getName());
return sheet;
}
/**
* Determines the PDF URL from a source URL using automatic detection or DIRECT_MODE.
* Writes the PDF URL to PDF_LINK_COLUMN and returns it.
*/
function getPDFUrl(url, row, sheet, pdfLinkColumnIndex) {
let pdfUrl;
if (DIRECT_MODE) {
pdfUrl = url;
Logger.log('Row ' + row + ': Using direct PDF URL (DIRECT_MODE)');
} else {
// First check for .pdf extension to avoid unnecessary network calls
if (typeof url === 'string' && url.trim().toLowerCase().split('?')[0].split('#')[0].endsWith('.pdf')) {
pdfUrl = url;
Logger.log('Row ' + row + ': Detected direct PDF URL by extension');
} else {
const isDirect = isDirectPDFUrl(url);
if (isDirect) {
pdfUrl = url;
Logger.log('Row ' + row + ': Detected direct PDF URL');
} else {
Logger.log('Row ' + row + ': Detected wrapper page, extracting PDF URL');
pdfUrl = extractPDFUrl(url);
if (pdfUrl) {
Logger.log('Row ' + row + ': Extracted PDF URL: ' + pdfUrl);
} else {
Logger.log('Row ' + row + ': Failed to extract PDF URL from wrapper page: ' + url);
}
}
}
}
if (pdfUrl) {
sheet.getRange(row, pdfLinkColumnIndex).setValue(pdfUrl);
SpreadsheetApp.flush();
}
return pdfUrl;
}
/**
* Writes an error message to the specified cell and logs it.
*/
function writeError(sheet, row, columnIndex, message) {
sheet.getRange(row, columnIndex).setValue('Error: ' + message);
SpreadsheetApp.flush();
Logger.log('ERROR: Row ' + row + ' - ' + message);
}
/**
* Logs continuation information.
*/
function logContinuation(row) {
Logger.log('CONTINUATION: Execution time limit approaching');
Logger.log('CONTINUATION: Stopping at row ' + row + ' to avoid timeout');
Logger.log('CONTINUATION: Creating trigger to resume in ' + (CONTINUATION_DELAY_MS / 1000) + ' seconds');
}
/**
* Cleans up triggers and properties, then exits.
*/
function cleanupAndExit() {
deleteContinuationTriggers();
PropertiesService.getScriptProperties().deleteProperty(SHEET_NAME_PROPERTY_KEY);
Logger.log('Cleanup complete');
}
// ============================================================================
// HELPER FUNCTIONS
// ============================================================================
/**
* Checks if a URL points directly to a PDF file.
* Uses HEAD request to check Content-Type, falls back to .pdf extension check.
*/
function isDirectPDFUrl(url) {
try {
const response = UrlFetchApp.fetch(url, {
method: 'head',
muteHttpExceptions: true,
followRedirects: true,
timeout: 10000
});
// Only check headers for successful (200) responses
if (response.getResponseCode() === 200) {
const headers = response.getHeaders();
const contentType = headers['Content-Type'] || headers['content-type'] || '';
if (contentType.includes('application/pdf')) {
Logger.log('Direct PDF detected via Content-Type');
return true;
}
}
const cleanUrl = url.toLowerCase().split('?')[0].split('#')[0];
if (cleanUrl.endsWith('.pdf')) {
Logger.log('Direct PDF detected via .pdf extension');
return true;
}
return false;
} catch (error) {
Logger.log('Error checking URL type, assuming wrapper page: ' + error.message);
return false;
}
}
/**
* Extracts the PDF URL from an embed wrapper page.
* Parses HTML to find embed tag with type="application/pdf" and extracts src attribute.
*/
function extractPDFUrl(pageUrl) {
try {
const response = UrlFetchApp.fetch(pageUrl, {
muteHttpExceptions: true,
followRedirects: true,
timeout: 30000
});
if (response.getResponseCode() !== 200) {
Logger.log('Failed to fetch page. HTTP status: ' + response.getResponseCode());
return null;
}
const html = response.getContentText();
const embedRegex = /<embed[^>]*type=["']application\/pdf["'][^>]*src=["']([^"']+)["'][^>]*>/i;
const embedRegexAlt = /<embed[^>]*src=["']([^"']+)["'][^>]*type=["']application\/pdf["'][^>]*>/i;
let match = html.match(embedRegex) || html.match(embedRegexAlt);
if (match && match[1]) {
let pdfUrl = match[1];
if (pdfUrl.startsWith('//')) {
pdfUrl = 'https:' + pdfUrl;
}
return pdfUrl;
}
Logger.log('No embed tag with type="application/pdf" found');
return null;
} catch (error) {
Logger.log('Exception while extracting PDF URL: ' + error.message);
return null;
}
}
/**
* Downloads a PDF from the specified URL.
*/
function downloadPDF(url) {
try {
const response = UrlFetchApp.fetch(url, {
muteHttpExceptions: true,
followRedirects: true,
timeout: 30000
});
if (response.getResponseCode() !== 200) {
Logger.log('Failed to download PDF. HTTP status: ' + response.getResponseCode());
return null;
}
const blob = response.getBlob();
const contentType = blob.getContentType();
if (!contentType.includes('pdf')) {
Logger.log('Downloaded content is not a PDF. Content type: ' + contentType);
return null;
}
Logger.log('Successfully downloaded PDF');
return blob;
} catch (error) {
Logger.log('Exception while downloading PDF: ' + error.message);
return null;
}
}
/**
* Saves a PDF blob to the specified Shared Drive folder.
*/
function savePDFToSharedDrive(blob, url, folderId) {
try {
const folder = DriveApp.getFolderById(folderId);
const filename = getFileName(url);
blob.setName(filename);
const file = folder.createFile(blob);
Logger.log('Successfully saved PDF: ' + filename);
return file;
} catch (error) {
Logger.log('Failed to save PDF to Shared Drive: ' + error.message);
return null;
}
}
/**
* Generates a filename for the PDF based on the URL or timestamp.
* Ensures .pdf extension and handles special characters.
*/
function getFileName(url) {
try {
const cleanUrl = url.split('?')[0].split('#')[0];
const urlParts = cleanUrl.split('/');
let filename = urlParts[urlParts.length - 1];
if (!filename || filename.indexOf('.') === -1) {
return 'pdf_' + new Date().getTime() + '.pdf';
}
filename = filename.replace(/[^a-zA-Z0-9._-]/g, '_');
if (!filename.toLowerCase().endsWith('.pdf')) {
const lastDotIndex = filename.lastIndexOf('.');
filename = lastDotIndex > 0
? filename.substring(0, lastDotIndex) + '.pdf'
: filename + '.pdf';
}
return filename;
} catch (error) {
return 'pdf_' + new Date().getTime() + '.pdf';
}
}
// ============================================================================
// TIME MONITORING FUNCTIONS
// ============================================================================
/**
* Checks if the script should continue processing based on elapsed time.
*/
function shouldContinueProcessing(startTime) {
return (Date.now() - startTime) < MAX_EXECUTION_TIME_MS;
}
/**
* Creates a time-based trigger to continue processing after a delay.
*/
function createContinuationTrigger() {
try {
deleteContinuationTriggers();
const trigger = ScriptApp.newTrigger(TRIGGER_FUNCTION_NAME)
.timeBased()
.after(CONTINUATION_DELAY_MS)
.create();
const properties = PropertiesService.getScriptProperties();
const existingTriggers = properties.getProperty(CONTINUATION_TRIGGER_PROPERTY_KEY);
const triggerIds = existingTriggers ? JSON.parse(existingTriggers) : [];
triggerIds.push(trigger.getUniqueId());
properties.setProperty(CONTINUATION_TRIGGER_PROPERTY_KEY, JSON.stringify(triggerIds));
Logger.log('Continuation trigger created. ID: ' + trigger.getUniqueId());
} catch (error) {
Logger.log('Failed to create continuation trigger: ' + error.message);
}
}
/**
* Deletes all continuation triggers created by this script.
*/
function deleteContinuationTriggers() {
try {
const properties = PropertiesService.getScriptProperties();
const storedTriggers = properties.getProperty(CONTINUATION_TRIGGER_PROPERTY_KEY);
if (!storedTriggers) return;
const triggerIds = JSON.parse(storedTriggers);
const allTriggers = ScriptApp.getProjectTriggers();
let deletedCount = 0;
for (let i = 0; i < allTriggers.length; i++) {
const trigger = allTriggers[i];
if (triggerIds.includes(trigger.getUniqueId())) {
ScriptApp.deleteTrigger(trigger);
deletedCount++;
}
}
properties.deleteProperty(CONTINUATION_TRIGGER_PROPERTY_KEY);
if (deletedCount > 0) {
Logger.log('Deleted ' + deletedCount + ' continuation trigger(s)');
}
} catch (error) {
Logger.log('Failed to delete continuation triggers: ' + error.message);
}
}