How can I use a Google Script in Google Doc to automatically remove text AFTER the numbers in a numbered list?

53 Views Asked by At

I have a document which has a auto numbered list:
1. text here
2. text here

and I'd like the script to remove the text(and all the numbers except the #1 (the first item in the numbered list should remain):

So after the script, the document should show only the blank numbered list with no entries after the numbers:
1.
2.

I cannot search for a "1. " since it is a automatically numbered list. All I want to be able to do is to remove the contents of the list but leave the numbers as a numbered list.

I tried

   var regExp2 = "[[:space:]]"
   docBody.replaceText("\n"+regExp2,"\n");

but get nothing.

1

There are 1 best solutions below

0
PFRC On

I think I got this to work:

  var body = DocumentApp.getActiveDocument().getBody();
  var id_old=""
  for (var num=body.getListItems().length-1; num>=0; num--){
    var list = body.getListItems()[num];
    var id=list.getListId();
    list.clear();
    if (id==id_old){list.removeFromParent();}  //remove item from list if id of item is repeated
    id_old=id
  }

This successfully removes all but the first items in the list and clears first the entry in the list.