FunThomas
FunThomas

Reputation: 29466

Remove Paragraph from ItemList

I have lots of GDocs that have a mess in their numbering. Part of the docs where copied from other docs, parts from Word documents (docx).

To clean up, I want to loop over a document and remove the numbering. I am able to loop over the paragraphs and identify header paragraphs, so I can cleanup numbering that is part of the text itself (at the beginning of the line), not problem.

But most of the paragraphs get their numbering from the "Bullets and Numbering" option. I learned that those paragraphs are part of the ListItem collection (is this an array?) and I am able to loop over that also. I can set the type by using setGlyphType, but that's not what I want, I want no type at all.

I thought maybe removing it from the list could solve this, but the only method I found was RemoveFromParent which deletes the whole paragraph.

When I'm in the document, I have to click at the beginning of the paragraph and press Backspace twice, but I can't find any method to remove the numbering from a paragraph per script.

enter image description here

Upvotes: 0

Views: 456

Answers (1)

NightEye
NightEye

Reputation: 11214

I have successfully implemented the workaround above.

Script:

function Remove_Bullets() {
  var body = DocumentApp.getActiveDocument().getBody();
  for (var i = 0; i < body.getNumChildren(); i++) {
    var element = body.getChild(i);
    if (element.getType() == DocumentApp.ElementType.LIST_ITEM) {
      element.getParent().asBody()
             .insertParagraph(i, element.asText().getText())
             .setAttributes(element.getAttributes());
      element.removeFromParent();
    }
  }
}

Sample:

sample

Turned into:

output

Note:

  • Some formatting wasn't applied properly due to it being stored in bullet/header settings but basic attributes were applied nevertheless.

Upvotes: 1

Related Questions