in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Excel add in development in C++ Aplications in finance - Dalton S.

Dalton S. Excel add in development in C++ Aplications in finance - Wiley publishing , 2005. - 425 p.
ISBN 0-470-02469-0
Download (direct link): exceladdindevelopmentincand2005.pdf
Previous << 1 .. 57 58 59 60 61 62 < 63 > 64 65 66 67 68 69 .. 168 >> Next

cpp_xloper Oper1(x); // creates an xltypeInt xloper, value = x cpp_xloper Oper2 = y; // creates an xltypeInt xloper, value = y cpp_xloper Oper3; // creates an xloper of undefined type
// Change the type of Oper3 to xltypeInt, value = z, using the // overloaded operator =
Oper3 = z;
// Create xltypeInt=z using copy constructor cpp_xloper Oper4 = Oper3;
The code for the xltypeInt constructor is:
cpp_xloper::cpp_xloper(int w) {
set_to_int(&m_Op, w);
The code for the overloaded conversion operator ‘=’ is:
void cpp_xloper::operator=(int w) {
set_to_int(&m_Op, w);
How you convert it into a C/C++ data type
The following code example shows how to access (or convert, if not an xltypeInt) the xloper:
bool coerce_to_int(xloper *p_op, int &w) {
return false;
Excel Add-in Development in C/C++
if(p_op->xltype == xltypeInt)
w = p_op->val.w; return true;
if(p_op->xltype == xltypeErr)
w = p_op->val.err; return true;
// xloper is not an integer type, so try to convert it. xloper ret_val;
if(!coerce_xloper(p_op, ret_val, xltypeInt)) return false;
w = ret_val.val.w; return true;
Using the cpp_xloper class the conversion would look like this:
The code for the overloaded conversion operator (int) is:
cpp_xloper::operator int(void)
int i;
if(coerce_to_int(&m_Op, i)) return i;
return 0;
What the memory considerations are
None (unless the 10 bytes for the xloper itself are dynamically allocated), as the integer w is contained entirely within the xloper.
How you can avoid usins it
Declare functions as taking int arguments and/or returning ints: Excel will do the necessary conversions.
Passing Data between Excel and the DLL
6.8.7 Array (mixed type): xltypeMulti
This xloper type is used to refer to arrays whose elements may be any one of a number of mixed xloper types. The elements of such an array are stored (and read) row-by-row in a continuous block of memory.4
There are important distinctions between such an array and an xloper that refers to a range of cells on a worksheet:
• The array is not associated with a block of cells on a worksheet.
• The memory for the array elements is pointed to in the xltypeMulti. (In range xlopers this is not the case. The data contained in the range of cells can only be accessed indirectly, for example, using xlCoerce.)
• Some Excel functions accept either range references or arrays as arguments, whereas others will only accept ranges.
An xltypeMulti is far more straightforward to work with than the range xloper types. Accessing blocks of data passed to the DLL in an xltypeMulti is quite easy. Their use is necessary if you want to pass arrays to C API functions where the data is not in any spreadsheet.
When you will encounter it
If a DLL function is registered with Excel as taking an xloper, an xltypeMulti is only passed to the DLL when the supplied argument is a literal array within the formula, for example, =SUM({1,2,3}). If the function is registered as taking an oper, an xltypeMulti is passed whenever the function is called with a range or a literal array. In this case, Excel handles the conversion from range xloper to array oper before calling the DLL.
Many of the C API functions return xltypeMulti xlopers, especially those returning variable length lists, such as a list of sheets in a workbook. (See section 8.9.10 Information about a workbook: xlfGetWorkbook on page 225 for details of this particular example.)
When you need to create it
A number of Excel’s own functions take both array and range arguments. When calling them from within the DLL, an xltypeMulti should be used unless the data are on a worksheet. In that case, it is better to use a range xloper. (Note that not all C API functions that take ranges will accept arrays: those returning information about a supposedly real collection of cells on a real worksheet will not.)
This xloper type provides the best way to return arrays of data that can be of mixed type back to a worksheet. (Note that to return a block of data to a worksheet function, the cell formula must be entered into the worksheet as an array formula.) It can also provide a stepping stone to reading the contents of a worksheet range, being much easier to work with than the xlopers that describe ranges xltypeSRef and xltypeRef. One of the cpp_xloper constructors below shows the conversion of these types to xltypeMulti using the xlCoerce function.
4 Variant arrays passed from VB to a C/C++ DLL store their elements column-by-column. See section 3.7 Excel ranges, VB arrays, SafeArrays, array Variants on page 64 for details.
Excel Add-in Development in C/C++
Warning: A range that covers an entire column on a worksheet (e.g., A:A in a cell formula, equivalent to A1:A65536) can, in theory, be passed into a DLL in an xloper of type xltypeSRef or xltypeRef. However, there is a bug. The xloper will be given the rwLast value of 0x3fff instead of 0xffff. Even if this were not the case, coercing a reference that represented an entire column to an xltypeMulti would fail. The rows field in the xltypeMulti, being a WORD that counts from 1, would roll back over to zero. In other words, the xltypeMulti is limited to arrays from ranges with rows from 1 to 65,535 inclusive OR 2 to 65,536 inclusive. You should bear this limitation in mind when coding and documenting your DLL functions.
Previous << 1 .. 57 58 59 60 61 62 < 63 > 64 65 66 67 68 69 .. 168 >> Next