InDesign Table - Collapse empty Row

New Here ,
Jan 21, 2010 Jan 21, 2010

Copy link to clipboard

Copied

My apologies if this is not posted in the proper category...


Ok, here's my issue...


I have a table with 5 rows.  Sometimes, not all of the rows in the table will contain data.  Is it possible for InDesign to automatically collapse a row if there is no data present in the cells in that row?


Thanks!

TOPICS
Scripting

Views

10.0K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Enthusiast , Jan 21, 2010 Jan 21, 2010

The following script should remove any empty rows in all tables in a document:

EDIT: From prior experience, I know it's a better idea to count backwards when deleting, so here is the earlier script with the order reversed:

var myDocument = app.activeDocument;

for(var i=myDocument.textFrames.length-1; i>=0; i--){

    for(var j=myDocument.textFrames.tables.length-1; j>=0; j--){

        for(var k=myDocument.textFrames.tables.rows.length-1; k>=0; k--){

            myContents = 0;

            for(var l=myDo

...

Likes

Translate

Translate
Community Expert , Jul 02, 2021 Jul 02, 2021

You could even try a shorter script like the following

var rows = app.activeDocument.textFrames.everyItem().tables.everyItem().rows.everyItem().getElements()
for(var i = rows.length - 1; i >= 0; i--)
{
    if(rows[i].cells.everyItem().contents.toString().replace(/,/g, "") == "")
        rows[i].remove()
}

-Manan

Likes

Translate

Translate
Enthusiast ,
Jan 21, 2010 Jan 21, 2010

Copy link to clipboard

Copied

The following script should remove any empty rows in all tables in a document:

EDIT: From prior experience, I know it's a better idea to count backwards when deleting, so here is the earlier script with the order reversed:

var myDocument = app.activeDocument;

for(var i=myDocument.textFrames.length-1; i>=0; i--){

    for(var j=myDocument.textFrames.tables.length-1; j>=0; j--){

        for(var k=myDocument.textFrames.tables.rows.length-1; k>=0; k--){

            myContents = 0;

            for(var l=myDocument.textFrames.tables.rows.cells.length-1; l>=0; l--){

                if (myDocument.textFrames.tables.rows.cells.contents != "") myContents++;

                }

            if (myContents == 0) myDocument.textFrames.tables.rows.remove();

            }

        }

    }

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 22, 2010 Jan 22, 2010

Copy link to clipboard

Copied

Thanks for that answer.. I created another post for how I actually utilize this script... Please help if you have any more info!

Thanks!

http://forums.adobe.com/thread/561169

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 22, 2010 Jan 22, 2010

Copy link to clipboard

Copied

Jongware has probably answered this already, but if not:

To run a script in InDesign, copy the code and paste it into

an ordinary text editor (such as Notepad in Windows), then

save the file with the extension .jsx in the Scripts Panel Folder

(typical path: Start > MyComputer > Local Disk(C:) >

Program Files > Adobe > Adobe InDesign CS3 > Scripts >
Scripts Panel).

script.png

It will then be available from within InDesign (Window > Automation > Scripts).

Yesterday's script removes empty rows from all tables in a document.

You might find the script below a bit more convenient, as it removes

empty rows in selected tables (or tables in selected text) only.

To run the script, highlight a table or some text containing a
number of tables, and then double-click on the name of the script in
the Scripts panel.

var mySelection=app.activeDocument.selection[0];

try{ var myTableCount = mySelection.tables.length;}

catch(myError){ var myTableCount = 1;}

for (var i=myTableCount-1; i >=0; i--) {

try{ var myTable = mySelection.tables.item(i);}

catch(myError){var myTable = mySelection; }

for(var j=myTable.rows.length-1; j>=0; j--){

            var myContents = 0;

            for(var k=myTable.rows.cells.length-1; k>=0; k--){

                if (myTable.rows.cells.contents != "") myContents++;

                }

            if (myContents == 0) myTable.rows.remove();

            }

        }

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 29, 2010 Jun 29, 2010

Copy link to clipboard

Copied

Hey Guys I tried to use both these scripts in CS4.

In both instances, Indesign crashed.

I have 800+ records of which approx 600 empty cells need to be removed.

I also tried it selecting a single table and it failed.

Any advice?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 14, 2011 Mar 14, 2011

Copy link to clipboard

Copied

I got this to work except that it considered rows that had a column with too wide of text as empty (i.e. there is a date 03/12/11 in a column too wide to hold it, therefore it shows up as a red dot). I need to keep those rows. How would you modify this script to be cautious of that?

Thank you,

Tim

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 15, 2011 Mar 15, 2011

Copy link to clipboard

Copied

Sure. The script currently increments a variable called "myContents" if there are non-zero contents in the cell:

if (myTable.rows.cells.contents != "") myContents++;

And then if myContents is zero, it removes the cell.

So you could also increment myContents if the text is overset; just add a line like this after the above:

if (myTable.rows.cells.overflows) myContents++;

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 03, 2012 Feb 03, 2012

Copy link to clipboard

Copied

Jeremy, your script works beautifully. Is it possible to modify it so it works for empty columns instead of rows? I tried doing it myself, but it didn't work (not suprisingly). Thanks

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

Hi everyone! I'm aware this post was made a few years ago, I'm in need of using this script but it won't work, claiming error 55  "Object does not support the property or method "tables"" on line 5.

I'm no expert in coding so I'm not sure what the problem could be...

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

hi @lauras86935790,

Try the following, the original code seems to have been corrupted when forum software was migrated

var myDocument = app.activeDocument;

for(var i=myDocument.textFrames.length-1; i>=0; i--){
    for(var j=myDocument.textFrames[i].tables.length-1; j>=0; j--){
        for(var k=myDocument.textFrames[i].tables[j].rows.length-1; k>=0; k--){
             myContents = 0;
            for(var l=myDocument.textFrames[i].tables[j].rows[k].cells.length-1; l>=0; l--){
                if (myDocument.textFrames[i].tables[j].rows[k].cells[l].contents != "") myContents++;
                }
            if (myContents == 0) myDocument.textFrames[i].tables[j].rows[k].remove();
            }
        }
    }

-Manan

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

You could even try a shorter script like the following

var rows = app.activeDocument.textFrames.everyItem().tables.everyItem().rows.everyItem().getElements()
for(var i = rows.length - 1; i >= 0; i--)
{
    if(rows[i].cells.everyItem().contents.toString().replace(/,/g, "") == "")
        rows[i].remove()
}

-Manan

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

Works like a charm! Thank you Manan, you saved my day!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

Hi there! Looks like the script doesn't work anymore... It just doesn't change anything ...

I'm using the latest Indd version now, maybe that's the reason? Did something change?

Thanks

L

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 03, 2013 Oct 03, 2013

Copy link to clipboard

Copied

Hello,

Nice script, but I have just one problem. The script deleted the blank rows, but the script add for some reasons blank spaces?

You will find my pdf here... Any idea?

http://www.wallacesanders.be/test.pdf

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 08, 2015 Apr 08, 2015

Copy link to clipboard

Copied

‌I'm trying to do the same thing after a data merge, however even if the field appears blank the data merge process leaves an invisible kernel/nugget that is viewed as content. So the row isn't deleted. I've cleared the content of the empty rows and tried the script again and it worked perfectly. Sometimes I will have cells with a zero in them. Anyway to modify this script to seek out and remove the rows with invisible kernels?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 22, 2015 Jul 22, 2015

Copy link to clipboard

Copied

I am having the same issues with the invisible kernals that you are any help would be great

thanks,

Drew

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 09, 2015 Oct 09, 2015

Copy link to clipboard

Copied

They're not really invisible, if you Show Hidden Characters and zoom in, you can see they look like a small double colon.  They're kind of hidden behind the "end of story" hashtag/symbol.


After spending half the day on this issue I finally found the solutionhttp://indesignsecrets.com/topic/cs5-data-merge-empty-field-character

Sure enough, in Find/Change, Text, searching for <feff><feff> and leaving "Change to" empty finally eradicated these buggers!  What a nuisance.  After that, the script runs fine and deletes the empty rows.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

FWIW: Just to reply to the original poster regarding the title of this thread:

 

"Collapse empty Row" or "Collapse Row" like you can do this in e.g. Excel is not possible with InDesign.

InDesign cannot collapse table rows or table columns. However, what is possible is to detect empty rows per scripting and remove them from the table.

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

@lauras86935790 said: "Looks like the script doesn't work anymore..."

 

Hi Laura,

what script code exactly do you mean?

Please point us to a specific post.

 

Note: when this discussion was moved from the old InDesign Scripting forum to the new InDesign forum in October 2019 a lot of scripting contents was damaged during the transition…

 

Thanks,
Uwe Laubender
( Adobe Community Professional )

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

Hi!

I was Referring to this one 

var rows = app.activeDocument.textFrames.everyItem().tables.everyItem().rows.everyItem().getElements()
for(var i = rows.length - 1; i >= 0; i--)
{
    if(rows[i].cells.everyItem().contents.toString().replace(/,/g, "") == "")
        rows[i].remove()
}

In Manan's response in Jul 02, 2021

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

Thanks for that, @lauras86935790 !

Could you post a sample document where this is not working?

Also note that one cannot remove all body rows of a table ( in case you have some header and footer rows and only one single body row that has no contents). You'll need at least one body row in every table.

 

Also not that graphic cells could spoil the result of the script.

Hm. And perhaps also specific constellations with merged table cells.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

I have 30 rows in every table (they were populated through data merge) and always have at least 10 full on top.

This is a catalogue I work on every year and always create it the same way (data merge + manual modifications), strange thing is it doesn't show any alert or erro, the script just runs to nothing. Debug says on line 1 "undefined is not an object".

It's a big file but nothing different from before. I'll try to share it tomorrow if necessary.

Thanks

laura 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

Ah, data merge!

Could be that there are remanants of that merge in the cells, special characters with Unicode code point FEFF.

You could look for them with Text Find with pattern <FEFF>.

Or there could be other white space character so that a script like the one above would regard such a cell not as empty. So, please go ahead and post a small sample document.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

LATEST

That was it! I ran a few text and Grep searches for similar characters and was sure I already got rid of them, but turns out I just got rid of the double ones and not the single ones.

Thank you so much for your help Uwe, an outside view is often what's necessary to solve problems.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines