Script to make 1 master table from multiple tables

New Here ,
Aug 16, 2022 Aug 16, 2022

Copy link to clipboard

Copied

Hi Everyone, I am new to InDesign scripting and I am after some advice.

I have a multi-page document with basic tables on each page. I am trying to create a script that adds the copy from every table into an array and creates a new document with just one table of all items. Any help/advice would be greatly appriciated, thanks Vincent.

Views

59

Likes

Translate

Translate

Report

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

Community Expert , Aug 17, 2022 Aug 17, 2022

Hi Vincent,

thank you for your screenshot. So it's a bit different than I initially thought.

You want to transpose all second columns of all tables of your source document to rows of a new table.

 

The contents of the first column of any table in your source document should be the contents of the header row of the new table. Did I understand this right?

 

Now your screenshot is also showing tables on parent pages. At least the dotted frame edges lead to this conclusion.

Should tables on parent p

...

Likes

Translate

Translate
Community Expert ,
Aug 16, 2022 Aug 16, 2022

Copy link to clipboard

Copied

Hi @vincent21942196xwz9 ,

do all the tables contain the same amount of columns?

 

Do you want to transfer formatted text?

Are there any header or footer rows? What, if they differ in contents from table to table?

Does the order of the contents matter if there is more than one table on a single page?

How would you determine the order of contents?

 

Best show some screenshots of some of your pages with frame edges enabled and hidden characters visible.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Likes

Translate

Translate

Report

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 ,
Aug 17, 2022 Aug 17, 2022

Copy link to clipboard

Copied

Hi Laubender,

Thanks for the reply!!!

The columns and rows are the same for every table (2x7) and the headers (left hand column) are the same for all tables.

The formatting and the order does not really matter. The document in question has about 300 pages with 2-3 tables per page. Please see screenshot, hope this all makes sense!

VincentScreenshot 2022-08-17 at 08.23.51.png

 

Likes

Translate

Translate

Report

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 ,
Aug 17, 2022 Aug 17, 2022

Copy link to clipboard

Copied

LATEST

Hi Vincent,

thank you for your screenshot. So it's a bit different than I initially thought.

You want to transpose all second columns of all tables of your source document to rows of a new table.

 

The contents of the first column of any table in your source document should be the contents of the header row of the new table. Did I understand this right?

 

Now your screenshot is also showing tables on parent pages. At least the dotted frame edges lead to this conclusion.

Should tables on parent pages that are applied to document pages count as well? This would complicate the matter.

 

Did you ever write a script for InDesign with ExtendScript code?
I'm asking this, because you should understand what I am doing with code and especially what objects I do address.

 

Tables live in Stories that live in a document. Somewhere. On pages, on pasteboards, on parent pages, even in overset text. Can we dare to gather all tables of the document in one array that we later walk through using a loop?

We do that with the following code:

 

var tablesArray =
app.documents[0]
	.stories.everyItem()
	.tables.everyItem()
	.getElements();

 

Note, that this will not get to tables that are in footnote texts or texts in Note objects.

Or tables that are positioned in table text cells. So I did some assumptions:

 

If you would like to exclude tables on e.g. pasteboards or parent pages or pasteboards of parent pages you need more code to test for things. For now let's assume this gathers the right tables. You'll be save if all relevant tables are on document pages and no other text frames in the whole document contain any tables at all.

 

Next thing is, we need the contents of a column that should be the contents of a row later in the new table in that new document. And we need to know how many header rows and body rows the new table should contain. First things first:

 

var numOfHeaderRows = 1;
var numOfBodyRows = tablesArray.length;
var contentsArray = tablesArray[0].columns[0].contents;

The contents of a table column of table row could be an array of contents of cells.

If there are only text cells with text contents, variable contentsArray above will have an array of strings. How many strings? The number of rows in the first table of your source document.

 

We will add contents while we go and walk through the tablesArray with a loop. While we are doing this we concatenate array after array of contents of the second column in every table that is stored in the array:

 

for( var n=0;n<tablesArray.length; n++ )
{
	contentsArray = contentsArray.concat( tablesArray[n].columns[1].contents );
};

 

That's it. Now we only need to build a new document with a new table and apply the gathered contents to that new table. As I already said, tables live in stories. And to add a new story to a document we could add a new text frame that automatically adds a new story and then use method add() for tables to add a new table to the story of that text frame.

 

For that we need a new document. The simplest thing if you can live with anything:

var newDoc = app.documents.add();

From experience we know that a new document always has at least one single page on the first document spread. So we could simply add a text frame to that first page with a specific size, not too small, so that some rows of the new table could be visible:

var newTextFrame = newDoc.pages[0].textFrames.add
(
	{ geometricBounds:[ 0,0,"100mm","200mm" ] }
);

 

Your task now:

Add a new table to the parentStory of that text frame that lives in variable newTextFrame with the following properties:

 

headerRowCount
bodyRowCount
columnCount
contents

 

What did I say about the number of header rows? The number of body rows?

Hm. We did not talk about the number of columns in the new table. But from your screenshot it is clear that it is the number of rows of any of your tables in your source document. E.g. the first table in the source document, that could be expressed as the first element of our table array:

tablesArray[0]

So the number of columns in the new table is:

tablesArray[0].rows.length

 

Also calculate the width of the new table if you like to have every column in the new table the same width like the columns in the tables in the source document. All properties can be found in the InDesign DOM documentation. Also property width of a table and width of a column. See Object Table and Object Column:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Table.html#d1e440541

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Column.html#d1e444267

 

Here method add() for tables:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Tables.html#d1e443758__d1e443807

 

Regards,
Uwe Laubender
( Adobe Community Professional )

 

Likes

Translate

Translate

Report

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