I'm trying to write a simple Google Apps Script to automate taking of text from a multipage document and splitting it into many smaller and separate documents. The problem is that the main document is made of all bulleted lists at multiple indents in terms of hierarchy. Here is an example of a test document I'm using:
Here is a list:
Fruit
Apples
Oranges
If I simply open the document by ID, get its body, list items and then re-append it to the bottom of the page using the following code:
function myFunction() {
var body = DocumentApp.getActiveDocument().getBody();
var listItems = body.getListItems();
body.appendHorizontalRule();
body.appendListItem(listItems[0].getText()).setNestingLevel(0).setIndentStart(36).setGlyphType(DocumentApp.GlyphType.BULLET);
body.appendListItem(listItems[1].getText()).setNestingLevel(1).setIndentStart(72).setGlyphType(DocumentApp.GlyphType.HOLLOW_BULLET);
body.appendListItem(listItems[2].getText()).setNestingLevel(1).setIndentStart(72).setGlyphType(DocumentApp.GlyphType.HOLLOW_BULLET);
}
I get this:
Here is a list:
Fruit
Apples
Oranges
Fruit
Apples
Oranges
Close, but the bullet 'Fruit' is turned into a Numbered List instead of a bulleted list. This seems to correct itself if I set all the nesting to 0, in which case, I lose the hierarchy (but keep the bullet at the top level).
I tried this code where I set all the setNestingLevel(0)
to zero. It works but of course it loses indentation:
function myFunction() {
var body = DocumentApp.getActiveDocument().getBody();
var listItems = body.getListItems();
body.appendHorizontalRule();
body.appendListItem(listItems[0].getText()).setNestingLevel(0).setIndentStart(36).setGlyphType(DocumentApp.GlyphType.BULLET);
body.appendListItem(listItems[1].getText()).setNestingLevel(0).setIndentStart(72).setGlyphType(DocumentApp.GlyphType.HOLLOW_BULLET);
body.appendListItem(listItems[2].getText()).setNestingLevel(0).setIndentStart(72).setGlyphType(DocumentApp.GlyphType.HOLLOW_BULLET);
}
Outputs this:
Here is a list:
Fruit
Apples
Oranges
Fruit
Apples
Oranges