DataRange::FindNext

Description

Find next cell for numierc value
Find next cell for string value

Syntax

int FindNext(Datasheet& ds, int& nCol, int& nRow, double dThresholdVal,
 uint wBitwiseOptions = WKSREPL_TEST_EQUAL, int nIndex = -1,
 double rTolerance = 1e-8, double* pdReplaceVal = NULL,
 BOOL bUndo = FALSE, int* pnMatrixObjectIndex = NULL)
int FindNext(Worksheet& wks, int& nCol, int& nRow, LPCSTR lpcszOld,
 uint wBitwiseOptions = WKSREPL_TEST_STR_MATCH_WHOLE_WORD,
 int nIndex = -1, LPCSTR lpcszNew = NULL, BOOL bUndo = FALSE)

Parameters

ds
[input]Datasheet
nCol
[input]Column index
nRow
[input]Row index
dThresholdVal
[input]Compared with original data
wBitwiseOptions
[input]Options:
nIndex
[input]Range index
rTolerance
[input]Tolerance for Equal testing
pdReplaceVal
[input]Used to replace original data
bUndo
[input]Undo controller
pnMatrixObjectIndex
[input]Index of matrix object

wks
[input]Worksheet
nCol
[input]Column index
nRow
[input]Row index
lpcszOld
[input]Compare with original string
wBitwiseOptions
[input]Replace options:
nIndex
[input]Range index
lpcszNew
[input]Used to replace original string
bUndo
[input]Undo controller


Bitwise Options:

WKSREPL_TEST_LESSTHAN = 1,
WKSREPL_TEST_EQUAL = 2,
WKSREPL_TEST_GREATER = 4,
WKSREPL_USE_ABSOLUTE_VALUE_IN_TEST = 8,
WKSREPL_KEEP_ORIGINAL_SIGN_WHEN_TEST_RESULT_IS_TRUE = 16,
WKSREPL_SET_TO_MISSING_VALUE_WHEN_TEST_RESULT_IS_FALSE = 32,
WKSREPL_TEST_STR_MATCH_WHOLE_WORD = 64,
WKSREPL_TEST_STR_WILDCARD_MATCHING = 128,
WKSREPL_TEST_STR_CASE_SENSITIVE = 256,
WKSREPL_REPLACE_MATCHING_PART_ONLY = 0x00000200,
WKSREPL_FIND_NEXT_SEARCH_UPWARD = 0x00000400,
WKSREPL_SKIP_LINKED_CELL = 0x00000800,
WKSREPL_NUMERIC_NANUM_SKIP_BLANK_TEXT = 0x08000000,
WKSREPL_NUMERIC_FIND_BLANK = 0x10000000,
WKSREPL_NUMERIC_REPL_BLANK = 0x20000000,
WKSREPL_CHECK_FIND_LABEL_ROWS = 0x40000000,

Return

Returns an integer with one or more of the following bits set:

DATARANGE_FIND_NEXT_SUCCESSFULLY_FOUND = 0x00010000 DATARANGE_FIND_NEXT_INPUT_MATCHED_CONDITIONS = 0x00020000 DATARANGE_FIND_NEXT_SUCCESSFULLY_REPLACE_INPUT = 0x00040000 DATARANGE_FIND_NEXT_INVALID_ARGUMENT = 0x00000001

These constants are declared in OC_const.h

Examples

EX1

//Find a cell in worksheet which is equal or less than 50.0 according to the datarange index.
void DataRange_FindNext_Ex1()
{
    Worksheet wks;
    wks.Create();
    if (wks)
    {
        while (wks.Columns(0))
            wks.DeleteCol(0);
 
        wks.AddCol("A");
        wks.AddCol("B");
 
        for (int j = 0; j < 2; j++)
            for (int i = 0; i < 2; i++)
                wks.SetCell(i, j, rnd() * 100);
 
        DataRange dr;
        dr.Add("Range1", wks, 0, 0, -1, 0); // from row,col, to row,col
        dr.Add("Range2", wks, 0, 1, -1, 1); // from row,col, to row,col
 
        int nCol, nRow;
        uint nOptions = WKSREPL_TEST_LESSTHAN|WKSREPL_TEST_EQUAL;
        int nRangeIndex = 1;//Find in the Range1.
        
        int nRet = dr.FindNext(wks, nCol, nRow, 50.0, nOptions, nRangeIndex);
        printf("FindNext returned %d\n", nRet);
        printf("nCol == %d, nRow == %d\n", nCol, nRow);
    }
}

EX2

//Find a cell in worksheet which is whole match the string "Abc" with case-sensitive according to the datarange index.
void DataRange_FindNext_Ex2()
{
        Worksheet wks;
        wks.Create();
        if(wks)
        {
                wks.SetCell(0,0,"ab");
                wks.SetCell(1,0,"Ab");
                wks.SetCell(2,0,"abc");
                wks.SetCell(3,0,"Abc");
                
                DataRange dr;
                dr.Add(wks,0,"Range");
        
                
                int nCol, nRow;
                uint nOptions =WKSREPL_TEST_STR_CASE_SENSITIVE;//Match the string with case-sensitive.
                
                int nRangeIndex = 0;
        
        int nRet = dr.FindNext(wks, nCol, nRow, "Abc", nOptions, nRangeIndex);
        printf("FindNext returned %d\n", nRet);
        printf("nCol == %d, nRow == %d\n", nCol, nRow);
        }
}

EX3

//wildchar search column for string and dump the row index
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;
        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
                
                printf("%d: found at col %d row %d\n", ii++, nCol+1, nRow+1);
                if(ii > 10) // testing code, don't show too many
                        break;
        }
}

void DataRange_FindNext_Ex3()
{
        Worksheet wks;
        wks.Create("origin");
        vector<string> vs1 = {"ABCD", "d", "abcd", "f", "qqabc", "ab"};
        vector<string> vs2 = {"q", "qqabc", "e", "r", "t", "abcd"};
        wks.Columns(0).PutStringArray(vs1);
        wks.Columns(1).PutStringArray(vs2);

        col_find_rows(2, "*abc*");
}

Remark

See Also

Header to Include

origin.h