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?
Table: How to find coordinates of selected cells
Re: Table: How to find coordinates of selected cells
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 just want to loop through all cells I recommend code like this:
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.
- Use Find to find the place in the table
- Use the .tableSelection property of the selection found
- Use the .cells of the TableSelection to identify the cell(s) where you are currently located
- Use .row and .column of the cell to find the coordinates inside the table
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
Re: Table: How to find coordinates of selected cells
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.
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.
Re: Table: How to find coordinates of selected cells
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.
The following code would output the selected values in your Octal calculator:
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
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
Re: Table: How to find coordinates of selected cells
Thank you Philip, as many times before. Both macros work very well.