Copy link to clipboard
Copied
I need to get all of the tables on a particular page. The document consists of a bunch of unlinked text frames. I am using this to get the tables:
var doc = app.activeDocument;
var page = doc.pages[54];
var tables = page.textFrames.everyItem().tables.everyItem().getElements();
alert(tables.length);
This page (page 55) has 3 tables on it, but my code only returns 2 tables. Can a table be put directly on a page and not be in a text frame? If so, what is the best way to get all of the tables on a page? Thank you very much.
Rick Quatro
Ah, I don't know that. "Tables" is a collection, not a simple array, and some careless experimentation shows that you cannot simply use .concat to paste them together.
I'm pretty sure I've seen a couple of workarounds for a situation like this. Perhaps it's time to read "[CS3][JS] everyItem() and getElements()" -- i.e. Marc Autret's blog posts about this, at the end of that thread.
That is, if you stick to using everyItem()! Perhaps it's easier to build an array "the old fashioned way". This code
...Copy link to clipboard
Copied
Here is some further information about this. If I run this, it tells me that there are two text frames on the page:
var doc = app.activeDocument;
var page = doc.pages[54];
var pageTextFrames = page.textFrames;
alert(pageTextFrames.length); // 2
If I query each text frame's id, I get this:
25823
25902
If I click in each table and run this code, I get the extra text frame id:
if (app.selection[0].parent.parent instanceof Table) {
var table = app.selection[0].parent.parent;
alert(table.parent.id);
}
25823
25902
25990
So, clearly there are 3 text frames on the page. But my first code only picks up 2. If I query each of the 2 text frames, each of them contains 1 table.
Again, my task is to reliably get all of the tables on a particular page. Thanks.
Copy link to clipboard
Copied
Rick, tables cannot be put independently on a page, they always must reside inside a text frame. However: maybe one of these tables is in a textframe of its own inside the larger (main thread) frame, and thus you get three different parent frames while you only see two "on the page".
Copy link to clipboard
Copied
Hi Theunis,
OK, I am seeing this now. One of the text frames is part of a group, so it doesn't show up in the page.textFrames collection. It looks like I can use each group's textFrames collection to get the "missing" tables. Thanks for the quick response.
Rick
Copy link to clipboard
Copied
If I use this, I get all three tables.
var tables = page.textFrames.everyItem().tables.everyItem().getElements();
alert (tables.length);
if (page.groups.length) {
tables = page.groups.everyItem().textFrames.everyItem().tables.everyItem().getElements();
alert (tables.length);
}
Obviously, I am overwriting my first tables variable inside of the if statement. Is there an elegant way to end up with a single collection of tables, realizing that one or both collections could return tables or be empty? Again, the goal is to all of the tables on the page. Thanks in advance.
Copy link to clipboard
Copied
Ah, I don't know that. "Tables" is a collection, not a simple array, and some careless experimentation shows that you cannot simply use .concat to paste them together.
I'm pretty sure I've seen a couple of workarounds for a situation like this. Perhaps it's time to read "[CS3][JS] everyItem() and getElements()" -- i.e. Marc Autret's blog posts about this, at the end of that thread.
That is, if you stick to using everyItem()! Perhaps it's easier to build an array "the old fashioned way". This code is based on my earlier assumption you had textframes-in-textframes, but you can rewrite it to cater for groups as well:
t = app.layoutWindows[0].activePage.textFrames.everyItem().tables;
tableList = [];
for (i=0; i<t.length; i++)
tableList.push (t);t = app.layoutWindows[0].activePage.textFrames.everyItem().textFrames.everyItem().tables;
for (i=0; i<t.length; i++)
tableList.push (t);alert (tableList.length);
Copy link to clipboard
Copied
I just read Marc's posts and they were a real eye-opener. Things are still a bit fuzzy, but I am sure the concepts will get clearer as I keep working with them. Thanks for all of your help.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now