I’ve called this post ‘removing duplicate paragraphs, but actually it’s a bit more than that – it’s removing paragraphs using a filtering function to compare the current paragraph with the next. By default, it will remove duplicate blank paragraphs in a document, which is a fairly common problem. Negotiating the Docs object model is always a bit tricky, so I find this a handy cleaning up function.

How to use

Examples

Remove any duplicate blank paragraphs.

Remove any paragraphs which would repeat the current one.

Because this takes an optional argument as a function, you can cause the paragraph represented by thisPara to be removed based on any criteria simply by returning true.

However you don’t need to actually compare things, so for example, you could delete paragraphs 8-10 like this.

If your call passes a function, then the objects that is passed to it are as follows (note that the last paragraph you’ll see is the 2nd last one, in other words both values are always a paragraph element). If your function returns true, thisPara will be deleted.

 property  contains
 thisPara  the current paragraph element
 nextPara  the nextParagraph element

The code

Here’s the function code.

For more like this see Google Apps Scripts Snippets
Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.