It seems pretty cumbersome, Lists are made up of ListItems but there doesn't seem to be a List object. There is a ListId, but it doesn't seem to have any real function other than you can split your list up and continue the numbers. You seem to only be able to add a ListItem to the body, but there doesn't seem to be an easy way to get the index for a ListItem so that you can append after it.
I wrote a function that will iterate all the items in the body to find a ListItem with some placeholder text and return its index.
function findListItemWithText(text) {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var index = -1;
for (var i=0; i<body.getNumChildren(); i++) {
var child = body.getChild(i);
if (child.getType() == DocumentApp.ElementType.LIST_ITEM) {
var listItem = child.asListItem();
if (listItem.getText() == text) {
index = i;
}
}
}
return index;
}
I then wrote a function that will replace a ListItem with the elements in an array:
function replaceListItem (placeholder, list) {
var index = findListItemWithText(placeholder);
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var listItem = body.getChild(index).asListItem();
// replace the text in the placeholder ListItem
listItem.setText(list[0]);
// append the rest of the list after the placeholder ListItem
for (var i=1; i<list.length; i++) {
body.insertListItem(index + i, list[i]);
}
}
You can then call this function with a placeholder and a list and if there is a ListItem with this text it will append ListItems the list at that point.
replaceListItem("{{list}}", ["One", "Two", "Three"]);
It works for numbered lists and bulleted ones. If there are two or more placeholders it will just replace the last one.
If anyone can critique this I'd be interested in hearing about more efficient ways to find and manipulate a given element as it seems like my solution is a lot more effort than I would have expected.