CGridView:GetVal
The GetVal method returns the contents and data type of a grid cell.
Note: Use this method if the cell's data type is unknown, then test the returned data type. If the data type is known, use GetNum or GetStr.
Val, data_type = CGridView:GetVal( nCol, nRow )
Where nCol, and nRow are the coordinates of the cell to get.
Val is the cell contents. The type of information returned as Val varies. See Remarks, below.
data_type is a numeric code that identifies the type of data being returned in Val. See Remarks, below.
On failure, nil, nil is returned.
The returned cell data_type is a numeric code for the type of data returned as Val. Use this number to interpret Val and choose the appropriate conversion for use in the script. The following table shows how to interpret the returned data_type code to process the data returned in Val:
Data_Type |
Interpretation |
1 |
String |
2 |
Number |
3 |
|
4 |
Time, as a string |
5 |
Currency, as a string |
10 |
Special time (hh:mm:ss.sss string) |
11 |
Special date (yyyy/mm/dd string) |
12 |
Special degrees (dd:mm:ss.sss or +dd°mm'ss.sss" string) |
other (1) |
String |
The GetVal() method provides a versatile way to fetch any type of data from a grid cell. However, with this versatility comes 2 possible side effects:
Unless you are certain of the data type in the cell at (nCol,nRow), use the data_type parameter to determine how to interpret the value returned in Val. For example, most grids assume a string data type for the cells and, in this case, the return type is String, which is data_type = 1.
If you know the cell data type, use GetNum or GetStr. Otherwise, test the value of data_type and continue the script accordingly.
Be careful when directly passing the return of GetVal as a parameter in another function call if that function can take a variable number of parameters. Since this method returns 2 values, you need to take care that the function does not unintentionally use the second return parameter. There are not many situations where this may arise, but one of them is described below.
For example, CLsqFit:AddPt() can accept 2 to 4 parameters, as in AddPt(x,y) and AddPt(x,y,z). Since the CGridView:GetVal method returns 2 values, placing GetVal() directly in the call to AddPt() will result in a y and z parameters being passed when you want only the y parameter. Suppose G is a CGridView object and L is a CLsqFit object, and you want to add the value from cell (5,10) at x=3.5 in a least squares fit. The following code will confuse AddPt into taking a z value when none is intended:
|
-- attach a grid on the Mira desktop |
|
-- create a new CLsqFit object |
|
-- WRONG! AddPt sees this as AddPt(3.5, y, z), not AddPt(3.5, y) |
If you want to use only the first of the 2 return values, you can assign them to values and pass only the first value, like this:
|
-- attach a grid on the Mira desktop |
|
-- create a new CLsqFit object |
|
-- get the type but ignore it here |
|
-- AddPt uses only the y value as AddPt(x,y) |
Another way to workaround is to use CGridView:GetNum() and CGridView:GetStr() as they return only the cell's value as a number or string, respectively. Here is an example using GetNum():
|
-- attach a grid on the Mira desktop |
|
-- create a new CLsqFit object |
|
-- GetNum returns only one value which is passed as AddPt(x,y) |
The following script returns the cell contents in (column 5, row 10):
|
-- attach a grid on the Mira desktop |
|
-- return the value and type for cell (5,10) |
|
-- if this is a string value... |
|
-- print a string |
|
-- else if this is a numeric value... |
|
-- print a number |
|
|
Mira Pro x64 Script User's Guide, v.8.73 Copyright Ⓒ 2024
Mirametrics, Inc. All Rights Reserved.