Are you looking for a script that will delete all empty rows and columns from the tables in your InDesign document?
You’ve come to the right place! If you’ve just done a data merge in InDesign, or imported a large Excel table, and there are many empty rows and/or columns in your document tables that need to be deleted, scroll to the bottom of this page to download a script to fix the document in one click!
The latest version of the script (updated May 2018) has been updated to work seamlessly with data-merged documents as well as Excel imports.
It works with all the latest versions of InDesign right down to CS6, on Macintosh and Windows computers.
But if you would like the challenge of writing a script like this yourself, read on to glean some useful tips and tricks that should speed things up!
Fixing up Dozens of Tables after Excel Import
I recently had a job involving importing dozens of Excel tables into InDesign in order to to create a document.
While the Excel files did come into InDesign properly and appeared as formatted tables, there were very many empty rows and colums that needed to be deleted.
And that, of course, called for a script!
So the first question is: How to detect whether a table row or column is empty in InDesign?
Detecting Empty Rows or Columns in an InDesign Table with Scripting
I started like this:
Given a table row r, we can write:
c = r.cells.everyItem().contents.join("");
and then test whether c is empty:
if (c === "");
And of course, we can do the same thing with columns as well.
However, it quickly became clear that this is an inadequate test. Why? Because a table cell in InDesign can be overset. If a cell is overset, its contents property returns an empty string. What this means is that rows and columns that are not really empty will be deleted. Whoops!
Overset Text Frames versus Overset Cells
To fix this, we have to find a different property to test. Initially (not having scripted tables for a while), I though that a cell has a Story property, just like an InDesign text frame.
If a TextFrame is completely overset, that is, there are no visible characters in a text frame because they are all overset, then checking myTextFrame.contents will return an empty string, just like an overset table cell. But checking myTextFrame.parentStory will return the Story object that the TextFrame is part of, and that can be checked to see if its empty.
However, cells do not have a Story property. Instead, the have a Text property. Given myCell being an InDesign table cell, you can check
If that is an empty string, you can be sure that the cell is empty and that there is not even any overset, invisible text attached to it.
The Efficient Solution: Collections
The next question then is how to test quickly whether all the cells in a row or column are empty.
The simple, straightforward solution is to loop through all cells in the row or column (and I’ve seen some scripts floating about on the Interwebs that do this). But this is very slow, and unnecessary.
This will get us an array of all the texts in all the cells in the row or column. We can use join() to join all of this, to make it easier to test whether the array is in fact empty:
myRow.cells.everyItem().texts.everyItem().contents.join("") === ""
If the above line returns True, we know the row is empty. And I think that is a pretty failsafe, and quick, method to test for an empty row or column in an InDesign table.
With this info, you should be able to loop through all rows and/or columns of all or some tables in an ID document, test whether they’re empty, and if so, remove() them!
There are one or two things to be aware of though: If you remove a column or a row, this may throw your reference to subsequent rows or columns off, so, as always, it’s best to work backwards.
Another danger is that by deleting a row or column, you delete the entire table (because some tables have only a single row or column). In such a case, this can throw references to further tables out of sync as well. Work backwards here too to avoid any problems.
I hope that armed with this information, you will find it an easy matter to write a little script to delete all empty rows and columns from your InDesign tables.
However, I am also making available below the final script I wrote, which deletes empty rows and columns for all or some tables in the document, and does a number of other useful things as well. I threw in a dialog to make it easier to check or uncheck which things the script should do (depending on the project). If you’re interested in the final script I came up with, read on.
More Table Cleanup Tasks
As I continued with the project I was working on, there were a number of other things I wanted to do to the tables I had imported from Excel:
- Many of the tables came in with merged cells. I needed to unmerge all of these.
- I wanted all the rows in all the tables to expand in height (auto-grow) to accommodate their contents. For some reason, they came in with their respective heights set to “exact”.
- I wanted to set a standard width to all the tables.
- I wanted an option for the script to ignore empty data-merge markers and white space, so that if that is all a row or columns contains, it will be considered empty.
A Ready-made Script for Instant Download
So, as mentioned, I threw together a script that does all or some these things, including deleting all rows and/or columns – with all options accessible via a simple options dialog.It also lets me choose whether to work on all tables in the document, or only the tables I select before running the script.
Here’s a screenshot of the UI window:
If, rather than trying to write a script to delete rows and columns yourself, you would like to use the one I wrote for myself, it is available for instant download for $29.99 (annual subscription). I found it to be a useful addition to my arsenal of InDesign scripts, and I think you will too!
The script works with all the latest versions of InDesign including CS6, CC2018 and beyond. (In fact, there is nothing in the script I’m aware of to prevent it from working in earlier versions of InDesign either.)
The script costs $29.99 (annual subscription).
RobMarch 31, 2016 6:04 pm
I’ve purchased DeleteEmptyRowsAndColumns. It works well! However, I’m having difficult with tables containing merge fields.
Here’s the scenario:
I have 5 records in the merge table for 5 employees. There are some employees that get a bonus, others that do not. I want t create the merged document, then delete all empty rows from employees that do not get a bonus. (note, that, in my actual situation, there are thousands of employees and several other criteria that lead to empty rows).
It seems as though having merge fields within the table cancels out the concept of “empty”, even though the fields contain no data. When I manually deleted the invisible content of merged cells, the script works as expected. But, of course, the manual process would defeat the purpose of the script.
Do you have a way of cleaning a merged documents table of clearing empty cells before running the DeleteEmptyRowsAndColumns script? Or, perhaps, some way of “flattening” the document prior to running the script.
AdminMarch 31, 2016 9:07 pm
I think I’d need to see a sample document to understand exactly what you mean. Could you send me a few pages of an .indd file to admin [at] id-extras.com?
RobApril 1, 2016 9:55 pm
Thanks for responding. I came back to let you know I found the solution.
Once the merged document has been created, I need only do a mass find/replace of , and replace it with nothing. is the invisible marker of the merge fields. In my instance, of the 800 merged pages, there were over 10,000 instances of . Once is replaced with nothing, the script works like magic!
AdminApril 3, 2016 2:56 pm
Thanks for posting back.
Something got garbled in your message (I think it’s the FEFF marker). Full details for anyone encountering a similar problem are here: