Convert all list items in a Doc to bullets Detect and find list items in elements Google Apps Script

Key Points:

  • Iterating Over Elements: The script goes through each child of the document body.
  • Checking Element Type: It checks the type of each element to see if it’s a paragraph or list item.
  • Handling List Items: For list items, it demonstrates how to set them to bullet points using setGlyphType(DocumentApp.GlyphType.BULLET). You can customize this section to apply any specific modifications you need for list items.
  • Customizing for Paragraphs: If you want to convert certain paragraphs to list items based on specific criteria (e.g., text pattern), you would include that logic in the section where it checks for paragraphs. This example doesn’t implement that logic directly but provides a placeholder for where you might do so.

This script provides a basic framework for iterating through document elements and modifying list items. You can extend it by adding more sophisticated checks and transformations based on your specific needs, such as converting certain paragraphs to list items or modifying existing list items in a particular way.

his script will specifically target elements that are already list items, but you might want to adjust it depending on your goal (e.g., converting only non-list paragraphs to list items, or adjusting existing list items).

Below is an updated script that iterates through all elements in the document, checks if an element is a list item, and performs actions based on that check. For the purpose of demonstration, I’ll include a basic structure that you can expand upon based on your specific requirements:

function convertListItemsToBullets() {
  const docId = '1K-g3FKh_jQq2ESqgPQ'; // Replace with your Google Doc ID
  const doc = DocumentApp.openById(docId);
  var body = doc.getBody();
  var numChildren = body.getNumChildren();

  for (var i = 0; i < numChildren; i++) {
    var child = body.getChild(i);
    if (child.getType() === DocumentApp.ElementType.PARAGRAPH) {
      var paragraph = child.asParagraph();
      // Here you could check if the paragraph matches certain criteria to be converted into a list item
      // For example, if paragraph starts with "-", you might want to convert it into a bullet list item
      // This example will just demonstrate how to check type, so this part is left as an exercise
    } else if (child.getType() === DocumentApp.ElementType.LIST_ITEM) {
      var listItem = child.asListItem();
      // This is where you can modify list items, for example, ensuring they are bulleted lists
      // Add any other modifications you want to apply to list items here
    // Add more conditions here if you want to handle other element types