Reply to topic  [ 9 posts ] 
Select a range of cells within a table 
Author Message

Joined: 2016-06-21 09:31:11
Posts: 5
Location: France
Hi,
Nisus has proved to be incredibly useful for me to work with text embedded in tables. I've written several macros to help me formatting text, i.e. BUT (I'm quite a newbie in programming) I've spent hours looking for a command allowing me to select a range of cells within a table from a given cell (copying it and then moving it downward), without success.
Any hint ?

Thanks.


2016-06-21 09:38:52
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Hello Fred,
To do what you are wanting to do you will need to use the macro language, and in particular the Nisus object model. A Nisus document is actually a complicated structure known as an object, which itself contains a whole series of other objects: the text, the notes, the comments, the floating content, etc. Each of these objects can in turn contain yet further objects. The tables themselves are actually inside the text object(s) of the document.

Now in order to move the content of table cells "down" in a table you also have two options:
  1. You can select (a range of) table cells and copy them whole
  2. You can copy the content of table cells one at a time
Here I'll explain how to do [1] although the second approach might have advantages.

So in order to make a table selection you need three things:
  1. A table object
  2. A range of rows over which you want to select
  3. A range of columns over which you want to select

In order to get a hold of the table and to make the selection, you in turn need the document object you want to work with. Generally we assume that this is the current active document. You can do that like this:
Code:
$doc = Document.active

Now in order to get the table you have several options. The first is where you happen to know which table you want, let's say the second table in your document. You can get that as follows:
Code:
$table = $doc.text.tables[1]

This command tells Nisus to look in the active document, find the text, and then find the tables inside the text. Since there may be many tables, they will be in an array. And the objects in the array are numbered from "0" (for the first), "1" (for the second), etc.
Now to get the ranges, you will need to create your own ranges. This is done with the command Range.new. Let's say you want a table selection that picks the 2nd, 3rd, and 4th row in the 2nd column of your table. The code to create these two ranges would look like this:
Code:
$rowRange = Range.new 1,3
$columnRange = Range.new 1,1

Note that again the table cells are numbered from 0, so the 2nd cell is number "1". The first command creates a range spanning 3 (rows), starting from the 2nd. The second command creates a range spanning 1 (column), starting from the 2nd.
Now we can put all the bits together, create a selection object and make the selection.
Code:
$doc = Document.active
$table = $doc.text.tables[1]
$rowRange = Range.new 1,3
$columnRange = Range.new 1,1
$sel = TableSelection.new $table, $rowRange, $columnRange
$doc.setSelection $sel

Whew!

Now here's the first thing. This code will only work right, if all the numbers in the code fit. If you don't have at least two tables, or if the 2nd table is too small for this selection, you will get an error. The code is also not flexible, so it can't deal with unforeseen situations. Maybe you want to select, not 3 rows, but all rows until the end. Or maybe you want to select not starting in the second rwo of the second table, but whatever row, in whatever table is currently selected There are ways to deal with all of this, but before I extend this already long explanation, I would have to know whether this is TMI or not.

Finally to complete, what you want, you would have to:
  1. Copy/cut the cells you have selected
  2. Make a second selection, in the place you want to put things
  3. Paste the cells
This is where it might be easier to simply copy the content of cells, one at a time (in the right order, so you don't lose anything). The efficiency of doing things by macro is sometimes different from that of doing things by hand.

_________________
philip


2016-06-22 18:00:11
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Here is a macro that will move stuff down in table, starting from the selected cell.


Attachments:
Move stuff down in Table.nwm [18.59 KiB]
Downloaded 67 times

_________________
philip
2016-06-22 18:43:24
Profile

Joined: 2016-06-21 09:31:11
Posts: 5
Location: France
phspaelti wrote:
Here is a macro that will move stuff down in table, starting from the selected cell.


Thank you phspaelti, but the macro stops at line #22.


2016-06-23 01:30:29
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Fred90s wrote:
phspaelti wrote:
Here is a macro that will move stuff down in table, starting from the selected cell.


Thank you phspaelti, but the macro stops at line #22.

What version of Nisus are you using?

_________________
philip


2016-06-23 01:56:42
Profile

Joined: 2016-06-21 09:31:11
Posts: 5
Location: France
phspaelti wrote:
Hello Fred,
To do what you are wanting to do you will need to use the macro language, and in particular the Nisus object model. A Nisus document is actually a complicated structure known as an object, which itself contains a whole series of other objects: the text, the notes, the comments, the floating content, etc. Each of these objects can in turn contain yet further objects. The tables themselves are actually inside the text object(s) of the document.


Thanks a lot for the lesson! Exactly what I needed. And a good reminder that the first item is actually #0...

I'll work all that out and I'll let you know.

PS : I use Nisus since Word for Mac dropped its macros years ago. I use Nisus to process tables with time code and translated text (I work as a translator) into formatted text. I've already done a few macros which perform that task in a single click instead of doing it line per line like my fellow translators do. No doubt your message will help me produce a "cleaner" code from now on...


2016-06-23 02:01:52
Profile

Joined: 2016-06-21 09:31:11
Posts: 5
Location: France
phspaelti wrote:
What version of Nisus are you using?


2.0.3


2016-06-23 02:02:47
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Fred90s wrote:
phspaelti wrote:
What version of Nisus are you using?


2.0.3

Is 2.1.4 a paid upgrade for you? Even if, it will be worth your while, if you write macros. I could try to rewrite the macro for older versions, but I'm starting to forget the old syntax :P

Addendum:
Here's a rewrite that should work for 2.0 (no guarantees).
Attachment:
Move stuff down in Table (NWP 2_0).nwm [18.82 KiB]
Downloaded 63 times

_________________
philip


2016-06-23 02:21:07
Profile

Joined: 2016-06-21 09:31:11
Posts: 5
Location: France
phspaelti wrote:
Is 2.1.4 a paid upgrade for you? Even if, it will be worth your while, if you write macros. I could try to rewrite the macro for older versions, but I'm starting to forget the old syntax :P


Upgrade was free, I did it and your macro worked great then. Assigned a shortcut to it. It will save me huge amounts of time (That's what computers were supposed to do be made for, remember...).

Thanks a lot for your time and help.


2016-06-23 03:58:53
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software