XLL+ Class Library (6.3)

Handling Excel coordinates

When you return to Visual Studio, code will have been added to Tutorial.cpp.

CopyC++
CXlOper* MAXCOORD_Impl(CXlOper& xloResult, const CXlOper* Array_op, BOOL XCoord)
{
    // Input buffers 
    ple::mtx_flat<double> Array; 
    // Validate and translate inputs
    XlReadMatrix(*Array_op, mtx_adapter(Array), L"Array", XLA_TRUNC_ONEMPTY|
        XLA_TRUNC_ONBLANK|XLA_FLAG_TRANSPOSE);
    // 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();
}

The matrix-reading code differs from that in INTERP2D in two ways:

  1. The matrix used is of type mtx_flat<T>.
  2. The flags argument to XlReadMatrix includes the bit XLA_FLAG_TRANSPOSE.

After XlReadMatrix() has run successfully, the data we need will be in Array, as a continous array of doubles, arranged with columns together, which is what we wanted for our implementation function, FindMax().

Add the code below, to call FindMax():

CopyC++
CXlOper* MAXCOORD_Impl(CXlOper& xloResult, const CXlOper* Array_op, BOOL XCoord)
{
    ...
    // End of generated code 
//}}XLP_SRC 
 
    // Note that the "Reject Empty Matrix" flag has been set for Array. 
    // We can therefore safely use Array[0] or Array.begin() in the     
    // code below.                                                      
 
    int nMaxCol, nMaxRow;                                              
    // Since we have transposed the matrix, by specifying that columns  
    // be held together, Array has columns as the outer axis (i.e.      
    // size(0)) and rows as the inner axis (i.e. size(1)).              
    if (FindMax(Array.size(0), Array.size(1),                          
                &Array[0], // or, if you prefer, Array.begin(),        
                &nMaxCol, &nMaxRow))                                   
    {                                                                  
        int nCoord = XCoord ? nMaxCol : nMaxRow;                       
        // Add 1 to the coordinate because Excel's coordinates are      
        // 1-based while C's are zero-based                            
        nCoord++;                                                      
        // Return a numeric value                                      
        xloResult = (double)nCoord;                                    
    }                                                                  
    else                                                               
        xloResult = xlerrNum;                                          
 
    return xloResult.Ret();
}

Notice that we add one to any coordinate's representation in Excel because Excel indices are by convention one-based, whereas C indices (being address offsets) are zero-based.

For full details on reading matrix arguments, see XlReadMatrix() in the class reference.

Next: Returning vectors to Excel >>