To find list items in a Google Document using Google Apps Script, you can iterate through all the elements in the document, check if an element is a list item, and then perform actions or log information based on your needs. Below is a sample script that demonstrates how to identify list items and log their text content:
function findListItems() {
var document = DocumentApp.getActiveDocument();
var body = document.getBody();
var numChildren = body.getNumChildren();
// Iterate through all elements in the document
for (var i = 0; i < numChildren; i++) {
var child = body.getChild(i);
// Check if the element is a list item
if (child.getType() === DocumentApp.ElementType.LIST_ITEM) {
// Cast the element to a ListItem and log its text
var listItem = child.asListItem();
Logger.log('List Item: ' + listItem.getText());
}
}
// View the log in the Apps Script editor under View > Logs after running the script
}
o use this script:
- Open your Google Document.
- Go to
Extensions
>Apps Script
. - Delete any existing code in the script editor and paste the provided script.
- Save the script with a name you’ll remember.
- Run the function
findListItems
by clicking the play icon or selecting the function name and then clicking the run button. - To see the output, go to
View
>Logs
in the Apps Script editor after the script has run.
This script will log the text of each list item found in the document. You can modify the Logger.log
line inside the if statement to change what information is logged or to perform other actions with each list item.
