Table: How to find coordinates of selected cells

Get help using and writing Nisus Writer Pro macros.
Post Reply
js
Posts: 259
Joined: 2007-04-12 14:59:36

Table: How to find coordinates of selected cells

Post by js »

In a table, if I know the coordinates x,y of a cell I can use "$tbl.textAtRowAndColumn x,y" to find the contents of that cell.
But what is the best way to achieve the inverse: to find the coordinates of a selected cell, or better: to get an array of the the coordinates of all selected cells?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Table: How to find coordinates of selected cells

Post by phspaelti »

First of all, remember that this is Nisus, so if you want to find something you can always use Find/Powerfind.

But otherwise it will depend on what you want to achieve
  • If you have a TableCell object you can use the .row and .column properties
  • If you have a Table object you can get the number of rows using .rowCount and columns using .columnCount which you could use to loop through all cells
  • If you have a TableSelection object you can use .rowRange and .columnRange in much the same way, but you can also just use the .cells array to loop through all the cells of the TableSelection.
So depending on what you want to do you might:
  1. Use Find to find the place in the table
  2. Use the .tableSelection property of the selection found
  3. Use the .cells of the TableSelection to identify the cell(s) where you are currently located
  4. Use .row and .column of the cell to find the coordinates inside the table
This procedure might be useful, for example, if you want to put something in the cell after/below the cell containing certain information.

If you just want to loop through all cells I recommend code like this:

Code: Select all

$sel = TableSelection.active
$tblSel = TableSelection.new $sel.table # We do this to be sure to get the whole table
foreach $cell in $tblSel.cells
    ....
end
philip
js
Posts: 259
Joined: 2007-04-12 14:59:36

Re: Table: How to find coordinates of selected cells

Post by js »

Thank you Philip.
What I try to do is checking which cells in a table have been selected, either manually, or by a Find expression. This is to study mathematical and geometrical relationships inside a matrix. Take a simple example like this: Build an 8x8 matrix and fill it with decimal numbers from 0 to 63, starting from top left to bottom right. Now select an arbitrary cell and find its coordinates. You may then realize that your table is a simple octal-decimal calculator: The .row and .column numbers concatenated are the octal number corresponding to the decimal number you had selected. So what I search for corresponds to your nr. 3. I just don't quite understand how to identify the cell(s) which are actually selected. And whether it is possible to find an noncontiguous selection of cells in a single process.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Table: How to find coordinates of selected cells

Post by phspaelti »

Hello js,
I see what you are saying. Nisus does have the Selection object which can work with non-continguous selections, but this object does not have direct access to the cells in the selection. You will have to loop through all the TableSelections within the selection. If your TableSelection(s) do not form a rectangle you will end up with multiple TableSelections.

The following code will gather all the selected cells / cells with selected bits in them.

Code: Select all

$doc = Document.active
$allCells = Array.new
foreach $sel in $doc.tableSelections
    $allCells.appendValuesFromArray $sel.cells
end
The following code would output the selected values in your Octal calculator:

Code: Select all

$doc = Document.active
$decimals = Array.new
$octals = Array.new
foreach $tblSel in $doc.tableSelections
  foreach $cell in $tblSel.cells
    $decimals.push $cell.text
    $octals.push $cell.row & $cell.column
  end
end
$out = $decimals.join(',') & "\n" & $octals.join(',')
prompt $out
philip
js
Posts: 259
Joined: 2007-04-12 14:59:36

Re: Table: How to find coordinates of selected cells

Post by js »

Thank you Philip, as many times before. Both macros work very well.
Post Reply