XLL+ Class Library

CXlOper::GetCallerDims() Example

The following example demonstrates GetCallerDims. It looks at the size of the range of cells that called the function, and fills them with evenly interpolated values between the start and end values supplied. If it was not called from a cell or cells, but from a command or an event handler, then it gracefully returns #NA.

// Function:    MyFillRange2
// Returns:     LPXLOPER
// Description: Return a series interpolated between start and end values

//{{XLP_SRC(MyFillRange2)
    // NOTE - the FunctionWizard will add and remove mapping code here.
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN2(MyFillRange2, "RBB", "MyFillRange2", "Start,End"
    , "User Defined", "Return a series interpolated between"
    " start and end values", "Start value for series\000End value"
    " for series\000", "\0\0", 1)

extern "C" __declspec( dllexport )
LPXLOPER MyFillRange2(double dStart, double dEnd)
{
    CXlOper xloResult;
//}}XLP_SRC

    USHORT usRows, usCols;
    double dValue, dInc;
    int nCells;

    // Get dimensions of caller. Fail if it is not a range of cells
    if (!CXllApp::GetCallerDims(usRows, usCols))
        return CXlOper::RetError(xlerrNA);

    // Create the result array
    xloResult.AllocArray(usRows, usCols);
    
    // Fill it with evenly spaced values between 
    // dStart and dEnd
    nCells = usRows * usCols;
    dInc = (nCells > 1) ? ( (dEnd - dStart) / (nCells - 1) ) 
                        : 0.0;
    dValue = dStart;
    for ( int i = 0; i < usRows; i++ )
    {
        for ( int j = 0; j < usCols; j++ )
        {
            xloResult.Cell(i, j) = dValue;
            dValue += dInc;
        }
    }

    // Return the array, which will be exactly the 
    // right size and shape to fill the array formula
    // that called it
    return xloResult.Ret();
}

Uses

CXllApp::GetCallerDims | CXlOper::AllocArray | CXlOper::Cell