Copy link to clipboard
Copied
How to operate only on selected cells in a table?
Thanks to so many enthusiastic people for the pointers., I've learned that I can operate on the current table accurately.
I would also like to know how to operate on the currently selected cell.
For example, in the script below, the entire table is now filled with “-” for empty cells.
How do I change it to only modify the selected cells.
Thank you very much.
var cell = app.activeDocument.selection[0].parent;
var myTable = cell.parent;
for (var i=0; i<myTable.columns.length; i++){
for (var j=0; j<myTable.columns.item(i).cells.length; j++){
if (myTable.columns.item(i).cells.item(j).contents == ""){
myTable.columns.item(i).cells.item(j).contents ="-";
}
}
}
1 Correct answer
In case of selected Cells - the return result is a bit misleading - selection[0] returns not just a cell - but a collection of Cells.
var mySelectedCells = app.activeDocument.selection[0].cells;
Copy link to clipboard
Copied
In case of selected Cells - the return result is a bit misleading - selection[0] returns not just a cell - but a collection of Cells.
var mySelectedCells = app.activeDocument.selection[0].cells;
Copy link to clipboard
Copied
Thank you very much.
The script is working fine.
By the way, how to represent the currently selected row, and column.
Like this? It's a little strange.
app.activeDocument.selection[0].column[j]?
app.activeDocument.selection[0].row[i]?
Copy link to clipboard
Copied
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Cell_2.html
Each cell has a parentColumn and parentRow property - then you can get index.
If you've selected more than one cell - you would've to iterate all selected - just in case there are merged ones.
Copy link to clipboard
Copied
Or simply from the name of the Cell.
But you should rather check them all - just to be safe.
Copy link to clipboard
Copied
var myrow=app.activeDocument.selection[0].cells.parentRow;
?
But sometimes you have 4 cells selected, but they may have different .parentRow.
Copy link to clipboard
Copied
No. You need to iterate through all "sub" cells:
myrow=app.activeDocument.selection[0].cells[i].parentRow;
Copy link to clipboard
Copied
Thank you.
But sometimes I select cells that may have more than 2 rows.
As in the following picture.
How can I recognize that I am selecting row[1] and row[2]?
Would it be too inefficient to traverse each cell once?
It must be traversed from B1 to E2, completing 8 cells to make sure that myrow=row[1];
myrow=row[2].
Is it possible to find only one cell in a row to be sure.
Then skip to the next row and find another cell.
Copy link to clipboard
Copied
Just check first and last cells.
When selecting Cells - either in the InDesign manually or through scripting - you can't select non-rectangual region.
So the first one - is the top-left = first row - last one - always bottom-right = last row.
Copy link to clipboard
Copied
Hi @dublove, this is a weird case—when the selection is a range of cells, app.selection[0] gives you a [Cell] object, but it isn't a normal Cell, it contains all the cells in it's `cells` property, as Robert mentioned. It is actually a similar object that you get when you make an itemByRange collection, so that you can do something like this:
app.activeDocument.selection[0].contents = '-';
and it will put text in every selected Cell.
But for most purposes, getting the selection's cells like Robert said, is best.
- Mark
Copy link to clipboard
Copied
[...] It is actually a similar object that you get when you make an itemByRange collection [...]
By m1b
Not exactly - if you select some text in this way - you'll get a single text object in return - so if you try to set its contents - you'll delete all the text objects you've used as the first and last element and in-between.

