This walkthrough demonstrates how to use the CXlOper class to return a 1-dimensional array (vector) from an add-in function.
Two such functions are created, which construct the array result in two different ways:
To create the project using Visual Studio 6
To create the project using Visual Studio .NET or Visual Studio 2005
For more details 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 an argument of type unsigned short integer.
Note: If you do not know how to start the Function Wizard, or you cannot find the tool-bar, look at Installing the Function Wizard under Developer Studio 6 or Installing the Function Wizard under Visual Studio .NET or Visual Studio 2005.
Open the source file VectorRes.cpp and click on the New XLL+ Function menu item or tool-button, to show the New Function dialog, and fill in the name, category and description as shown below.
Add a new argument Count of type UShort (unsigned short integer), by typing into the arguments grid, as shown below. Note that the return type must be of type CXlOper.
Click on the OK button to close the Function Wizard and save the function.
The following function has been added to MatrixRes.cpp.
extern "C" __declspec( dllexport ) LPXLOPER PointerToArray(USHORT Count) { CXlOper xloResult; //}}XLP_SRC // TODO - Set the value of xloResult return xloResult.Ret(); }
Our task is going to be to populate xloResult with a numeric vector.
Add the code below to set up a temporary array of numbers. In the real world this would be a more useful data set, probably returned by a previously written function.
extern "C" __declspec( dllexport ) LPXLOPER PointerToArray(USHORT Count) { CXlOper xloResult; //}}XLP_SRC // Create and populate a 1-dimensional array of numbers double* ad = new double[Count]; for (USHORT i = 0; i < Count; i++) ad[i] = (double)(i+1); // Set the value of xloResult xloResult.FromDoubleArray(ad, Count); // Clean up temporary array delete[] ad; // Return contents of xloResult return xloResult.Ret(); }
The important function here is of course FromDoubleArray(). This populates xloResult with a copy of the numeric array ad. We can then safely delete ad, and return the contents of xloResult.
Use the XLL+ Function Wizard to create a new add-in function and to add an argument of type unsigned short integer.
Open the source file VectorRes.cpp and click on the New XLL+ Function menu item or tool-button, to show the New Function dialog, and fill in the name, category and description as shown below.
Add a new argument Count of type UShort (unsigned short integer), by typing into the arguments grid, as shown below. Note that the return type must be of type CXlOper.
Click on the OK button to close the Function Wizard and save the function.
The following function has been added to MatrixRes.cpp.
extern "C" __declspec( dllexport ) LPXLOPER VectorToArray(USHORT Count) { CXlOper xloResult; //}}XLP_SRC // TODO - Set the value of xloResult return xloResult.Ret(); }
Our task is going to be to populate xloResult with a numeric vector.
Add the code below to set up a temporary array of numbers. In the real world this would be a more useful data set, probably returned by a previously written function.
extern "C" __declspec( dllexport ) LPXLOPER VectorToArray(USHORT Count) { CXlOper xloResult; //}}XLP_SRC // Create and populate a vector std::vector<double> vec(Count); for (USHORT i = 0; i < Count; i++) vec[i] = (double)(i+1); // Set the value of xloResult xloResult = vec; // Return contents of xloResult return xloResult.Ret(); }
The important method here is the overloaded assignment operator of the CXlOper class. This populates xloResult with a copy of vec. We then return the contents of xloResult.
In a worksheet, select a range containing five rows and one column, and type in the formula =PointerToArray(5), as shown below.
Then press the key combination Shift+Ctrl+Enter to enter the formula as an array formula, as shown below.
Walkthroughs | CXlOper::FromDoubleArray method | CXlOper::operator =