Get Selected Table Rows With ExtendScript?

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Get Selected Table Rows With ExtendScript?

Viewing 16 reply threads
  • Author
    Posts
    • #115753
      Jeremy Howard
      Participant

      Hello,

      I am writing a script that needs to require the user to select two or more rows within a table so that those rows may be parsed. I am running into a bit of an issue trying to reference the user selection though.

      I have tried the usual methods, (app.selection) and the only object that I can seem to come back with is cells. If I attempt anything with “.rows” the script comes back as undefined.

      Is there a way to reference the selected rows in a table without having to do a bunch of math with the cell count/columnspan?

    • #115770
      Kal Starkis
      Member

      Hi Jeremy

      How about:
      app.selection[0].rows.everyItem().getElements();

      This should give you all your selected rows in an array, which you can then iterate through as needed.

      Cheers
      Kal

    • #115773
      Jeremy Howard
      Participant

      Hey Kal,

      Thanks for the response! I actually ended up figuring it out. Oddly enough, InDesign saw my selection as “[object Cell]” – even though I had two entire rows selected! Once I got the “[0]” added to the end of “app.selection” (like you indicated in your reply) I was able to get the rows just fine. The reason I was stuck earlier is because it never occurred to me to look for “rows” inside of an “[object Cell]”…

      Here is what I ended up doing:

      var myRows = app.activeDocument.selection[0].rows;

      Thanks again!

    • #115774
      Kal Starkis
      Member

      No worries. Yep, I agree, it’s totally confusing that it would report the selection as a Cell (rather than a Cells collection). If you look at app.selection[0].contents, you’ll see that the contents of ALL selected cells are there (in an array) so… go figure. Anyway, Adobe API mysteries aside, I’m glad you got it working!

    • #115779

      … Just by curiosity:

      If you just select some cells (not the entire rows), how do you get the selection of the entire corresponding rows?

      Best,
      Michel, for FRIdNGE

    • #115780
      Jeremy Howard
      Participant

      Hey Michel,

      You can access the rows of the selected cells this way:

      var mySelection = app.selection[0].rows;

      Cheers!

    • #115781

      I want to physically “select” the rows!

      app.selection[0].rows.select();

      doesn’t work!

      Best,
      Michel

    • #115782
      Jeremy Howard
      Participant

      Oh, I see! So, what you will want to do is this:

      var mySelectedCells = app.selection[0];
      var myTargetRows = mySelectedCells.rows;

      for(x = 0; x < myTargetRows.length; x++){
      myTargetRows[x].select(SelectionOptions.ADD_TO);
      }

    • #115783

      I always think simpler than most:

      app.selection[0].rows[-1].select(SelectionOptions.ADD_TO);

      Best,
      Michel

    • #115786
      Jeremy Howard
      Participant

      Ah, but that is thinking more complicated than most (at least from a readability and troubleshooting perspective).

      I broke out the pieces of the script in order to simplify it, people new to scripting will find the drawn out version easier to read and will almost certainly get more out of it.

      Condensed scripting != Simple scripting

      :)

    • #115790

      Hmm! …

      In ID, when you select some cells in a table and want to select the corresponding rows, you just click on the “shift” keyboard [“adding”] AND, THEN, “on the first cell then on the last cell of the last row covered by the initial selection” (that selects the last row)!

      THIS is exactly what the code simply means! ;-)

      Have a good day!

      M

    • #115801
      Kal Starkis
      Member

      Well, all this just reminds me why I don’t enjoy working with Adobe’s API. I mean, can anyone explain why app.selection[0].rows.everyItem().remove() works, but app.selection[0].rows.everyItem().select() fails?

      And more astonishing still, app.selection[0].rows[0].select(SelectionOptions.ADD_TO) selects ALL the rows with a selected cell, not just the first one. I get that SelectionOptions.ADD_TO is supposed to add to the current selection, but still, it makes no sense to me that you target just one row, and the others magically get selected too.

      • #115803

        I didn’t talk about this variant because it seems to be less fast than the one I gave. ;-)

    • #115802
      Kal Starkis
      Member

      Okay, reading Michel’s comment again, and actually checking the behaviour in InDesign, I think I get it. If you select multiple cells, say in column 1 of a two column table, then shift-click on ANY cell in column 2, all the rows get selected. (It doesn’t matter which row you target.) So I can see how SelectionOptions.ADD_TO just mimics this behaviour.

      And I guess app.selection[0].rows.everyItem().select() probably fails because there’s no guarantee of everyItem() delivering a contiguous selection (even though it does in this case). Since InDesign doesn’t support multiple selections, it makes sense that select() would require a single object. Jeremy, this may begin to explain why your selection (going back to your original question) was reported as a single object (Cell) and not a collection?? That seems like a bit of a hack on Adobe’s part though. Perhaps a genuine ExtendScript guru will stumble onto this thread and explain it to us in a way that makes sense.

    • #115868

      Less elegant but more readable! …

      app.selection[0].rows.itemByRange(app.selection[0], app.selection[0].rows[-1]).select();

      Best,
      Michel

    • #14323323
      Brian Pifer
      Participant

      Resurrecting this thread, as I’m struggling to get certain rows selected in a table.

      I want to select all rows after a certain point in the table using itemByRange. I’m trying the following.

      var rowsToMove = table.rows.itemByRange(i, rowCount-1).getElements();
      app.select(rowsToMove, SelectionOptions.REPLACE_WITH);

      I get the following error on the app.select line: Error Code# 30477: Invalid value for parameter ‘selectableItems’ of method ‘select’. Expected Object, Array of Objects, NothingEnum enumerator or SelectAll enumerator, but received (Row, Row, Row, Row, Row, Row)

      I’ve tried setting app.selection[0] = rowsToMove; I’ve tried rowsToMove without the getElements(). Mind-boggling.

      Any thoughts? Thanks.

    • #14323319
      Peter Kahrel
      Participant

      This works: select an an insertion point in any cell, then do this:

      cell = app.selection[0].parent.parentRow.cells[0];
      table = cell.parent.parent;
      table.rows.itemByRange(cell,table.cells[-1]).select();

      In other words, select the rows from the first cell in the selected cell’s row to the table’s last cell.

      A convoluted and non-intuitive way to go about it, but there you are!

      Peter

    • #14323318
      Brian Pifer
      Participant

      So weird that you have to select a cell in rows.itemByRange, but there you have it. Michel also PM’d me with a similar solution. Thanks, Peter!

Viewing 16 reply threads
  • You must be logged in to reply to this topic.
>