将新的 ListItem 附加到 Google 文档 API 中的特定列表

Append new ListItem to particular list in Google Docs API

我正在寻找一种方法将新的 ListItem 附加到 Google 文档 API 中的特定列表。

我的问题是我不知道如何确定列表中的最后一项,所以我只能使用它的索引插入新项目。 我对如何实现它的看法如下:

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

P.S。我做错了什么,也许还有其他方法可以实现追加?

提前致谢!

ListItem 是 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

具有相同列表ID的ListItem属于同一个列表,并相应编号。给定列表的 ListItem 不需要在文档中相邻,甚至不需要具有相同的父元素。属于同一列表的两个项目可以存在于文档中的任何位置,同时保持连续编号,如下例所示:

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);