Select contents of Table Cell

Get help using and writing Nisus Writer Pro macros.
Post Reply
Bob Stern
Posts: 170
Joined: 2006-03-12 12:32:47

Select contents of Table Cell

Post by Bob Stern »

The result of the macro commands "Select Table Cell n, m" and "Select Relative Table Cell n, m" is the cell contents preceded and followed by a Return.

How can I avoid these return characters being included?

Alternatively, what macro steps or Perl block will strip the leading and trailing returns? I don't see any macro commands to do string manipulation. (I'm a Perl neophyte, so please don't assume any knowledge in your response.)

Thanks!
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Post by martin »

Yes, those commands will select all of the cell's content, whatever it may be. Once a selection has been made (eg: via the Select Table Cell command), here's some code to have the selection exclude newlines:

Code: Select all

$limitedLoc = Selection Location
$limitedLen = Selection Length

# strip newlines from the start
If Find '^\n+', 'Es'
    $newlineCount = Selection Length
    $limitedLoc += $newlineCount
    $limitedLen -= $newlineCount
End

# strip newlines from the end
Set Selection $limitedLoc, $limitedLen
If Find '\n+$', 'Es'
    $newlineCount = Selection Length
    $limitedLen -= $newlineCount
End

Set Selection $limitedLoc, $limitedLen
Bob Stern
Posts: 170
Joined: 2006-03-12 12:32:47

Bug in "Copy Table Text" & "Copy Text Onl

Post by Bob Stern »

Thanks, Martin!

I now see that my problem is caused by inconsistencies among the results of three Copy commands if used after an entire table cell is selected. The inconsistency occurs irrespective of whether the commands are invoked manually or in a macro.

Specifically, suppose a table cell includes one or more words without any newline characters.

Suppose I position the cursor in that cell and then select the entire contents of the cell using either the menu command "Table:Select:Cells" or the macro equivalent "Select Relative Table Cell 0, 0".

If I perform Edit:Copy:Copy (command-C) and then paste the clipboard into an empty document window, I get the contents of the table cell as expected.

However, if I perform Edit:Copy:Copy Text Only,
I get the contents of the table cell preceded by a spurious return and followed by another spurious return.

If I perform Table:Copy Table Text,
I get the contents of the table cell followed by a spurious return.

It appears to me that the returns inserted by "Copy Text Only" and "Copy Table Text" represent bugs.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Post by martin »

The additional newlines are indeed a bug- I'll file a report. The reason they appear is because copying the table cells as text can insert newlines before/after each table row.

If you're always working with exactly one table cell, this code should do what you want by bypassing the smart text conversion:

Code: Select all

$text = Read Selection
$text = Cast to String $text, false
Write Clipboard $text
Post Reply