Skip to main content
Kasyan Servetsky
Legend
January 4, 2017
Answered

How to get reference to the first body row on page in a long table?

  • January 4, 2017
  • 2 replies
  • 18558 views

Dear forum,

I have a long table which is threaded among several text frames (pages). I want to get the reference to the first body row in each frame so that to check if it contains a product name. (I can do this by checking if all the cells in the row have been merged.)

Since a table looks like a single character for script, I don’t see a straightforward way to achieve my goal.

So far, I solved it in a sloppy way: I check the baseline of the 1st insertion point of the 1st cell in the row.

else if (RoundString(row.cells[0].insertionPoints[0].baseline, 1) == firstRowBaseline && mainRow != null) {

Is there a more elegant solution?

Thank you in advance!

Regards,
Kasyan

This topic has been closed for replies.

2 replies

Liphou
Inspiring
December 13, 2017

Merci

I'm on Mac, I do not know the command in Termibal.

For the error, it signals me that 'r' is 'undefined'.

I am especially curious to be able to use the 'Compare Functions.jsx' script

//---------------------------

Je suis sur Mac, je ne connais pas la commande en Termibal.

Pour l'erreur, il me signal que 'r' est 'undefined'.

Je suis surtout curieux de pouvoir réutilisé le script 'Compare Functions.jsx'

Trevor:
Trevor:Correct answer
Legend
January 4, 2017
Kasyan Servetsky
Legend
January 6, 2017

Hi Trevor,

Thank you very much for pointing me in the right direction!

I used the approach you used in your second script.

Here's the code in case someone is interested:

main();

function main() {

    var doc = app.activeDocument,

    table = doc.stories[1].tables[0];

    tableTest(table);

}

function tableTest(table) {

    var textFrame, previousTextFrame, currentTextFrame,

    rows = table.rows;

   

    previousTextFrame = rows[0].cells[0].insertionPoints[0].parentTextFrames[0];

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

        row = rows;

        if (row.rowType != RowTypes.BODY_ROW) continue; // skip headers & footers

       

        currentTextFrame = row.cells[0].insertionPoints[0].parentTextFrames[0];

        // the first body row in the first frame, or text frame has changed

        if ((i - table.headerRowCount == 0 && currentTextFrame == previousTextFrame) || currentTextFrame != previousTextFrame) {

            row.fillColor = "Yellow";

        }

        else {

            row.fillColor = "Magenta";

        }

   

        previousTextFrame = currentTextFrame;

    }

}

Regards,
Kasyan

Kasyan Servetsky
Legend
January 9, 2017

Hi Kasyan,

a brief comment on the getElements() approach.

The first time we gather all rows in one array by using getElements()—you call it static and it is static in the sense that it captures the rows at a given time—we have to work with that array from back to forth if we want to add rows, because the index of rows will then be effected downstream only, so to say.

In my examples I did not add (or remove) rows, just colored some rows and changed the contents of some cells. So I did not run into trouble looping forward. Maybe getElements() is not applicable to your script structure here, but generally I think it should be possible to work with getElements() when adding rows. But that will mean a total rewrite of your script.

Your other problem with the undo:

Currently I have no other idea than to:

1. Save the document before running the script with as Save As and a version number.

2. Running the script.

3. Save the document with a version number.

Regards,

Uwe


Hi Uwe,

In practice, using .everyItem().getElements() is not always faster than using collections.

I tested the script against the Test-Before.indd (the link to both is in a previous post) on my home PC a few times and it takes to complete:

4 secs with dynamic collection

5 secs with static array

Adding the 'Undo-Redo' feature is not a client's requirement; it's just a habit of mine. In this particular case, it didn't work for some reason. For pure academic interest, I'd like to know why.

Regards,

Kasyan