Skip to main content
dublove
Legend
January 4, 2025
Answered

My basic logic is confused: how to compare the row 1 with row2.

  • January 4, 2025
  • 2 replies
  • 701 views
I want to compare the contents of row 1 and row 2, and if the contents are same(same row),  delete the second row.

It's a bit confusing to use for and if together.
logical disorder.
It always feels like the loop nesting isn't finished.
Or it just starts and ends.

 

 

for(var i = 0; i < table.columns.cells.length; i++){
	if(table.rows[0].cells[i].contents==table.rows[1].cell[i].contents){
	table.rows[0].remove();
	}
	else{
	}
	}

 

 

 
Correct answer m1b

Hi @dublove, you can use the "everyItem" method to quickly collect the contents of every cell of the row. Like this:

if (table.rows[0].cells.everyItem().contents.join() == table.rows[1].cells.everyItem().contents.join())
    table.rows[1].remove();

.

2 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
January 4, 2025

Hi @dublove, you can use the "everyItem" method to quickly collect the contents of every cell of the row. Like this:

if (table.rows[0].cells.everyItem().contents.join() == table.rows[1].cells.everyItem().contents.join())
    table.rows[1].remove();

.

dublove
dubloveAuthor
Legend
January 5, 2025

Hi m1b.

extremely grateful
You've got this one, simple and working. It's very efficient.

But I still want to know why this one of mine is stuck.
It seems to go into a dead loop.
It took me all night to figure it out.

var sameRow=false;
for(var i = 0; i < table.columns.length; i++){
	if(table.rows[0].cells[i].contents==table.rows[1].cell[i].contents){
		sameRow=true;
	}
	else{
		sameRow=false;
		break;
	}
	}
	if( sameRow==true){
		table.rows[0].remove();
	}

 

Robert at ID-Tasker
Legend
January 5, 2025
quote

The table hierarchy is sometimes messed up.


By @dublove

 

It's not messed up at all - it's very simple and practical. 

 

Each Table has Rows, Columns and Cells collections. 

 

Each Row and Column has its own Cells collection. 

 

Each Row has Columns collection. 

 

Each Column has Rows collection. 

 

If you want to process all Cells in the Table - you iterate through myTable.cells

 

If you want to process Cells in a specific Column - you first get a reference to this column - myCol=myTable.columns[x] - then iterate trough the collection of the Cells in this Column - myCol.cells

 

The same if you want to process a specific Row. 

 

If you want to process a range of columns / rows in the Table - your 1st loop is through the columns / rows then 2nd loop is thought the cells in each column / row. 

 

 

The beauty of the Tables structure is - you can make a single universal function that takes Cells collection - so you don't have separate functions for processing cells in columns or cells in rows. 

 


quote
 

Each Row and Column has its own Cells collection. 

 

Advantage - you don't have to care if there are merged cells - if it's not important - and you won't process the same Cell twice - or won't get error that Cell doesn't exist. 

 

Robert at ID-Tasker
Legend
January 4, 2025

I'm sorry, but what are you doing? 

 

It's like you're constantly ignoring all the previous information / advice - and example code - and are putting some garbled / random code?

 

This doesn't make any sense: table.columns.cells.length

 

columns collection doesn't have cells - so you should iterate through the collection of columns of the table: 

 

table.columns.length

 

 

But then, you don't have guarantee, that number of cells in both rows is the same - some cells can be merged - horizontally or vertically. 

 

dublove
dubloveAuthor
Legend
January 4, 2025

I delete duplicate header rows.
There is a “faux header” that is sometimes deleted.

It's bedtime. Good night.

Robert at ID-Tasker
Legend
January 4, 2025

To avoid problems like this - you should "mark" rows like this - added during 1st processing - in case that they could / should be removed at some point - then remove them first before doing any updates / extra processing. 

 

You can "mark" Table / Column / Row / Cell - and other objects in InDesign - using label property - or you can use insertLabel and then extractLabel to check the value.