Copy/paste

3.18K viewsScripting
0

Hi. Im trying to copy from one matrix to another using a script.

In my attempt l used the following script

|Sorting::*|.values = |Deliveries::*|.values

This one does well in copying figure to the other matrix but the challenge is that in the Sorting matrix l sort the records by date(using sort feature) which is an item. ..so wen l use the above script. The records will not come sorted on the Deliveries matrix…..

Please help me …with a code to copy cells only and paste to an identical matrix.

Regards,
Tafadzwa

0

Hi Tafadzwa,

Sorry to take so long in responding to your post.

Quantrix has two kinds of selections:

1) [b:2kxub6n7]Node[/b:2kxub6n7] selections, which select the contents of groups or items. Examples:

[code:2kxub6n7]|Matrix1::A1|
|Matrix1::A1*|
|Matrix1::A|
|Matrix1::A1:B1|[/code:2kxub6n7]

2) [b:2kxub6n7]Cell[/b:2kxub6n7] selections, which select regions of cells in a matrix. Example:

[code:2kxub6n7]
/* Selects all cells between row 1, column 1 and row 4, column 1. The first
* row or column has index 1, not 0.
*/
|Matrix1::<1, 1>-<4, 1>|
[/code:2kxub6n7]

The important difference is that node selections give you the values without sorting, while cell selections give you the values as you see them in the matrix, with sorting.

Note: Even though [font=”Courier New”]|Matrix1::A1*|[/font] uses the * character to indicate “cells only”, this is deceptive. Quantrix still gives you the values without sorting, because this is fundamentally a node selection. The distinction here between cells only and nodes only is mostly for applying formatting to a matrix, where “cells only” will exclude the item header.

If you use a cell selection, you should be able to copy and paste with sorting. To make a cell selection, you need to know how many rows and columns you want to select. If you know ahead of time how large your matrix is, you can hard-code the correct numbers into the cell selection. For a matrix with 10 rows and 2 columns, that would look like this:

[code:2kxub6n7]|Matrix1::<1, 1>-<10, 2>|[/code:2kxub6n7]

You can then copy and paste the selection as follows:

[code:2kxub6n7]/* Note: Assumes that Matrix2 is large enough to receive the values.
* You will get an error if not.
*/

|Matrix1::<1, 1>-<10, 2>|.copy()
|Matrix2::<1, 1>-<10, 2>|.paste()[/code:2kxub6n7]

Another option is to count the number of items in the row and column categories using a script, which will allow you to write one script that will continue to work even if you change the number of rows or columns. I have attached an example that does this. You can generate the selection string (that is, “Matrix1::<a, b>-<c, d>”) dynamically, then pass it to the [font=”Courier New”]getSelection()[/font] method of [font=”Courier New”]Matrix[/font] to get the result.

Please see the attached sample model for a complete implementation of this approach. Feel free to ask follow-up questions if anything is unclear.

Hope this helps.
-Ben