CGridView:GetType CGridView:GotoCell

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.

Syntax

Val, data_type = CGridView:GetVal( nCol, nRow )

bullet.gif    Where nCol, and nRow are the coordinates of the cell to get.

bullet.gif    Val is the cell contents. The type of information returned as Val varies. See Remarks, below.

bullet.gif    data_type is a numeric code that identifies the type of data being returned in Val. See Remarks, below.

bullet.gif    On failure, nil, nil is returned.

Remarks

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

Boolean true or false

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

Avoiding Side Effects

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:

1. Test the returned data_type

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.

2. Remember that 2 parameters are returned

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:

G = attach_gridview()

-- attach a grid on the Mira desktop

L = new_lsqfit()

-- create a new CLsqFit object

L:AddPt(3.5, G:GetVal(5,10))

-- 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:

G = attach_gridview()

-- attach a grid on the Mira desktop

L = new_lsqfit()

-- create a new CLsqFit object

y, type = G:GetVal(5,10)

-- get the type but ignore it here

L:AddPt(3.5, y)

-- 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():

G = attach_gridview()

-- attach a grid on the Mira desktop

L = new_lsqfit()

-- create a new CLsqFit object

L:AddPt(3.5, G:GetNum(5,10))

-- GetNum returns only one value which is passed as AddPt(x,y)

Example

The following script returns the cell contents in (column 5, row 10):

G = attach_gridview()

-- attach a grid on the Mira desktop

Val, type = G:GetVal(5,10)

-- return the value and type for cell (5,10)

if type == 1 then

-- if this is a string value...

  Printf("value = %s\n", Val)

-- print a string

elseif type == 2 then

-- else if this is a numeric value...

  Printf("Val = %d\n", value)

-- print a number

end

 

Related Topics

CGridView class

SetVal

GetNum

GetStr

GetType

 


Mira Pro x64 Script User's Guide, v.8.73 Copyright Ⓒ 2024 Mirametrics, Inc. All Rights Reserved.