Manipulating cells in a matrix

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

Manipulating cells in a matrix

Post by js »

Some time ago Philip helped create a macro which ouputs an array of an arbitrary selection of cells. In an 8x8 matrix each cell is thus defind by a two-digit number representing row and column number of the cell:

Code: Select all

$doc = Document.active
$octals = Array.new
foreach $tblSel in $doc.tableSelections
 foreach $cell in $tblSel.cells
 $octals.push $cell.row & $cell.column
 end
end
$out = $octals
Document.newWithText "$out"
Let‘s say the result is $out = ("00", "21", "24")
My question is: How can I do the inverse: How can I select the 3 cells, knowing there position in the matrix?

What I try to do is to manipulate the selection. For a simple case like inverting row and column this can of course be done by swapping within the push expression. But what about changing the row number, for example. I tried:

Code: Select all

$octals.push $cell.column & (7-$cell.row)
This worked. But when I tried to to do the same thing with the column number, like this

Code: Select all

$octals.push (7-$cell.column) & $cell.row
This did not work. Why is that?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Manipulating cells in a matrix

Post by phspaelti »

This problem is very technical, and due to the way the Nisus coders have implemented evaluation of expressions.
In the first iteration you were not allowed to combine different operators on a line. Currently the implementation is more powerful, but in case of doubt or problems you should use parentheses to tell Nisus the order of evaluation. So you can fix your code like this:

Code: Select all

$octals.push ((7-$cell.column) & $cell.row)
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Manipulating cells in a matrix

Post by phspaelti »

Now addressing what I assume is your real question:
js wrote: 2023-08-31 03:06:14 Let‘s say the result is $out = ("00", "21", "24")
My question is: How can I do the inverse: How can I select the 3 cells, knowing there position in the matrix?
You can create table selections as follows:

Code: Select all

$sel = TableSelection.new $table, $rowRange, $columnRange
So first you will need to get hold of the relevant table. For example you can get the first table in your document like this:

Code: Select all

$doc = Document.active
$table = $doc.text.tables[0]
Then you need the ranges for the selection. You can get them from a string like "21" as follows:

Code: Select all

$str = "21"
$nums = $str.split('')
$rowRange = Range.new $nums[0], 1
$columnRange = Range.new $nums[1], 1
You can put all the individual table selections into an array and select all of them at once using:

Code: Select all

$doc.setSelection $selectionArray
Last edited by phspaelti on 2023-08-31 07:04:27, edited 1 time in total.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Manipulating cells in a matrix

Post by phspaelti »

So putting it all together into a single macro:

Code: Select all

# Get the table
$doc = Document.active
$table = $doc.text.tables[0]
# The list of cells to be selected
$cellList = Array.new "00", "21", "24"
# Make the selection
$selectionArray = Array.new
foreach $str in $cellList
   $nums = $str.split('')
   $rowRange = Range.new $nums[0], 1
   $columnRange = Range.new $nums[1], 1
   $selectionArray.push TableSelection.new $table, $rowRange, $columnRange
end
# Select the cells
$doc.setSelection $selectionArray
philip
js
Posts: 259
Joined: 2007-04-12 14:59:36

Re: Manipulating cells in a matrix

Post by js »

Thank you, Philip, for your explanations and macro models. All very satisfying and work fine!
Post Reply