This topic shows you how to return a 1-dimensional array to Excel. The example is a very simple function for sorting numeric arrays in ascending order.
Use the Function Wizard to create a new function with these properties:
Name: SORTVECTOR Return type: CXlOper Category: Lookup & Reference Description: Sort a vector of numbers in ascending order
Add a single vector argument with the following properties:
Type: Double[] Name: Vector Description: Unsorted vector of numbers
Press OK to return to Visual Studio.
Add code so that your add-in function looks like this:
CXlOper* SORTVECTOR_Impl(CXlOper& xloResult, const CXlOper* Vector_op) { // Input buffers std::vector<double> Vector; // Validate and translate inputs XlReadVector(*Vector_op, Vector, L"Vector", XLA_TRUNC_ONEMPTY| XLA_TRUNC_ONBLANK); // End of generated code //}}XLP_SRC // Use STL algorithm to sort input std::sort(Vector.begin(), Vector.end()); // Copy the sorted vector to xloResult xloResult = Vector; return xloResult.Ret(); }
Yet another overload of the CXlOper assignment operator is used to copy the STL vector into xloResult:
xloResult = Vector;The operator creates an array of values within xloResult and populates it with the values in Vector.
This is one of the most pleasingly simple add-in functions in this tutorial. It is not necessarily very useful - a better implementation would sort a 2-dimensional array by a given column or columns, in ascending and descending order - but it is very short, and does a surprising amount of work with only two lines of hand-written code.