How do I get a Google Apps Script Document Outline from the API?

Viewed 383

I am able to get the contents of a documents outline by searching all the paragraphs and seeing the heading each paragraph is using. This works but isn't ideal. If I manually remove an item from the outline, it doesn't change the heading of that item to normal. Therefore when I run my code again it'll detect the removed item as still being part of the outline since it's heading matches one used for the outline.

var searchHeading = DocumentApp.ParagraphHeading.HEADING4;
var paragraphs = getParagraphs();
return paragraphs.filter(function(paragraph) {
    return (paragraph.header == searchHeading);
});

function getParagraphs() {
    var paragraphs = DocumentApp.getActiveDocument().getBody().getParagraphs();
    return paragraphs.map(function(p, index) {
        return {
            paragraph: p.getText(),
            header: p.getHeading(),
            id: index
        };
    });
}

Is there anyway to get the contents of the outline without parsing each paragraph and filtering those whose header matches a specific heading, ideally from an API call?

EDIT: Added getParagraphs()

0 Answers
Related