Append new ListItem to particular list in Google Docs API

395 Views Asked by At

I'm looking for a way to append new ListItem to particular list in Google Docs API.

My problem is that I can't figure out how to determine last item in list, so I could just insert new item using it's index. My vision of how to implement this looks like:

body.insertListItem(body.getChildIndex(lastItem) + 1, newItem);

P.S. Am I doing something wrong, maybe there is some other way to implement appending?

Thanks in advance!

1

There are 1 best solutions below

0
On BEST ANSWER

A ListItem is a Paragraph that is associated with a list ID. A ListItem may contain Equation, Footnote, HorizontalRule, InlineDrawing, InlineImage, PageBreak, and Text elements. For more information on document structure, see the guide to extending Google Docs.

ListItems with the same list ID belong to the same list and are numbered accordingly. The ListItems for a given list are not required to be adjacent in the document or even have the same parent element. Two items belonging to the same list may exist anywhere in the document while maintaining consecutive numbering, as the following example illustrates:

var body = DocumentApp.getActiveDocument().getBody();

 // Append a new list item to the body.
 var item1 = body.appendListItem('Item 1');

 // Log the new list item's list ID.
 Logger.log(item1.getListId());

 // Append a table after the list item.
 body.appendTable([
   ['Cell 1', 'Cell 2']
 ]);

 // Append a second list item with the same list ID. The two items are treated as the same list,
 // despite not being consecutive.
 var item2 = body.appendListItem('Item 2');
 item2.setListId(item1);