|
1 | 1 | function exportAllRevisionsById() { |
2 | | - const docId = 'TOKEN'; <--- put here your token, you can find it in the url string, after /d/ |
3 | | - const docName = 'name'; <--- put here the doc's name |
| 2 | + const docId = 'DOCID'; <-- PUT HERE DOC'S ID |
| 3 | + const docName = DriveApp.getFileById(docId).getName(); |
| 4 | + |
4 | 5 | const revisions = Drive.Revisions.list(docId).items; |
5 | 6 | if (!revisions || revisions.length === 0) { |
6 | 7 | Logger.log("No edits found"); |
7 | 8 | return; |
8 | 9 | } |
| 10 | + |
| 11 | + revisions.sort((a, b) => new Date(a.modifiedDate) - new Date(b.modifiedDate)); |
| 12 | + |
9 | 13 | const token = ScriptApp.getOAuthToken(); |
10 | 14 | const folder = DriveApp.createFolder(`${docName}_edits`); |
11 | 15 |
|
| 16 | + let csvRows = []; |
| 17 | + csvRows.push("index,revisionId,modifiedDate,user"); |
| 18 | + |
12 | 19 | revisions.forEach((rev, index) => { |
13 | | - Logger.log(`Exporting revision ${index + 1} id=${rev.id} modifiedDate=${rev.modifiedDate}`); |
14 | | - if (rev.exportLinks && rev.exportLinks['application/vnd.openxmlformats-officedocument.wordprocessingml.document']) { |
15 | | - const url = rev.exportLinks['application/vnd.openxmlformats-officedocument.wordprocessingml.document']; |
16 | | - try { |
17 | | - const response = UrlFetchApp.fetch(url, { |
18 | | - headers: { Authorization: 'Bearer ' + token }, |
19 | | - muteHttpExceptions: true |
20 | | - }); |
21 | | - if (response.getResponseCode() === 200) { |
22 | | - const blob = response.getBlob(); |
23 | | - const user = rev.lastModifyingUserName ? rev.lastModifyingUserName.replace(/\s+/g, '_') : "unknown"; |
24 | | - const filename = `${docName}_revision_${index + 1}_${rev.modifiedDate.replace(/[:\-T]/g, '_').slice(0,19)}_${user}.docx`; |
25 | | - folder.createFile(blob.setName(filename)); |
26 | | - Logger.log(`File saved: ${filename}`); |
27 | | - } else { |
28 | | - Logger.log(`HTTP error ${response.getResponseCode()} for edit ${rev.id}`); |
| 20 | + const dateStr = rev.modifiedDate.replace(/[:\-T]/g, '_').slice(0,19); |
| 21 | + const user = rev.lastModifyingUserName ? rev.lastModifyingUserName.replace(/\s+/g, '_') : "unknown"; |
| 22 | + csvRows.push(`${index+1},${rev.id},${rev.modifiedDate},${user}`); |
| 23 | + |
| 24 | + Logger.log(`Exporting revision ${index + 1} id=${rev.id} modifiedDate=${rev.modifiedDate}`); |
| 25 | + |
| 26 | + if (rev.exportLinks && rev.exportLinks['application/vnd.openxmlformats-officedocument.wordprocessingml.document']) { |
| 27 | + const url = rev.exportLinks['application/vnd.openxmlformats-officedocument.wordprocessingml.document']; |
| 28 | + try { |
| 29 | + const response = UrlFetchApp.fetch(url, { |
| 30 | + headers: { Authorization: 'Bearer ' + token }, |
| 31 | + muteHttpExceptions: true |
| 32 | + }); |
| 33 | + if (response.getResponseCode() === 200) { |
| 34 | + const blob = response.getBlob(); |
| 35 | + const filename = `${docName}_revision_${index + 1}_${dateStr}_${user}.docx`; |
| 36 | + folder.createFile(blob.setName(filename)); |
| 37 | + Logger.log(`File saved: ${filename}`); |
| 38 | + } else { |
| 39 | + Logger.log(`HTTP error ${response.getResponseCode()} for edit ${rev.id}`); |
| 40 | + } |
| 41 | + } catch (e) { |
| 42 | + Logger.log(`Fetch error for edit ${rev.id}: ${e}`); |
29 | 43 | } |
30 | | - } catch (e) { |
31 | | - Logger.log(`Fetch error for edit ${rev.id}: ${e}`); |
| 44 | + Utilities.sleep(7000); // just a sleep to avoid 429 |
| 45 | + } else { |
| 46 | + Logger.log(`None exportLink DOCX for edit ${rev.id}`); |
32 | 47 | } |
33 | | - Utilities.sleep(7000); // just a pause to avoid 429 |
34 | | - } else { |
35 | | - Logger.log(`None exportLink DOCX for edit ${rev.id}`); |
| 48 | + }); |
| 49 | + |
| 50 | + try { |
| 51 | + const csvContent = csvRows.join("\n"); |
| 52 | + const csvBlob = Utilities.newBlob(csvContent, 'text/csv', `${docName}_revisions.csv`); |
| 53 | + folder.createFile(csvBlob); |
| 54 | + Logger.log("CSV file saved successfully"); |
| 55 | + } catch (e) { |
| 56 | + Logger.log("Error creating CSV: " + e); |
36 | 57 | } |
37 | | -}); |
38 | 58 |
|
39 | 59 | Logger.log("Exportation complete. Check your drive folder."); |
40 | 60 | } |
0 commit comments