Skip to main content
Participant
July 1, 2014
Answered

Not all cells in the table getting formatted except just the first one.

  • July 1, 2014
  • 2 replies
  • 392 views

var doc = app.activeDocument,

    _pages = doc.pages, i, j, k, l,

    _textframes, _tables, _row, _cell, rownum;

for (i = 0; i < _pages.length; i++) {

    _tables = _pages.item(i).Tables;

    for (j = 0; j < _tables.length; j++) {

        _row = _tables.item(i).rows;

        rowlen = _row.length;

        for (k = 0; k < _row.length; k++) {

            _cell = _row.item(i).cells;

            for (l = 0; l < _cell.length; l++) {

                _cell.item(i).appliedCellStyle = "CellA";

                _cell.item(i).paragraphs.everyItem().appliedParagraphStyle = "ParA";

            }

        }

    }

}

Hi, I am stuck in this code. I want to format all the cells in the table but using the above code only the first cell of first row is getting formatted . The other problem is that in all the rows it is couting only one cell. "CellA" and "ParA" are the cell styles and paragraph styles which I made in my pc. I am relatively new to indesign scripting and trying to learn something new.

This topic has been closed for replies.
Correct answer Jump_Over

Hi,

1. There is no ".table" property for "page" so pages.item(i).tables returns error;

2. Javascript is case sensitive so watch to start a property names with  a small letter ("Tables" is wrong);

3. There is no need to iterate through pages, tables, rows and cells - just use .everyItem().

so

app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().appliedCellStyle = "CellA";

app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().appliedParagraphStyle = "ParA";

should work.

Jarek

2 replies

TᴀW
Legend
July 1, 2014

I can see a few problems:

First a typo: in line 5 you've written Tables instead of tables

But more fundamentally, a Page object does not have a Table collection

as one of its properties. So you can't say

pages.item(i).tables

because there is no such thing.

However, various other objects do have tables as properties.

Specifically, any of the objects that inherit from Text provide access

to the Table collection (so: text, paragraph, line, word, character,

insertionPoint, etc.)

Depending on your needs, perhaps the TextFrames object is good for you.

So, for instance:

myTables = myPage.textFrames.everyItem().tables

will get you a collection of all tables in every text frame on the page

myPage (not sure if that will include tables in footnotes, or tables

within tables).

HTH!

id-extras.com | InDesign tools & scripts for typesetters, form designers, and translators
Community Expert
July 2, 2014

(not sure if that will include tables in footnotes, or tables

within tables).

@Ariel – no, they are not. Especially tables inside tables are a bit tricky.

Other "cases": text frames with tables nested to other objects. Or anchored text frames with tables.

So to get all tables inside "regular" stories without seeing "footnotes" or "tables inside cells of other tables" it's best to access document's stories.everyItem().tables.everyItem() .

For getting access to all tables in a document do a TEXT search for the special character that represents a table.

app.findTextPreferences = app.changeTextPreferences = null;

app.findTextPreferences.findWhat = "<0016>";

var tableCharacters = app.documents[0].findText();

var allTablesArray = new Array();

for(var n=0;n<tableCharacters.length;n++){

    allTablesArray = tableCharacters.texts[0].tables[0];

    };

app.findTextPreferences = app.changeTextPreferences = null;

If you are unsure, if the special character <0016> is representing a table object, you could do further tests in the for loop above.

Uwe

Jump_Over
Jump_OverCorrect answer
Legend
July 1, 2014

Hi,

1. There is no ".table" property for "page" so pages.item(i).tables returns error;

2. Javascript is case sensitive so watch to start a property names with  a small letter ("Tables" is wrong);

3. There is no need to iterate through pages, tables, rows and cells - just use .everyItem().

so

app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().appliedCellStyle = "CellA";

app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().appliedParagraphStyle = "ParA";

should work.

Jarek

Jump_Over
Legend
July 2, 2014

Hi,

...

Am I invisible...? I mean is post #1 viewed as public?

...

Jarek

Community Expert
July 2, 2014

Hi, Jarek!
Of course not !!

Forgive me, but I only wanted to answer Ariel's comment.

Uwe