Reference: Q0024
Article Last Modified on 28-Mar-2006
How do I extract only the numeric values from an input vector?
Use the COper::Cell(...) method to inspect each cell, and add the
contents to your input variable only if the cell is numeric.
COper.COper::IsVector() to ensure that your input is a vector
(or a single cell, which is the trivial case of a vector).COper::GetVectorCount() to get the number of
cells in the unfiltered vector.COper::Cell(...) to inspect a cell directly.COper::IsDouble() to test the contents of each cell.The example functions below demonstrate this technique for a vector<double>
argument.
// 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();
}