-
Notifications
You must be signed in to change notification settings - Fork 1
/
Process Smaller Batches of doc split by headers
58 lines (48 loc) · 1.9 KB
/
Process Smaller Batches of doc split by headers
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
function splitDocumentByH1Incremental() {
const docId = "YOUR_DOCUMENT_ID"; // Replace with your Document ID
const folderId = "YOUR_FOLDER_ID"; // Replace with the target folder ID
const scriptProperties = PropertiesService.getScriptProperties();
const startIndex = parseInt(scriptProperties.getProperty("lastProcessedIndex") || "0", 10);
const doc = DocumentApp.openById(docId);
const body = doc.getBody();
const paragraphs = body.getParagraphs();
const targetFolder = DriveApp.getFolderById(folderId);
let newDoc = null, newDocBody = null;
let currentTitle = "";
let inSection = false;
for (let i = startIndex; i < paragraphs.length; i++) {
const paragraph = paragraphs[i];
const style = paragraph.getHeading();
// Check for H1 heading to start a new section
if (style === DocumentApp.ParagraphHeading.HEADING1) {
if (newDoc) {
newDoc.saveAndClose();
const newDocFile = DriveApp.getFileById(newDoc.getId());
targetFolder.addFile(newDocFile);
DriveApp.getRootFolder().removeFile(newDocFile);
}
currentTitle = paragraph.getText();
newDoc = DocumentApp.create(currentTitle);
newDocBody = newDoc.getBody();
inSection = true;
}
if (inSection && newDocBody) {
const copiedElement = paragraph.copy();
newDocBody.appendParagraph(copiedElement.getText())
.setAttributes(paragraph.getAttributes());
}
// Save progress every 50 paragraphs to avoid timeout
if (i % 50 === 0) {
scriptProperties.setProperty("lastProcessedIndex", i);
return; // Stop and allow re-execution
}
}
if (newDoc) {
newDoc.saveAndClose();
const newDocFile = DriveApp.getFileById(newDoc.getId());
targetFolder.addFile(newDocFile);
DriveApp.getRootFolder().removeFile(newDocFile);
}
scriptProperties.deleteProperty("lastProcessedIndex");
Logger.log(`Document split successfully.`);
}