🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: adobe.scripting.indesign
7 messages
7 total messages Started by Michael_Dean Sat, 10 Jan 2009 03:43
[JS CS3] Merging table columns
#2169
Author: Michael_Dean
Date: Sat, 10 Jan 2009 03:43
13 lines
1056 bytes
Hi all,

What's the fastest way to merge columns in a table?

I'm using the following loop to merge the 4th and 5th columns in my table:
for( var f = myTable.rows.length-1; f >= 0; f-- ){
	myTable.columns[3].cells[f].merge (myTable.columns[4].cells[f])
	}

It works, but I'm working on a large table (120 pages worth) and have many instance where I need to merge columns. Using the loop above, the script takes hours to run. Is there a faster way to merge columns while keeping the rows intact?

Thanks,
Michael
Re: [JS CS3] Merging table columns
#2171
Author: Peter Kahrel
Date: Sat, 10 Jan 2009 10:09
20 lines
1813 bytes
Michael,

You can optimise two things in your code. First, avoid referring to collections (such as myTable.rows and myTable.columns) repeatedly. Instead, create arrays from collections. Earlier this week this was discussed here: Harbs, "[JS] :: [CSX] - PageItem Stacking Order" #15, 6 Jan 2009 11:23 am </webx?14@@.3bbf275d.59b767cf/14> . see especially the reference to Kris Coppetier's article in Harbs's post, who explains that each time you refer to a collection, InDesign rebuilds it -- so you can imagine that rebuilding a big table such as yours causes the script to run for hours.

In your case, instead of referring to myTable.rows.length-1, do this:

myRows = myTable.rows.everyItem().getElements();
myRows_length = myRows.length-1;
for (var f = myRows; . . .

Secondly, you needn't refer to myRows[f].columns[...].cells[...]: myRows[...].cells[...] will do too. So your script coulkd look like this:

myRows = myTable.rows.everyItem().getElements(); 
myRows_length = myRows.length-1; 
for (var f = myRows_length; f >= 0; f--) 
   myRows[f].cells[3].merge (myRows[f].cells[4]);

This will probably speed things up.

Peter
Re: [JS CS3] Merging table columns
#2174
Author: Dave_Saunders
Date: Sat, 10 Jan 2009 14:37
7 lines
1000 bytes
I did some timing experiments a while back on referencing cells in tables and I was amazed to discover that the fastest way to reference a cell is:

myTable.cells.item("c:r")

where c is the column number and r is the row number. This was significantly faster (in my tests, probably in early CS3 days but might have been earlier) than any other method I could think of.

Dave
Re: [JS CS3] Merging table columns
#2175
Author: Dave_Saunders
Date: Sat, 10 Jan 2009 14:48
7 lines
798 bytes
Let me correct that a bit. What I actually used was:

myTable.cells.item(c + ":" + r);

where c and r and numbers and so can be derived from loop variables.

Dave
Re: [JS CS3] Merging table columns
#2176
Author: Peter Kahrel
Date: Sat, 10 Jan 2009 15:11
3 lines
623 bytes
Interesting -- thanks.

Peter
Re: [JS CS3] Merging table columns
#2181
Author: Michael_Dean
Date: Sat, 10 Jan 2009 22:06
7 lines
1206 bytes
Thanks to both of you. I'll try both of your suggestions and see how much faster I can make my script.

One more question - in your experience, do scripts seem to get bogged down with large tables? For example, my table script was running on my 120 page table for around 12 hours and still wasn't finished. But, when I cut the table down to 25 pages, the script completed in under an hour.

I thought the change in the time it would take to run would be proportional, but it doesn't seem to be. I don't think the loops get stuck running indefinitely, but I could be wrong.

Thanks again!
Re: [JS CS3] Merging table columns
#2182
Author: Dave_Saunders
Date: Sat, 10 Jan 2009 22:33
11 lines
897 bytes
Are you doing anything about the undo queue in your script? As that gets longer and longer it can really bog down a script.

I usually have something like this in my main loop:

if (j % 100 == 0) { 
  myDoc.save(File(myFullName)); 
}

where j is the main loop counter.

Dave
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads