Search code examples
google-apps-scriptformattinggoogle-docsglyphbulletedlist

How do I set the glyph style on all bulleted lists in a Google doc using App Script


I am automating the building of a document based on a set of inputs. I started with Google's example here: https://developers.google.com/apps-script/samples/automations/aggregate-document-content, and made modifications.

The problem I have is when one or more of the source documents contains a bulleted list. The list is imported into the new document as a bulleted list, which is what I expect, but there is no glyph for the list, so it just looks like indented text. I have to manually select each bulleted list and set the bullet style for the list.

Is there a way for me to specify the bullet style I want to be used for all bulleted lists one time, or in some way search for the bulleted lists with App Script after the import to find them and change them?


Solution

  • Okay, I think I figured it out.

        function setGlyphType() {
            var body = DocumentApp.getActiveDocument().getBody();
            var bullet = null;
            while(bullet = body.findElement(DocumentApp.ElementType.LIST_ITEM, bullet)) {
                bullet.getElement().asListItem().setGlyphType(DocumentApp.GlyphType.BULLET);
            }
        }
    

    The while loop searches the body for elements of type LIST_ITEM, starting from the last LIST_ITEM found. If an element of that type was found, change the GlyphType to BULLET.