Skip to main content
Loic.Aigon
Legend
July 19, 2010
Answered

[JS] select several rows in a table

  • July 19, 2010
  • 2 replies
  • 922 views

Hi Guys,

Is there a way to select several rows in a table ? If I pass an array of rows reference to the app.select() method, it fails.

For one that's ok but not for two or more. That rows are next to each other don't chnge a thing.

var t = ....tables[0] //a given reference to a table

app.select([t.rows[1], t.rows[2], t.rows[3]])

//Error > Expected array of objects but received (Row,Row,Row)

app.select(t.rows[1]) works great. But when I want to select several rows it doesn't.

Any advice ?

TIA Loic

This topic has been closed for replies.
Correct answer tomaxxi

Hey!

Try selecting each row separatly:

var myT = app.selection[0].tables[0];
myT.rows[0].select();
myT.rows[1].select(SelectionOptions.ADD_TO);

tomaxxi

2 replies

Jongware
Community Expert
Community Expert
July 19, 2010

I couldn't get it to work either -- but the important question here is "why would you want to select them?" You don't need a selection to change its properties; this snippet works just fine, and it does not need to change the selection.

myRange = t.rows.itemByRange(1,3);
myRange.fillColor = "Black";

Loic.Aigon
Legend
July 19, 2010

Hi Theunis, Tomaxxi,

Using itemByRange is certainly helpful to act on several cells at once. But my intention is to grab the contents of these cells to copy/paste in another place.

I could use rows.itemByRange(n,n).contents but it seems contents is a property that donesn't handle other contents than strings. If my cell include graphic frame, this is unoperant. But maybe I am wrong.

So far, Tomaxxi's way seems to be the good way for me.

Thanks for all your help on that topic to both of you.

Loic

Jongware
Community Expert
Community Expert
July 19, 2010

I was going to say, "you can use .duplicate to copy parts around" ... but ... neither Table, nor Row, nor Cell has a .duplicate method! W00t!?

But you are correct on the .contents property -- it's just the text inside cells, converted to unformatted Javascript strings. I think applying it to a row might cause trouble, though.

You can still use the .text property, which does include inline graphics and all the rest -- but it's a property of cells, not of rows. This quick experiment worked:

table = app.selection[0].parentStory.tables[1];
table.rows.itemByRange(1,3).cells.everyItem().texts.everyItem().duplicate(LocationOptions.AFTER, app.selection[0]);

but you get the text of all cells and rows in one long line ... so you'd have to loop over rows and cells 'manually' ...

tomaxxi
tomaxxiCorrect answer
Inspiring
July 19, 2010

Hey!

Try selecting each row separatly:

var myT = app.selection[0].tables[0];
myT.rows[0].select();
myT.rows[1].select(SelectionOptions.ADD_TO);

tomaxxi