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_XLLFN3(MyFillRange2, MyFillRange2_4, MyFillRange2_12, "RBB", "UBB", L"MyFillRange2", 0, L"Start,End", 0, L"14", 0, L"Return a series interpola" L"ted between start and end values", 0, L"Start value for series\0End valu" L"e for series\0", 0, 0, L"{MyFillRange2,,,Return a series interpolated be" L"tween start and end values,14,1,128,U,{{0,{Start,Double,0,,Start value f" L"or series,,,,}},{0,{End,Double,0,,End value for series,,,,}}},{},3,,0,0}", 1) CXlOper* MyFillRange2_Impl(CXlOper&, double, double); extern "C" __declspec(dllexport) LPXLOPER12 MyFillRange2_12(double Start, double End) { XLL_FIX_STATE; CXlOper xloResult; try { CXlStructuredExceptionHandler _seh_; xloResult.HandleResult(MyFillRange2_Impl(xloResult, Start, End)); } catch(const CXlRuntimeException& ex) { CXllApp::Instance()->DisplayException(xloResult, ex); } return xloResult.Ret12(); } extern "C" __declspec(dllexport) LPXLOPER4 MyFillRange2_4(double Start, double End) { XLL_FIX_STATE; CXlOper xloResult; try { CXlStructuredExceptionHandler _seh_; xloResult.HandleResult(MyFillRange2_Impl(xloResult, Start, End)); } catch(const CXlRuntimeException& ex) { CXllApp::Instance()->DisplayException(xloResult, ex); } return xloResult.Ret4(); } CXlOper* MyFillRange2_Impl(CXlOper& xloResult, double Start, double End) { // End of generated code //}}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 // Start and End nCells = usRows * usCols; dInc = (nCells > 1) ? ( (End - Start) / (nCells - 1) ) : 0.0; dValue = Start; 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(); }
CXllApp::GetCallerDims | CXlOper::AllocArray | CXlOper::Cell