Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

How to operate only on selected cells in a table?

Advisor ,
Feb 13, 2025 Feb 13, 2025

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 ="-";
         } 
      } 

}

 

 only.pngexpand image

TOPICS
Bug , Feature request , How to , Scripting

Views

274
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 13, 2025 Feb 13, 2025

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;

 

RobertatIDTasker_0-1739452761245.pngexpand image

 

Votes

Translate
Community Expert ,
Feb 13, 2025 Feb 13, 2025

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;

 

RobertatIDTasker_0-1739452761245.pngexpand image

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Feb 13, 2025 Feb 13, 2025

Copy link to clipboard

Copied

Hi Robert at ID-Tasker

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]?

R and C.pngexpand image

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2025 Feb 13, 2025

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. 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2025 Feb 13, 2025

Copy link to clipboard

Copied

Or simply from the name of the Cell. 

 

But you should rather check them all - just to be safe. 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Feb 14, 2025 Feb 14, 2025

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.

 

 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 14, 2025 Feb 14, 2025

Copy link to clipboard

Copied

No. You need to iterate through all "sub" cells: 

 

myrow=app.activeDocument.selection[0].cells[i].parentRow;

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Feb 14, 2025 Feb 14, 2025

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.

2.jpgexpand image

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 14, 2025 Feb 14, 2025

Copy link to clipboard

Copied

LATEST

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. 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2025 Feb 13, 2025

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2025 Feb 13, 2025

Copy link to clipboard

Copied

quote

[...] 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.

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines