Update all List Items in Doc with different List Glyph Apps Script Code Example

This Google Apps Script function, updateListItemsToBullets1, is designed to update all list items in a Google Document to use square bullets. Here is a step-by-step explanation of what each part of the code does:

  1. Open Document by ID:
    • var doc = DocumentApp.openById(DOCID);
    • This line opens a Google Document using its unique identifier (you should replace DOCID with the actual ID of your document).
  2. Get Document Body:
    • var body = doc.getBody();
    • This retrieves the body of the document, which contains all the content (text, lists, images, etc.).
  3. Count of Elements:
    • var numChildren = body.getNumChildren();
    • This counts how many top-level elements (paragraphs, tables, lists, etc.) are in the document’s body.
  4. Track Processed Lists:
    • var processedListIds = {};
    • Initializes an empty object to keep track of which list IDs have been processed. However, in this specific script, this object is initialized but not used later. This could be an artifact from a template or previous version of the code.
  5. Iterate Through Elements:
    • The for loop iterates through each top-level element in the document body.
  6. Check for List Items:
    • if (child.getType() === DocumentApp.ElementType.LIST_ITEM)
    • This checks if the current element is a list item.
  7. Get List Item:
    • var listItem = child.asListItem();
    • If the element is a list item, this line treats (casts) the current element as a list item, allowing list-specific properties and methods to be accessed.
  8. Get List ID (not utilized effectively in this script):
    • var listId = listItem.getListId();
    • Retrieves the unique identifier for the list that the current list item belongs to. In this script, listId is retrieved but not used, which might be a point of confusion or inefficiency.
  9. Set List Item Glyph Type:
    • listItem.setGlyphType(DocumentApp.GlyphType.SQUARE_BULLET);
    • This changes the bullet style of the current list item to a square bullet. This line is the key part of the script where the actual update to each list item happens, applying the square bullet style to each list item iterated over in the document.

In summary, this script is intended to go through all the elements in a Google Document, identify the list items, and change their bullet style to square bullets. However, note that there is a slight inefficiency: the processedListIds object is created but not used, which might have been intended for avoiding processing the same list multiple times, but the actual implementation of that logic is missing in the script. Also, the script updates all list items regardless of their original list type (numbered, bulleted, etc.), which may or may not be the desired behavior depending on the context.

updateListItemsToBullets1() {
  var doc = DocumentApp.openById(DOCID);
  var body = doc.getBody();
  var numChildren = body.getNumChildren();
  // Initialize an object to keep track of which list IDs have been processed
  var processedListIds = {};
  // 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) {
      var listItem = child.asListItem();
      var listId = listItem.getListId();
      listItem.setGlyphType(DocumentApp.GlyphType.SQUARE_BULLET);
    }
  }
}

listItem.setGlyphType.(DocumentApp.GlyphType.SQUARE_BULLET);

Properties

PropertyTypeDescription
BULLETEnumThe default bullet, circular and filled.
HOLLOW_BULLETEnumA hollow bullet.
SQUARE_BULLETEnumA square bullet.
NUMBEREnumA number based bullet.
LATIN_UPPEREnumA latin, uppercase bullet.
LATIN_LOWEREnumA latin, lowercase bullet.
ROMAN_UPPEREnumA roman numeral, uppercase bullet.
ROMAN_LOWEREnumA roman numeral, lowercase bullet.