XLL+ Class Library

MySum Example

This example demonstrates how to iterate over array inputs.

// 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_XLLFN2(MySum, "BP", "MySum", "Input", "User Defined", 
    "Returns the sum of numeric cells in the input range", 
    "Input range\000", "\0", 1)

extern "C" __declspec( dllexport )
double MySum(const COper* lpopInput)
{
//}}XLP_SRC

    // Initialise total
    double dSum = 0.0;

    // Get the size of the array
    unsigned short cRows, cCols;
    lpopInput->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++ )
        {
            const COper& opItem = lpopInput->Cell(i, j);
            if ( opItem.IsDouble() )
                dSum += opItem.ToDouble();
        }
    }

    return dSum;
}

Uses

COper::GetDims | COper::Cell | COper::IsDouble | COper::ToDouble