XLL+ Class Library (6.3)

MySum Example

This example demonstrates how to iterate over array inputs.

CopyC++
// Function:    MySum 
// Returns:     double 
// Description: Returns the sum of numeric cells in the input range 
 
//{{XLP_SRC(MySum) 
    // NOTE - the FunctionWizard will add and remove mapping code here. 
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN3(MySum, MySum_4, MySum_12, "BP", "BQ", L"MySum", 0, L"Input", 0,
    L"14", 0, L"Returns the sum of numeric cells in the input range", 0, L"Inp"
    L"ut range\0", 0, 0, L"{MySum,,,Returns the sum of numeric cells in the in"
    L"put range,14,1,0,B,{{0,{Input,Value,0,,Input range,,,,}}},{},3,,0,0}", 1)
double MySum_Impl(const CXlOper*);

extern "C" __declspec(dllexport)
double MySum_12(LPXLOPER12 Input)
{
    XLL_FIX_STATE;
    CXlOper Input__port(Input);
    return MySum_Impl(&Input__port);
}
extern "C" __declspec(dllexport)
double MySum_4(LPXLOPER4 Input)
{
    XLL_FIX_STATE;
    CXlOper Input__port(Input);
    return MySum_Impl(&Input__port);
}

double MySum_Impl(const CXlOper* Input)
{
    // End of generated code 
//}}XLP_SRC 
 
    // Initialise total 
    double dSum = 0.0;

    // Get the size of the array 
    unsigned short cRows, cCols;
    Input->GetDims(cRows, cCols);

    // Iterate through array, summing the values of cells 
    for ( unsigned short i = 0; i < cRows; i++ )
    {
        for ( unsigned short j = 0; j < cCols; j++ )
        {
            CXlConstCell opItem = Input->Cell(i, j);
            if ( opItem.IsDouble() )
                dSum += opItem.ToDouble();
        }
    }

    return dSum;
}

Uses

CXlOper::GetDims | CXlOper::Cell | CXlOper::IsDouble | CXlOper::ToDouble