Adjust line indentation with apps script in Docs


To adjust the indentation of list items properly in a Google Document using Apps Script, it’s essential to use the correct properties and methods. Unfortunately, the setIndentStart and setIndentFirstLine methods I mentioned earlier do not exist in the Google Apps Script Document service. For list items, you can adjust the indentation level via the setGlyphType method to control the list’s appearance, although this method primarily changes the list bullet or numbering style rather than indentation. Direct control over indentation like you would with normal text isn’t as straightforward for list items.

If the goal is to adjust list items to have a specific indentation appearance, you might consider adjusting the list format to a different type that visually meets your requirements or manipulating the paragraph indentation for non-list items. However, if we’re focusing on list items specifically, we’re limited in direct indentation control.

For list items, a common approach is to adjust their nesting level to change their indentation. While this doesn’t directly set indentation in points or inches, it can be used to visually indent list items further or bring them closer to the margin. Below is an example of how you might attempt to adjust list formatting indirectly:

This script sets the nesting level of all list items to 0, ensuring they are at the least indented level possible within the list structure. Keep in mind, this approach works within the constraints of Google Docs’ handling of lists and doesn’t provide pixel-perfect control over indentation.

function adjustListIndentation() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var numElements = body.getNumChildren();

  for (var i = 0; i < numElements; i++) {
    var element = body.getChild(i);
    if (element.getType() === DocumentApp.ElementType.LIST_ITEM) {
      var listItem = element.asListItem();
      // Example of adjusting list item appearance
      // This doesn't directly adjust indentation but can change the list level or type
      // For demonstration, let's assume we want to make sure every item is at the first level (less indented)
      // Check if listItem is already at the desired level to avoid unnecessary changes
      if (listItem.getNestingLevel() > 0) {
        listItem.setNestingLevel(0); // Adjusts the nesting level to 0, which is the first level
      }
    }
  }
}

For more precise formatting needs not related to lists, you might consider converting list items to regular text and then using paragraph formatting methods to adjust indentation. However, this would involve removing the list formatting altogether, which doesn’t seem to be your goal.