You can use the XLL+ Function Wizard to specify that an argument should contain a vector of a particular type, e.g. a row of strings or a column of dates. This walkthrough demonstrates how to add and use a vector argument in an add-in function.
You can find a copy of all the code used in this walkthrough
in the Walkthroughs/VectorFns
folder in your
XLL+ installation, along with a demonstration spreadsheet,
VectorFns.xls
.
For more information about creating projects, see Creating an add-in project in the XLL+ User Guide.
Use the XLL+ Function Wizard to create a new add-in function and to add a vector argument.
Note: If you do not know how to start the Function Wizard, or you cannot find the command to open it, look at Invoke the Function Wizard in the User Guide.
Open the source file VectorFns.cpp and click on the New XLL+ Function menu item or tool-button, to show the New Function dialog, and fill in the name as shown below.
Name | VectorSum |
---|---|
Return type | CXlOper |
Category | Math & Trig |
Description | Returns the sum of a vector |
Fill in the function's category and description.
Add a new argument named Input of type Double, by typing into the arguments grid, as shown below.
Click on the Vector tool, to convert the argument to a vector.
The argument is now a vector of type double, as shown below.
Type | Name | Description |
---|---|---|
Double[] | Input | A vector of numbers |
Click on the OK button to close the Function Wizard and save the function.
The following code has been added to VectorFns.cpp.
CXlOper* VectorSum_Impl(CXlOper& xloResult, const CXlOper* Input_op) { // Input buffers std::vector<double> Input; // Validate and translate inputs XlReadVector(*Input_op, Input, L"Input", XLA_TRUNC_ONEMPTY|XLA_TRUNC_ONBLANK); // End of generated code //}}XLP_SRC // TODO - set the value of xloResult, or return another value // using CXlOper::RetXXX() or throw a CXlRuntimeException. return xloResult.Ret(); }
Let us examine the interesting code.
Note that the argument has been declared as type const CXlOper*. The CXlOper type is usually used to pass vector and matrix values from Excel to add-in functions.
CXlOper* VectorSum_Impl(CXlOper& xloResult, const CXlOper* Input_op)
A buffer variable Input is declared. The contents of the COper argument passed by Excel will be extracted and put into Input.
std::vector<double> Input;
Code has been generated to read the contents of Input into vecInput.
XlReadVector(*Input_op, Input, L"Input", XLA_TRUNC_ONEMPTY|XLA_TRUNC_ONBLANK);
Note that if the cell range passed to the function cannot be read for any reason (for example, because a cell contains a string instead of a number) then an exception will be thrown, and no further processing will take place within the function. Instead, control will pass to an outer wrapper function, which will catch the exception, and return it to Excel as an error result.
Add code to implement the function, as shown below:
CXlOper* VectorSum_Impl(CXlOper& xloResult, const CXlOper* Input_op) { // Input buffers std::vector<double> Input; // Validate and translate inputs XlReadVector(*Input_op, Input, L"Input", XLA_TRUNC_ONEMPTY|XLA_TRUNC_ONBLANK); // End of generated code //}}XLP_SRC double dSum = 0.0; for (size_t i = 0; i < Input.size(); i++) dSum += Input[i]; xloResult = dSum; return xloResult.Ret(); }
=VectorSum(A1:A6)
to return the sum
of the numbers in cells A1 to A6.XlReadVector method | User guide: Creating a vector argument | Samples and Walkthroughs