XLL+ Class Library (6.3)

CXlOper::ToDoubleArray() Examples

The first example demonstrates how to copy numeric values from an OPER to a single array of double's. The bByRows flag is set to FALSE, so columns are grouped together into the output array. The bStrict flag is set to FALSE, so non-numeric values are treated as zeroes.

CopyC++
//{{XLP_SRC(OperToDoubleArrayExample1) 
    // NOTE - the FunctionWizard will add and remove mapping code here. 
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN3(OperToDoubleArrayExample1, OperToDoubleArrayExample1_4, 
    OperToDoubleArrayExample1_12, "RP", "UQ", L"OperToDoubleArrayExample1", 0, 
    L"Arg1", 0, L"Example", 0, L"Demonstrates COper::ToDoubleArray() with a si"
    L"ngle array", 0, L"Argument 1\0", 0, 0, L"{OperToDoubleArrayExample1,,,De"
    L"monstrates COper::ToDoubleArray() with a single array,Example,1,128,U,{{"
    L"0,{Arg1,Value,0,,Argument 1,,,,}}},{},3,,0,0}", 1)
CXlOper* OperToDoubleArrayExample1_Impl(CXlOper&, const CXlOper*);

extern "C" __declspec(dllexport)
LPXLOPER12 OperToDoubleArrayExample1_12(LPXLOPER12 Arg1)
{
    XLL_FIX_STATE;
    CXlOper xloResult, Arg1__port(Arg1);
    try {
        CXlStructuredExceptionHandler _seh_;
        xloResult.HandleResult(OperToDoubleArrayExample1_Impl(xloResult, &
            Arg1__port));
    }
    catch(const CXlRuntimeException& ex) {
        CXllApp::Instance()->DisplayException(xloResult, ex);
    }
    return xloResult.Ret12();
}
extern "C" __declspec(dllexport)
LPXLOPER4 OperToDoubleArrayExample1_4(LPXLOPER4 Arg1)
{
    XLL_FIX_STATE;
    CXlOper xloResult, Arg1__port(Arg1);
    try {
        CXlStructuredExceptionHandler _seh_;
        xloResult.HandleResult(OperToDoubleArrayExample1_Impl(xloResult, &
            Arg1__port));
    }
    catch(const CXlRuntimeException& ex) {
        CXllApp::Instance()->DisplayException(xloResult, ex);
    }
    return xloResult.Ret4();
}

CXlOper* OperToDoubleArrayExample1_Impl(CXlOper& xloResult, const CXlOper* Arg1)
{
    // End of generated code 
//}}XLP_SRC

    BOOL bOk = TRUE;

    USHORT usRows, usCols, i;
    double *pdArray = 0, dTotal = 0.0;

    bOk = Arg1->IsArray();

    if ( bOk )
    {
        // Allocate enough memory, in one continuous block,  
        // to hold the results.
        Arg1->GetDims(usRows, usCols);
        pdArray = new double[usRows * usCols];

        // Copy the OPER's contents into an array of double's 
        // Columns are kept together (bByRows=FALSE) and  
        // non-numeric cell values are treated as zeroes  
        // (bStrict=FALSE).
        Arg1->ToDoubleArray(pdArray, usRows, usCols, 
                                FALSE, FALSE);

        // Do something useful (!) with the array 
        for ( i = 0; i < (usRows * usCols); i++ )
            dTotal += pdArray[i];

        // Clean up 
        delete[] pdArray;
    }

    if ( bOk )
        xloResult = dTotal;
    else
        xloResult = xlerrNA;

    return xloResult.Ret();
}

The second example demonstrates how to copy numeric values from an OPER to an array of pointers to double. The bByRows flag is set to FALSE, so columns are grouped together into the output array. The bStrict flag is set to TRUE, so non-numeric values are treated are not tolerated.

CopyC++
//{{XLP_SRC(OperToDoubleArrayExample2) 
    // NOTE - the FunctionWizard will add and remove mapping code here. 
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN3(OperToDoubleArrayExample2, OperToDoubleArrayExample2_4, 
    OperToDoubleArrayExample2_12, "RP", "UQ", L"OperToDoubleArrayExample2", 0, 
    L"Arg1", 0, L"Example", 0, L"Demonstrates COper::ToDoubleArray() with an a"
    L"rray of pointers", 0, L"Argument 1\0", 0, 0, L"{OperToDoubleArrayExample"
    L"2,,,Demonstrates COper::ToDoubleArray() with an array of pointers,Exampl"
    L"e,1,128,U,{{0,{Arg1,Value,0,,Argument 1,,,,}}},{},3,,0,0}", 1)
CXlOper* OperToDoubleArrayExample2_Impl(CXlOper&, const CXlOper*);

extern "C" __declspec(dllexport)
LPXLOPER12 OperToDoubleArrayExample2_12(LPXLOPER12 Arg1)
{
    XLL_FIX_STATE;
    CXlOper xloResult, Arg1__port(Arg1);
    try {
        CXlStructuredExceptionHandler _seh_;
        xloResult.HandleResult(OperToDoubleArrayExample2_Impl(xloResult, &
            Arg1__port));
    }
    catch(const CXlRuntimeException& ex) {
        CXllApp::Instance()->DisplayException(xloResult, ex);
    }
    return xloResult.Ret12();
}
extern "C" __declspec(dllexport)
LPXLOPER4 OperToDoubleArrayExample2_4(LPXLOPER4 Arg1)
{
    XLL_FIX_STATE;
    CXlOper xloResult, Arg1__port(Arg1);
    try {
        CXlStructuredExceptionHandler _seh_;
        xloResult.HandleResult(OperToDoubleArrayExample2_Impl(xloResult, &
            Arg1__port));
    }
    catch(const CXlRuntimeException& ex) {
        CXllApp::Instance()->DisplayException(xloResult, ex);
    }
    return xloResult.Ret4();
}

CXlOper* OperToDoubleArrayExample2_Impl(CXlOper& xloResult, const CXlOper* Arg1)
{
    // End of generated code 
//}}XLP_SRC

    BOOL bOk = TRUE;

    USHORT usRows, usCols, i, j;
    double **ppdArray = 0, dTotal = 0.0;

    bOk = Arg1->IsArray();

    if ( bOk )
    {
        // Allocate enough memory to hold the results, 
        // with each column in a block
        Arg1->GetDims(usRows, usCols);
        ppdArray = new double*[usCols];
        for ( j = 0; j < usCols; j++ )
            ppdArray[j] = new double[usRows];

        // Copy the OPER's contents into the arrays of  
        // double's. Columns are kept together  
        // (bByRows=FALSE) and non-numeric cell values  
        // are not tolerated (bStrict=TRUE).
        bOk = Arg1->ToDoubleArray(ppdArray, usRows, usCols, 
                                      FALSE, TRUE);
    }

    if ( bOk )
    {
        // Do something useful (!) with the array 
        for ( i = 0; i < usRows; i++ )
            for ( j = 0; j < usCols; j++ )
                dTotal += ppdArray[j][i];

        // Clean up 
        for ( j = 0; j < usCols; j++ )
            delete[] ppdArray[j];
        delete[] ppdArray;
    }

    if ( bOk )
        xloResult = dTotal;
    else
        xloResult = xlerrNA;

    return xloResult.Ret();
}

Uses

CXlOper::ToDoubleArray | CXlOper::IsArray