Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Delete entire row if any cell is blank

Community Beginner ,
Jul 27, 2014 Jul 27, 2014

Hi all

I have found this script and it works perfectly for its purposes (if an entire row has no data/length, then remove it).

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();

            }

        }

    }

However, in the first column of my table, I have some text, and in the second column, I have no data.

How do I delete an entire row (including the first column containing text) using a script if just one cell of the row is blank?

I like the above script as I don't have to enter a table to run it - i.e. it will just apply to the whole document as I have many pages...

Thanks!

TOPICS
Scripting
4.2K
Translate
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 1 Correct answer

Enthusiast , Jul 28, 2014 Jul 28, 2014

Hi,

I would think, that this can be done with one loop, instead of three:

var doc = app.activeDocument,

  _cells = doc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();

for ( var k = _cells.length-1; k>= 0; k-- ) {

  if ( _cells.contents == "" )  _cells.rows[0].remove();

}

– Kai

Translate
Advocate ,
Jul 27, 2014 Jul 27, 2014

Hi steviej147,

Try the below codes, I hope it will helpful for you.

var myDoc = app.activeDocument;

var myStories = myDoc.stories.everyItem();

var myTables = myStories.tables.everyItem().getElements();

//--------------Below lines are delete the row,

for(i=0; i<myTables.length;i++){

    var myRows = myTables.rows;

    for(r=myRows.length-1; r>=0; r--){

        if(myRows.cells.everyItem().texts.everyItem().contents.join("") ==""){

            myRows.remove();

            }

        }

    }

//------------------Below lines are delte the column

for(i=0; i<myTables.length;i++){

    var myRows = myTables.columns;

    for(r=myRows.length-1; r>=0; r--){

        if(myRows.cells.everyItem().texts.everyItem().contents.join("") ==""){

            myRows.remove();

            }

        }

    }

thx,

csm_phil

Translate
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 Beginner ,
Jul 27, 2014 Jul 27, 2014

Hi!

Thanks for the fast response.

This works but only seems to delete completely blank rows.

After looking at my question, I think my explanation wasn't clear to begin with. An example would be:

Existing Table:

Column 1     Column 2

Edgeyard      Permitted

Courtyard     Permitted

Sideyard       Not Permitted

Readyear   

No Yard   

Proposed Table:

Column 1     Column 2

Edgeyard         Permitted

Courtyard         Permitted

Sideyard           Not Permitted

Thanks again!!

Steve

Translate
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 ,
Jul 27, 2014 Jul 27, 2014

Hi,

Try this.

var doc = app.activeDocument,

    _tables = doc.stories.everyItem().tables.everyItem().getElements();

for(var i =0;i<_tables.length;i++)

{

        var _rows = _tables.rows;

        for(var j =0;j<_rows.length;j++)

        {

                var _cells = _rows.cells;

                for(var k =0;k<_cells.length;k++)

                {

                    //alert(_cells)

                        if(_cells.contents == "")

                        {

                                _rows.remove();

                            }

                    }

            }

    }

Regards,

Chinna

Translate
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 Beginner ,
Jul 27, 2014 Jul 27, 2014

Hi Chinna

This sort of work, but for some reason, the last two rows won't delete? and I get an error message as well

Translate
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 ,
Jul 27, 2014 Jul 27, 2014

Chinna code working fine, just add try-catch in your code,

var doc = app.activeDocument, 

    _tables = doc.stories.everyItem().tables.everyItem().getElements(); 

 

for(var i =0;i<_tables.length;i++) 

        var _rows = _tables.rows; 

        for(var j =0;j<_rows.length;j++) 

        { 

                var _cells = _rows.cells; 

                for(var k =0;k<_cells.length;k++) 

                { 

                    //alert(_cells

                    try{

                        if(_cells.contents == "") 

                        { 

                                _rows.remove(); 

                            } 

                        }catch(e){}

                    } 

            } 

    } 

Vandy

Translate
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
Mentor ,
Jul 27, 2014 Jul 27, 2014

Hi Chinna,

One has to watch if the loop result increases or decreases a loop counter.

You need to use backward steps (i--) in this case ==> otherwise a code sometimes works - sometimes ends with error.

In current subject this problem shows according to last rows of modified table.

Jarek

Translate
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 ,
Jul 28, 2014 Jul 28, 2014

Hi,

I would think, that this can be done with one loop, instead of three:

var doc = app.activeDocument,

  _cells = doc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();

for ( var k = _cells.length-1; k>= 0; k-- ) {

  if ( _cells.contents == "" )  _cells.rows[0].remove();

}

– Kai

Translate
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 28, 2014 Jul 28, 2014

@Kai – your script will fail with the situation I presented in answer #8.
And that would be a scenario you never can rule out…

Uwe

Translate
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 28, 2014 Jul 28, 2014

@Kai – However a try/catch will do it:

var doc = app.activeDocument,

_cells = doc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();

for ( var k = _cells.length-1; k>= 0; k-- ) {

    try{

    if ( _cells.contents == "" )  _cells.rows[0].remove();

    }catch(e){};

  

    }

Uwe

Translate
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 Beginner ,
Jul 28, 2014 Jul 28, 2014

It works!

Thank you all for your very prompt assistance - you are all legends!!

Hopefully looking forward to helping someone else out one day!

All the best

Steve

Translate
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 28, 2014 Jul 28, 2014

@Steve – I suggest you'll give Kai the 10 points.

I made just a minor adjustment to his idea…

And:
Kai's code will also remove whole tables, if no row will qualify to stay.

Uwe

Translate
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 Beginner ,
Jul 28, 2014 Jul 28, 2014

Legend!

Thanks so much!

Translate
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 28, 2014 Jul 28, 2014

@Jarek – even that will not prevent all errors, if we have a situation like that:

TwoColumnTable.png

Immediately removing the row will prevent the loop to go on, because the cell you want to test next is already removed…

Looping back to forth within the index of the rows will help. But immediately removing the rows after detecting an empty cell will not.

And another question: should the whole table be removed, if ALL rows in a table qualify for removing?
If yes, then looping back to forth on table level is also recommended.

Try/Catch on cell level would do the job then.
(But I feel a little dirty using this). Better would be detecting all rows to remove first, then decide:

1. Should the whole table be removed (all rows qualify for removing)

2. If not, remove the individual rows from highest index number to lowest index number

Uwe

Translate
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
Mentor ,
Jul 28, 2014 Jul 28, 2014

...will prevent the loop to go on, because the cell you want to test next is already removed…

@Uwe,

check it pls.

From my side, in case of consecutive empty cells in the same row, after removing the row ==> next cell is taken from the upper row (so is checked twice in fact).

To make a code work more clear I suggest to check row.contents instead of cell.contents.

Like this:

var

  doc = app.activeDocument,

  _rows = doc.stories.everyItem().tables.everyItem().rows.everyItem().getElements();

for ( var k = _rows.length-1; k>= 0; k-- ) {

  curRowCont = "," + _rows.contents + ",";

  if( curRowCont.search(",,") != -1 )

  _rows.remove();

  }

Jarek

Translate
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 28, 2014 Jul 28, 2014

@Jarek – yes. That does it. Clever…
Uwe

Translate
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 28, 2014 Jul 28, 2014

But with one exception: if the separator character "," is used two or more times in a row inside the contents (could be a typo on the user's side), that row will be removed as well.
A different separator character like "§" will not qualify, because the individual cell contents of a whole row will always be separated with a "," if we stringify the row contents by adding a string.


Uwe

Translate
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
Mentor ,
Jul 28, 2014 Jul 28, 2014

Hi Uwe,

Right of course.

That's why I am repeating al the time:

One can be given with a way but exact solution need him to be awake what's going on inside a code.

This is a help forum!  ( most helpful I met, by the way )

Jarek

Translate
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 ,
Nov 18, 2015 Nov 18, 2015

Hi all,

I have a similar problem as Stevie, however having tried all the scripts provided here I can't seem to resolve this.
I am using data merge within indesign to create multiple (8,000+) documents as below... However if the right hand table cell has no data (e.g. Shift Payments) I want the whole row to be deleted. Like I said, I have tried running all the scripts provided here and none have worked for me so far... any ideas? I wondered if it was to do with the data merge leaving a hidden character in the cell?

Thanks,

Libby
Screen Shot 2015-11-18 at 16.40.20.png

Translate
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
Mentor ,
Nov 18, 2015 Nov 18, 2015

Hi,

I am pretty sure there is a hidden character left.

Solution could be to test contents with some RegEp, like:

var

  doc = app.activeDocument,

  _colls = doc.stories.everyItem().tables.everyItem().columns[-1].getElements(), cCol,

  _cells, cCell, k,

  checkWhiteRegEx = new RegExp ("\\S");

while (cCol = _colls.pop() ) {

  _cells = cCol.cells.everyItem().getElements();

  while (cCell = _cells.pop()) {

       if ( !checkWhiteRegEx.test(cCell.contents) )

            cCell.parentRow.remove();

       }

  }

Notice:

     RegEx is to test if contents match any of "not white space" character.

     If RegEx.test() fails with last cells contents ==> row is removed

Jarek

Translate
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 ,
Nov 18, 2015 Nov 18, 2015

Hi Jarek,

Thanks for your reply, unfortunately the script didn't work, It did work on a test row that I created with text in the left hand cell and an empty right hand cell, so I think it is definitely this hidden character left over from the data merge which is causing the problem. Any other suggestions most welcome...

Thanks,

Libby

Translate
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
Mentor ,
Nov 18, 2015 Nov 18, 2015

More info about those cells contents would most welcome as well.

Can you tell any about its length? Unicode?

Jarek

Translate
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 ,
Nov 19, 2015 Nov 19, 2015

Hi Jared,

Turning on hidden characters in Indesign reveals this (in screenshot) if I copy that in to text FIND in InDesign it appears as ^I^I^I^I (I typed this out as copying here didn't show up – don't know if that makes any difference) replacing this to nothing creates 0 replacements and the script still does not work

Hi Uwe, FIND and replace <FEFF> to nothing creates 74 replacements, the script still doesn't work. The next step using a GREP search for ^\s+ reveals nothing. Script still not working.

Screen Shot 2015-11-19 at 08.57.34.png

Translate
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 ,
Nov 19, 2015 Nov 19, 2015

Looking in the info panel I think the unicode is: 0xFEFF 0xFEFF 0xFEFF 0xFEFF

Is this any help?

Translate
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 ,
Nov 19, 2015 Nov 19, 2015

Another update – If I open the structure pane in Indesign I can manually untag elements from frames (I have to do this twice, once for top table then again for second table) then if I run a FIND search for <FEFF> replacing to nothing (54 replacements) the script works.

Is there any way of running a script that will do all of this? Doing this manually for each 8,000 documents produced would be a nightmare.

Thanks for you help.

Libby

Translate
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