Apps Script Doc Lists

o add numbered list items to a Google Docs document using Apps Script, you can use the setListId() and setNestingLevel() methods of the ParagraphHeading element. Here’s an example that demonstrates how to add numbered list items:

function addNumberedListToDoc() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  // Create a numbered list
  var listItems = ["Item 1", "Item 2", "Item 3"];
  // Iterate through the list items and add them to the document
  for (var i = 0; i < listItems.length; i++) {
    const listItem = body.insertListItem(1 + i, listItems[i]);
      listItem.setGlyphType(DocumentApp.GlyphType.BULLET);
      listItem.setNestingLevel(0);
      }
}

In the code above, the addNumberedListToDoc() function adds a numbered list to the active Google Docs document. The listItems array contains the text for each list item.

The code uses a for loop to iterate through the listItems array. For each item, a new paragraph is created using body.appendParagraph(listItem). The setNestingLevel() method sets the nesting level of the list item, with a value of 0 indicating the top level.

To use this code:

  1. Open the script editor in your Google Docs document.
  2. Paste the code into the editor.
  3. Save the project.
  4. Run the addNumberedListToDoc function by clicking the play button or by going to “Run” -> “Run function” -> “addNumberedListToDoc”.
  5. Check your document, and you will see the numbered list items added.

Please note that the code assumes that you are running it from a Google Docs document and will add the numbered list items to that same document. Adjust the code as needed if you want to add the list items to a different document.

  1. The addNumberedListToDoc() function is declared, which will be used to add a numbered list to the active Google Docs document.
  2. var doc = DocumentApp.getActiveDocument() retrieves the active document object.
  3. var body = doc.getBody() gets the body of the document.
  4. var listItems = ["Item 1", "Item 2", "Item 3"] creates an array called listItems that contains the text for each list item.
  5. The for loop is used to iterate through the listItems array. It starts with i = 0 and continues until i is less than listItems.length.
  6. Inside the loop, body.insertListItem(1 + i, listItems[i]) is used to insert a new list item at the specified index. The index is 1 + i to ensure the items are added in the correct order. The text for the list item is listItems[i].
  7. listItem.setGlyphType(DocumentApp.GlyphType.BULLET) sets the glyph type of the list item to BULLET, which changes it from a numbered list to a bulleted list. If you want to keep it as a numbered list, you can remove this line.
  8. listItem.setNestingLevel(0) sets the nesting level of the list item to 0, indicating that it is at the top level of the list. If you want to create nested lists, you can modify the nesting level accordingly.

To use this code:

  1. Open the script editor in your Google Docs document.
  2. Paste the code into the editor.
  3. Save the project.
  4. Run the addNumberedListToDoc function by clicking the play button or by going to “Run” -> “Run function” -> “addNumberedListToDoc”.
  5. Check your document, and you will see the numbered list items or bulleted list items added, depending on whether you keep or remove the listItem.setGlyphType() line.

Note: The code assumes that you are running it from a Google Docs document and will add the numbered/bulleted list items to that same document. Modify the code as needed if you want to add the list items to a different document.