Author |
Topic |
|
couturier
France
291 Posts |
Posted - 04/11/2017 : 1:10:34 PM
|
Origin Ver. and Service Release (Select Help-->About Origin): 2017SR1 Operating System:win7
Hi, I want to find row indexes in which a partial string is found. Rather than looping over all the cells and testing content with str.Match(*my string*), I was wondering if I could get the result faster with a string array variable. Unless I missed something, I can't use wildcards with the string array find method. Is it possible ?
thanks |
|
YimingChen
1603 Posts |
Posted - 04/11/2017 : 5:31:57 PM
|
Hi,
Origin has GUI way to search in a worksheet for partial string using wild card. Select in menu bar Edit: Find in sheet... to open the dialog. Set the Data Type to String and check the Box besides Use wildcards. Now you can search using wildcard. Please let us know if this works for your case, thank you.
Yiming
|
|
|
couturier
France
291 Posts |
Posted - 04/11/2017 : 5:35:26 PM
|
Thank you very much for your help. The thing is I'd like to do it with script :-/ |
|
|
cpyang
USA
1406 Posts |
Posted - 04/11/2017 : 6:04:09 PM
|
We will post OC code shortly, and can be called from LT.
CP
|
|
|
YimingChen
1603 Posts |
Posted - 04/12/2017 : 10:51:41 AM
|
Hi,
Here's a piece of OC code that searches using wildcard of a certain column. The searched row indices are saved into a vector. To run the function in Labtalk, first compile the function in OriginC builder. Then in the Labtalk script window, run the function with arguments of column number and wildcard expression, e.g. , search_worksheet(0, "*d*") . It will print out the row indices. Hope this helps. Thank you.
void search_worksheet(int nCol, string StrFind)
{
DataRange dr;
Datasheet ds;
UINT nBitwiseOptions;
uint wBitwiseOptions = WKSREPL_TEST_STR_WILDCARD_MATCHING;
int bRet;
bRet = get_active_sheet_range_ex(dr, 0, &ds, true, O_QUERY_BOOL(nBitwiseOptions, WKSREPL_CHECK_FIND_LABEL_ROWS));
if(bRet)
{
Worksheet wks(ds);
int nRow = -10000;
int nRowPrev = nRow;
vector<int> rowIndex;
while(true)
{
dr.FindNext(wks, nCol, nRow, StrFind, wBitwiseOptions, -1, NULL, 1);
if (nRow < nRowPrev) break;
rowIndex.Add(nRow);
nRowPrev = nRow;
}
for(int n = 0 ; n<rowIndex.GetSize(); n++)
printf("%d ", rowIndex[n]);
}
}
------ Yiming |
Edited by - YimingChen on 04/12/2017 10:52:05 AM |
|
|
couturier
France
291 Posts |
Posted - 04/12/2017 : 11:16:37 AM
|
thanks a lot |
|
|
cpyang
USA
1406 Posts |
Posted - 04/12/2017 : 8:56:06 PM
|
I will put the following version into OC header for documentation.
//wildchar search column for string and dump the row index
//col_find_rows 2 *abc*
void col_find_rows(int nCol, string StrToFind)
{
Worksheet wks = Project.ActiveLayer();
nCol--;//OC index from 0, LT from 1
if(nCol < 0 || nCol > wks.GetNumCols()-1)
{
out_int("invalid col index: ", nCol+1);
return;
}
char szBookSName[100];
LT_get_str("%H", szBookSName, 100);//active book from %H
string strBookRange;
strBookRange.Format("[%s]",szBookSName);
DataRange dr;//need a Book datarange to search a sheet at specified column
dr.Create(strBookRange);
int nRow = DATARANGE_FIND_REPLACE_INVALID_ROW_INDEX;
int ii = 1;
int nRowPrev = nRow;
while(true)
{
int nRet = dr.FindNext(wks, nCol, nRow, StrToFind, WKSREPL_TEST_STR_WILDCARD_MATCHING);
if(!( nRet & DATARANGE_FIND_NEXT_SUCCESSFULLY_FOUND))
break;//not found
if (nRow < nRowPrev)
{
out_int("going back to beginning again: ", nRow+1);
break;
}
nRowPrev = nRow;
printf("%d: found at row %d\n", ii++, nRow+1);
if(ii > 10) // testing code, don't show too many
break;
}
}
CP
|
|
|
|
Topic |
|
|
|