Copy link to clipboard
Copied
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
myContents = 0;
for(var l=myDocument.textFrames.tables
if (myDocument.textFrames.tables
}
if (myContents == 0) myDocument.textFrames.tables
}
}
}
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!
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
for(var k =0;k<_cells.length;k++)
{
//alert(_cells
try{
if(_cells
{
_rows
}
}catch(e){}
}
}
}
Vandy
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
@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
Copy link to clipboard
Copied
@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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
@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
Copy link to clipboard
Copied
Legend!
Thanks so much!
Copy link to clipboard
Copied
@Jarek – even that will not prevent all errors, if we have a situation like that:
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
Copy link to clipboard
Copied
...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
Copy link to clipboard
Copied
@Jarek – yes. That does it. Clever…
Uwe
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
More info about those cells contents would most welcome as well.
Can you tell any about its length? Unicode?
Jarek
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Looking in the info panel I think the unicode is: 0xFEFF 0xFEFF 0xFEFF 0xFEFF
Is this any help?
Copy link to clipboard
Copied
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
Find more inspiration, events, and resources on the new Adobe Community
Explore Now