The following example demonstrates GetCaller. 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: MyFillRange
// Returns: LPXLOPER
// Description: Return a series interpolated between start and end values
//{{XLP_SRC(MyFillRange)
// NOTE - the FunctionWizard will add and remove mapping code here.
// DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN2(MyFillRange, "RBB", "MyFillRange", "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 MyFillRange(double dStart, double dEnd)
{
CXlOper xloResult;
//}}XLP_SRC
CXlOper xloCaller;
double dValue, dInc;
int nCells;
// Get caller. Fail if it is not a range of cells
if ( ( xloCaller.GetCaller() != 0 ) || !xloCaller.IsRef() )
{
xloResult = xlerrNA;
return xloResult.Ret();
}
// Get the first (and only) reference from
// the caller CXlOper
CXlRef& xlrefCaller = xloCaller.GetRefItem(0);
// Create the result array
xloResult.AllocArray(xlrefCaller.Height(),
xlrefCaller.Width());
// Fill it with evenly spaced values between
// dStart and dEnd
nCells = xlrefCaller.Height() * xlrefCaller.Width();
dInc = (nCells > 1) ? ( (dEnd - dStart) / (nCells - 1) )
: 0.0;
dValue = dStart;
for ( int i = 0; i < xlrefCaller.Height(); i++ )
{
for ( int j = 0; j < xlrefCaller.Width(); 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();
}
CXlOper::GetCaller | CXlOper::IsRef | CXlOper::GetRefItem | CXlOper::AllocArray | CXlRef | CXlRef::Height | CXlRef::Width | CXlOper::Cell