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:
- Open the script editor in your Google Docs document.
- Paste the code into the editor.
- Save the project.
- Run the
addNumberedListToDocfunction by clicking the play button or by going to “Run” -> “Run function” -> “addNumberedListToDoc”. - 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.
- The
addNumberedListToDoc()function is declared, which will be used to add a numbered list to the active Google Docs document. var doc = DocumentApp.getActiveDocument()retrieves the active document object.var body = doc.getBody()gets the body of the document.var listItems = ["Item 1", "Item 2", "Item 3"]creates an array calledlistItemsthat contains the text for each list item.- The
forloop is used to iterate through thelistItemsarray. It starts withi = 0and continues untiliis less thanlistItems.length. - Inside the loop,
body.insertListItem(1 + i, listItems[i])is used to insert a new list item at the specified index. The index is1 + ito ensure the items are added in the correct order. The text for the list item islistItems[i]. listItem.setGlyphType(DocumentApp.GlyphType.BULLET)sets the glyph type of the list item toBULLET, 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.listItem.setNestingLevel(0)sets the nesting level of the list item to0, 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:
- Open the script editor in your Google Docs document.
- Paste the code into the editor.
- Save the project.
- Run the
addNumberedListToDocfunction by clicking the play button or by going to “Run” -> “Run function” -> “addNumberedListToDoc”. - 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.