2.2.4.46.67 Worksheet::SelectRowsSelectRows
Description
Select rows from worksheet based on a LabTalk condition expression
Syntax
int SelectRows( LPCSTR lpcszCondition, vector<uint> & vnRowIndices, int r1 = 0, int r2 = -1, int nMax = -1, LPCSTR lpcszBeforeLoopScript = NULL, LPCSTR lpcszBeforeIfScript = NULL, LPCSTR lpcszAfterIfScript = NULL, DWORD dwCntrl = 0, LPCSTR lpcszAfterLoopScript = NULL )
Parameters
- lpcszCondition
- [input] LabTalk conditional expression involving columns in the worksheet. LabTalk variable i is automatically declared and can be used for each row
- vnRowIndices
- [output] vector to receive the row indices, 0-offset. default is 0
- r1
- [input] starting row index, 0 offset. default is 0.
- r2
- [input] ending row index, 0 offset, inclusive, -1(by default) to indicate the end of data in worksheet
- nMax
- [input] optional maximum number of rows to select, -1(by default) to have no limit
- lpcszBeforeLoopScript
- [input] LabTalk script to be executed before the looping start, this is where you can make declarations to be used in lpcszCondition.
- default is NULL.
- lpcszBeforeIfScript
- [input] LabTalk script to be executed before the if test, this is where you can use temp variable to build more complex conditions.
- default is NULL.
- lpcszAfterIfScript
- [Input] LabTalk script to be executed after the if test, the if test result is in the an local int variable called _IF_TRUE.
- default is NULL.
- dwCntrl
- [input] bits to control the conditional expression, Can be:
- WKS_SELECT_ROWS_CHECK_IGNORE_HIDDEN_ROW,
- WKS_SELECT_ROWS_XL_COLUMN_NAME.
- lpcszAfterLoopScript
- [input] LabTalk script to be executed after the looping end.
- default is NULL.
Return
number of rows (size of vnRowIndices) if success, otherwise returns
-1 if lpcszCondition has syntax error
-2 if lpcszBeforeScript has syntax error
Examples
EX1
// example to test col(2) alias as a for missing values
// we first define the NULL const as 0/0 and then
// we setup alias a as col(2)
void Worksheet_SelectRows_Ex1(string strCond = "a==null", string strPre = "const null=0/0;range a=2")
{
Worksheet wks("Book1");
Worksheet wksDest("Book2");
vector<uint> vnRowIndices;
vector<uint> vnCols = {0,2};
int nn = wks.SelectRows(strCond, vnRowIndices, 0, -1, -1, strPre);
if(nn < 0)
out_str("User cancel");
else if(nn == 0)
out_str("no matching row");
else
{
BOOL bRet = wks.Extract(wksDest, vnRowIndices, vnCols);
if(bRet)
out_str("done");
}
}
EX2
// example on how to select cells by matching text
// wildcard support is automatic if either * or ? characters are found in the
// testing string.
//
// To try, put data into a sheet with some text in col(2)
// this function can allow you to select rows by a string
// like
// Worksheet_SelectRows_Ex2 "my string"
// Worksheet_SelectRows_Ex2 *Boston*
void Worksheet_SelectRows_Ex2(string strCell = "*about*")
{
string strPre = "range b=2";
string strCond = "b==";
strCond += "\"" + strCell + "\"";
Worksheet wks = Project.ActiveLayer();
vector<uint> vnRowIndices;
int nn = wks.SelectRows(strCond, vnRowIndices, 0, -1, -1, strPre);
Grid gg;
if(gg.Attach(wks))
{
vector<int> vnRows;
vnRows = vnRowIndices;
if(nn)
gg.SetSelection(vnRows);
else
gg.SetSelection(NULL);
out_int("Number of rows =", nn);
}
}
Remark
See Also
Worksheet::Extract, Grid::SetSelection, Worksheet::SetSelectedRange
Header to Include
origin.h
|