Howto write Excel add-in functions that have a variable number of arguments (ex: MAX, MIN...)

StackOverflow https://stackoverflow.com/questions/15688533

  •  30-03-2022
  •  | 
  •  

Pregunta

I am writing an Excel Add-In in C++ using using Microsoft's dev frameworks (Excel 2010).

I would like to know if it is possible to write in c++ an excel function that have a variable number of arguments just like MAX, MIN, AVERAGE...

Thanks in advance.

¿Fue útil?

Solución

Assuming you are coding to the XLL interface then AFAIK the only way of handling a variable number of arguments is to declare each of the arguments in turn up to the max number you want to handle (subject to Excel version limits) and then check which ones are missing.

Otros consejos

Excel Add-ins are built as COM objects, right?

Have you tried using the [vararg] attribute on your COM interface definition?

Here is an example that handles up to 16 arguments using http://xll.codeplex.com.

// 16 LPOPERs
#define XLL_LPOPERSX XLL_LPOPERX XLL_LPOPERX XLL_LPOPERX XLL_LPOPERX XLL_LPOPERX XLL_LPOPERX     XLL_LPOPERX XLL_LPOPERX \
                 XLL_LPOPERX XLL_LPOPERX XLL_LPOPERX XLL_LPOPERX XLL_LPOPERX XLL_LPOPERX XLL_LPOPERX XLL_LPOPERX
static AddInX xai_functional_args(
    FunctionX(XLL_LPOPERSX, _T("?xll_functional_args"), _T("FUNCTIONAL.ARGS"))
    .Arg(XLL_LPOPERSX, _T("Arg1, ..."), _T("are a numeric arguments or NA() to indicate a missing argument."))
    .Category(CATEGORY)
    .FunctionHelp(_T("Return an array for the second argument to FUNCTIONAL.BIND."))
    .Documentation(
        _T("Non numeric arguments will not be bound.")
    )
);
LPOPER WINAPI
xll_functional_args(LPOPERX px)
{
#pragma XLLEXPORT
    static OPER o;

    o.resize(0,0);

    for (LPOPERX* ppx = &px; (*ppx)->xltype != xltypeMissing && ppx - &px < 16; ++ppx) {
        o.push_back(**ppx);
    }

    o.resize(1, o.size());

    return &o;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top