What I’m trying to do:
I need to split a multi-page Google document into separate documents, one for each page. My plan was to find page breaks and use them as splitting points.
My approach:
I’m trying to parse through the document body to locate PAGE_BREAK elements. When I find one, I want to create a range, copy that section, and make a new Google Doc from it.
The problem:
I made a test document with just one line on page 1 and one line on page 2. When I run my script to parse the document, I can’t find any PAGE_BREAK elements. I thought when text flows to the next page, there would be a PAGE_BREAK element, but it’s not showing up.
Here’s my code:
var currentDoc = DocumentApp.getActiveDocument();
var docBody = currentDoc.getBody();
function clearDocument() {
docBody.clear();
// Only works when I manually add page breaks with script
// docBody.appendParagraph("First page content");
// docBody.appendPageBreak();
// docBody.appendParagraph("Second page content");
}
function startParsing() {
const structure = parseElements(docBody);
Logger.log(structure);
}
function parseElements(elem) {
const nodeData = {
element: elem,
};
if (elem.getNumChildren) {
var totalChildren = elem.getNumChildren();
var childNodes = [];
for (var j = 0; j < totalChildren; j++) {
var currentChild = elem.getChild(j);
var breakFound = searchForBreak(elem);
if(breakFound) {
Logger.log("Located page break at position " + j);
}
var childData = parseElements(currentChild);
Logger.log(currentChild.getType());
childNodes.push(childData);
}
nodeData["children"] = childNodes;
}
return nodeData;
}
function searchForBreak(elem) {
var targetType = DocumentApp.ElementType.PAGE_BREAK;
var foundBreak = docBody.findElement(targetType);
if(foundBreak) {
Logger.log("Page break detected");
return true;
} else {
Logger.log("No page break found");
return false;
}
}
The issue is that natural page breaks (where text flows to the next page) don’t seem to create PAGE_BREAK elements. Only manual page breaks added through the script are detectable.
Has anyone figured out how to handle this? I need to split documents that have natural page breaks, not just manual ones.