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

You will find solutions, if you follow my link in my answer above.
EDIT: Oops. You mentioned 8,000 documents…

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

Peter Kahrel has a script that can batch process InDesign files.

And you can run a script along with this batch run.

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

Thanks Uwe – I'll have a look at that.

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
LATEST

Thanks again Jared and Uwe for all your help. I have ended up with the below script that removes tags, removes hidden characters and then deletes rows from a table which have an empty data cell. I am looking in to batch processing now.

app.documents.everyItem().xmlElements.item(0).xmlElements.everyItem().untag();

var aDoc = app.documents[0];   

var allTables = aDoc.stories.everyItem().tables.everyItem().getElements();  

 

app.findTextPreferences = app.changeTextPreferences = null;  

 

app.findTextPreferences.findWhat = "<FEFF>";  

app.changeTextPreferences.changeTo = "";  

aDoc.stories.everyItem().tables.everyItem().changeText();  

app.findTextPreferences = app.changeTextPreferences = null;  

alert("done");

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

       }

  }

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

Hi Libby,

there is a lengthy discussion about special characters left over by datamerge.

See the details here:

Re: Multiple record data merge into paragraph styles-applies the wrong style

Do a TEXT search/replace to get rid of the special characters.

Search for (without the quotes): "<FEFF>"

Steps 2 and 3 might apply in your case.

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