A faster way would probably be to add your data as regular text,
separated by tabs and paragraph breaks. Then, get a reference to the
first and last character of the text you have added, and run a
myText.convertToTable() on it.
Good idea! Thank you. I will try!
I read about XML Import.
Is this also a possible way?
The question also is: if you want to create a table from data OR fill existing (formatted) table with data?
table.contents is an array of cell.contents (order by index) - so you can assign an array if it structure match your table size.
I mean, if your array is 24 elements and your table is 4x6 dimension - you can use:
table.contents = array;
thank you for this hint!
Yes, I want to fill the data in an existing table.
So your idea works for me.
The array must also include the header rows.
This is really faster, than my solution
I had one case with about 100,000 cells (fortunatelly formatting was no issue in this case) and a lot of text in it.
Read the contents to an array, did some sorting and contents manipulation in the array, wrote out a text file of the contents, removed the original table, placed that text file in the story of the now removed table and converted to table again.
That was tremendously faster than assigning the new contents to the table already built.
thank you for sharing your experience.
I have a question. In your example, you have the content already in an array.
Is it really faster to use the ConvertToTable(): write Array in text file, place text file and ConvertToTable?
This should be faster: table.Contents=Array. Or not?
Other Point: In my first solution I use two Loops.
It is faster, when I make the text box height smaller.
Then InDesign is faster, because the cells are not visible.
"This should be faster: table.Contents=Array. Or not?"
Surprisingly is was not. It was slower. A lot slower.
myArray = myTable.contents;
Then I did operate on the array. Not on the table object or its cell objects. No direct access to InDesign's DOM objects. Just the built array.
My text file was written by populating it with a string of the array:
myString = myArray.join("separatorString");
separatorString was something that was never used as contents in the table.
Something like "§§§"…
After importing the text file I used the convertToTable() method providing the separatorString as separator for the first and second argument with the number of columns as third argument. The number of columns was known from my original table.
var myNewTable = myText.convertToTable("separatorString", "separatorString", myNumberOfColumns);
Alternatively you could also remove the table after building the array and assign "myString" as contents for the insertionPoint of the removed table in the story. I think I tested that as well, but do not know, if there is a difference in speed opposed to placing a text file with the same contents (I think it was, but not I'm not sure anymore). So I ended up with:
1. Contents of table to Array
2. Array manipulation
3. Array to String
4. Write String as file
5. Remove table
6. Place file at InsertionPoint of (now removed) table
Also to note: This was in InDesign CS5 with a very large table.
Things could have changed in InDesign versions with 64-Bit support.
But I did not test that yet. The customer I wrote this script for is still on CS5.
it is also a surprise for me, that ConvertToTable() is faster.
Maybe because InDesign can write all data in an unformatted table.
I have in my application (building a catalog page) a formatted table.
So it is better for me to assign the array directly to the table.Contents.