Page 1 of 2
Data Grid Parsing
Posted: Fri Jun 14, 2013 11:07 pm
by buchacho
I am trying to understand how to use data grids, but I did not get to far with the tutorials...
I have a data grid that contains several columns. Let's say I want to:
- Go down sequentially row by row
Get the value in the first row and first column and put it into a variable
Get the character value in the next column
Then go to another reference table to find a match to that value
Then pull a value from another column in that row
So in the example below, it would go to the "master grid" grab QWERTY, then find it associated with "3". Then the script would go to the "reference grid", go down the first column, until it found "3" and then grab "pie" and put it into a variable. How can I do this? Thanks!
Master Grid
QWERTY 3
WXYZ 4
Reference Grid
1 Cookie
2 Cake
3 Pie
4 Pudding
Re: Data Grid Parsing
Posted: Sat Jun 15, 2013 8:37 am
by bangkok
Many ways to do what you want. Here is one simple.
-put the content of the 2 datagrids in 2 variables
put the dgText of group "master grid" into tMaster
put the dgText of group "reference grid" into tReference
-Now, you can, as you say, pick up the first row, and second column for instance
set itemdelimiter to tab
put item 2 of line 1 of tMaster into tChunk
-Now, you can look for tChunk in tReference. Again many ways to do this. Here is one method.
replace cr with tab in tReference
set the wholeMatches to true
put itemOffset(tChunk,tReference) into tResult
if tResult <>0 then
put item (tResult +1) of tReference into tFinish
answer tFinish
end if
This method has flaws. You could use arrays. You could even use SQLlite
Arrays are very powerfull.
If you put the content of reference in array It would make :
put "COOKIE" into reference[1]
put "CAKE" into reference[2]
put "PIE" into reference[3]
Then, it's very easy to pick up a cell by its number
put reference[tChunk] into tResult
Anyway. Here is a small stack with both methods.
Re: Data Grid Parsing
Posted: Mon Jun 17, 2013 8:35 pm
by buchacho
Thanks for your reply!
I have another related question. When I try to re-size the columns of my data grid in the property editor, the rendering of the grid is all messed-up and looks jumbled. Why does this happen, and how can I refresh the rendering of the table so that it looks correct? Thanks!
Re: Passing only selected data from Data Grid
Posted: Thu Jun 20, 2013 11:15 pm
by buchacho
I have a script that gets the entire data grid an then puts it into an array:
Code: Select all
put the dgData of group "Grid" into GridArray
Now I want to pass the data from only the selected lines of the data grid into the variable (not the entire grid). Is there a simple way of coding that action?
Re: Passing only selected data from Data Grid
Posted: Fri Jun 21, 2013 11:45 am
by Klaus
HI buchacho,
buchacho wrote:Now I want to pass the data from only the selected lines of the data grid into the variable (not the entire grid).
Is there a simple way of coding that action?
...
## Get all selected INDICES = a COMMA separated list of (line) numbers: 2,5,9,188
put the dghilitedindexes of grp "Grid" into tIndices
## Get complete data:
put the dgData of group "Grid" into GridArray
## Now loop through the indices:
repeat with i = 1 to the num of items of tIndices
put GridArray
into tFirstSelectedIndexArray
## etc...
## You get the picture
...
Best
Klaus
Re: Data Grid Parsing
Posted: Tue Jun 25, 2013 1:22 am
by buchacho
Thanks for the suggestion. It mostly makes sense, but when I try to implement it, it does not work properly. I am thinking the repeat portion is missing something... Does it need to reference tIndices again? And does every column of the row need to be called out and transferred?
Re: Data Grid Parsing
Posted: Tue Jun 25, 2013 11:49 am
by Klaus
Hi buchacho,
sorry, I made a mistake in my script:
...
## Now loop through the indices:
repeat with i = 1 to the num of items of tIndices
put GridArray[item i of tIndices] into tAnyVariable
...
What exactly do you want to extract form the selected rows?
Best
Klaus
Re: Data Grid Parsing
Posted: Tue Jun 25, 2013 7:33 pm
by buchacho
Thanks for the clarification. I would like to copy the complete row (each column) and preserve the column names. I am wondering if this can be done without calling out every single column in the table. Essentially, the exact result of the following code, except only with the highlighted/selected rows of the data grid:
Code: Select all
put the dgData of group "Grid" into GridArray
Re: Data Grid Parsing
Posted: Tue Jun 25, 2013 8:45 pm
by Klaus
Hi buchacho,
ah, i see.
Create a new array containing only the currently selecte rows:
Code: Select all
...
## Get all selected INDICES = a COMMA separated list of (line) numbers: 2,5,9,188
put the dghilitedindexes of grp "Grid" into tIndices
put the dgData of group "Grid" into GridArray
## Now loop through the indices:
repeat with i = 1 to the num of items of tIndices
put GridArray[item i of tIndices] into tNewArray[i]
end repeat
...
Now tNewArry will contain only the data from the selected rows:
tNewArray[1]["name of your first column"]
tNewArray[1]["name of your seocnd column"]
...
tNewArray[2]["name of your first column"]
tNewArray[2]["name of your seocnd column"]
etc...
Best
Klaus
Re: Data Grid Parsing
Posted: Tue Jun 25, 2013 9:03 pm
by buchacho
I do not think this is a valid way of transferring rows between the two arrays since the rest of my script acts as if the resultant tNewArray is empty.
Code: Select all
put GridArray[item i of tIndices] into tNewArray[i]
Re: Data Grid Parsing
Posted: Tue Jun 25, 2013 10:08 pm
by Klaus
Can you please post the rest of your script(s)?
Re: Data Grid Parsing
Posted: Wed Jun 26, 2013 1:50 am
by buchacho
Ooops, I think I did not have any lines selected so now it is working.
One more question I have: for a multi-dimensional array, how do I get the number of rows to be able to reference for a repeat function?
repeat with i=1 to the number of rows in tNewArray
...
end repeat
Thanks!
Re: Data Grid Parsing
Posted: Wed Jun 26, 2013 10:54 am
by Klaus
Hi buchacho,
do you mean the "keys" of an array?
...
put the num of keys of tNewArray into tNumOfKeys
...
Best
Klaus
Re: Data Grid Parsing
Posted: Wed Jun 26, 2013 5:54 pm
by buchacho
Yes, I think so (keys), however, the command results in an error. Does it work with multidimensional arrays?
Re: Data Grid Parsing
Posted: Wed Jun 26, 2013 5:59 pm
by Klaus
Hi buchacho,
buchacho wrote:Yes, I think so (keys), however, the command results in an error.
what error? what script do you use?
buchacho wrote:Does it work with multidimensional arrays?
Yes!
Best
Klaus