HOW TO: How do I filter a vector input to read only numeric cells?
Reference: Q0024
Article last modified on 28-Mar-2006
The information in this article applies to:
- XLL+ for Visual Studio 2005 - 5.0
- XLL+ for Visual Studio .NET - 4.2, 4.3.1, 5.0
- XLL+ for Visual Studio 6 - 3, 4.1, 4.2, 4.3.1, 5.0
HOW TO: How do I filter a vector input to read only numeric cells?
Question
How do I extract only the numeric values from an input vector?
Answer
Use the COper::Cell(...)
method to inspect each cell, and add the
contents to your input variable only if the cell is numeric.
-
In the Function Wizard, set the type to be
COper
. - In your add-in function declare a local variable of type vector<T> to accept the input values.
-
Use
COper::IsVector()
to ensure that your input is a vector (or a single cell, which is the trivial case of a vector). -
Call
COper::GetVectorCount()
to get the number of cells in the unfiltered vector. -
Use
COper::Cell(...)
to inspect a cell directly. -
Use
COper::IsDouble()
to test the contents of each cell.
The example functions below demonstrate this technique for a vector<double>
argument.
Example
// Function: FilterVec // Purpose: Extracts only the numeric values from a vector //{{XLP_SRC(FilterVec) // NOTE - the FunctionWizard will add and remove mapping code here. // DO NOT EDIT what you see in these blocks of generated code! IMPLEMENT_XLLFN2(FilterVec, "RP", "FilterVec", "A", "User Defined" "", "Extracts only the numeric values from a vector", "Vector containing values of various types\000", "\0appscope=" "1\0", 1) extern "C" __declspec( dllexport ) LPXLOPER FilterVec(const COper* A) { XLL_FIX_STATE; CXlOper xloResult; //}}XLP_SRC std::vector<double> vecA; if (A->IsVector()) { for (USHORT i = 0; i < A->GetVectorCount(); i++) if (A->VectorCell(i).IsDouble()) vecA.push_back(A->VectorCell(i).ToDouble()); xloResult = vecA; } return xloResult.Ret(); }