Get Selected Table Rows With ExtendScript?
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Get Selected Table Rows With ExtendScript?
Tagged: extendscript, rows, Selected Rows, selection, table, Tables
- This topic has 17 replies, 5 voices, and was last updated 4 years, 2 months ago by Brian Pifer.
-
AuthorPosts
-
-
April 2, 2019 at 4:04 am #115753Jeremy HowardParticipant
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?
-
April 2, 2019 at 3:14 pm #115770Kal StarkisMember
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 -
April 2, 2019 at 5:37 pm #115773Jeremy HowardParticipant
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!
-
April 2, 2019 at 6:48 pm #115774Kal StarkisMember
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! -
April 3, 2019 at 5:01 am #115779Michel Allio for FRIdNGEParticipant
… 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 -
April 3, 2019 at 5:59 am #115780Jeremy HowardParticipant
Hey Michel,
You can access the rows of the selected cells this way:
var mySelection = app.selection[0].rows;
Cheers!
-
April 3, 2019 at 6:11 am #115781Michel Allio for FRIdNGEParticipant
I want to physically “select” the rows!
app.selection[0].rows.select();
doesn’t work!
Best,
Michel -
April 3, 2019 at 6:42 am #115782Jeremy HowardParticipant
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);
} -
April 3, 2019 at 6:58 am #115783Michel Allio for FRIdNGEParticipant
I always think simpler than most:
app.selection[0].rows[-1].select(SelectionOptions.ADD_TO);
Best,
Michel -
April 3, 2019 at 7:59 am #115786Jeremy HowardParticipant
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
:)
-
April 3, 2019 at 9:45 am #115790Michel Allio for FRIdNGEParticipant
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
-
April 3, 2019 at 4:49 pm #115801Kal StarkisMember
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, butapp.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.-
April 3, 2019 at 5:14 pm #115803Michel Allio for FRIdNGEParticipant
I didn’t talk about this variant because it seems to be less fast than the one I gave. ;-)
-
-
April 3, 2019 at 5:07 pm #115802Kal StarkisMember
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. -
April 5, 2019 at 2:15 pm #115868Michel Allio for FRIdNGEParticipant
Less elegant but more readable! …
app.selection[0].rows.itemByRange(app.selection[0], app.selection[0].rows[-1]).select();
Best,
Michel -
January 8, 2020 at 11:33 am #14323323Brian PiferParticipant
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.
-
January 9, 2020 at 8:30 am #14323319Peter KahrelParticipant
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
-
January 9, 2020 at 8:43 am #14323318Brian PiferParticipant
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!
-
-
AuthorPosts
- You must be logged in to reply to this topic.