XLL+ Class Library (6.3)

MyMonthsInYear Example

This example shows how to return an array of mixed types from an add-in function.

CopyC++
// Function:    MyMonthsInYear 
// Returns:     LPXLOPER 
// Description: Return array describing each month in a given year 
 
//{{XLP_SRC(MyMonthsInYear) 
    // NOTE - the FunctionWizard will add and remove mapping code here. 
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN3(MyMonthsInYear, MyMonthsInYear_4, MyMonthsInYear_12, "RH", 
    "UH", L"MyMonthsInYear", 0, L"Year", 0, L"14", 0, L"Return array describin"
    L"g each month in a given year", 0, L"Year as a number (eg 2005)\0", 0, 0, 
    L"{MyMonthsInYear,,,Return array describing each month in a given year,14,"
    L"1,0,U,{{0,{Year,Unsigned Short Int,0,,Year as a number (eg 2005),,,,}}},"
    L"{},3,,0,0}", 1)
CXlOper* MyMonthsInYear_Impl(CXlOper&, unsigned short int);

extern "C" __declspec(dllexport)
LPXLOPER12 MyMonthsInYear_12(unsigned short int Year)
{
    XLL_FIX_STATE;
    CXlOper xloResult;
    try {
        CXlStructuredExceptionHandler _seh_;
        xloResult.HandleResult(MyMonthsInYear_Impl(xloResult, Year));
    }
    catch(const CXlRuntimeException& ex) {
        CXllApp::Instance()->DisplayException(xloResult, ex);
    }
    return xloResult.Ret12();
}
extern "C" __declspec(dllexport)
LPXLOPER4 MyMonthsInYear_4(unsigned short int Year)
{
    XLL_FIX_STATE;
    CXlOper xloResult;
    try {
        CXlStructuredExceptionHandler _seh_;
        xloResult.HandleResult(MyMonthsInYear_Impl(xloResult, Year));
    }
    catch(const CXlRuntimeException& ex) {
        CXllApp::Instance()->DisplayException(xloResult, ex);
    }
    return xloResult.Ret4();
}

CXlOper* MyMonthsInYear_Impl(CXlOper& xloResult, unsigned short int Year)
{
    // End of generated code 
//}}XLP_SRC 
 
    if ( Year < 1901 || Year > 2099 )
        xloResult = xlerrValue;
    else
    {
        static LPCSTR apszMonths[12] = {
            "Jan", "Feb", "Mar", "Apr",
            "May", "Jun", "Jul", "Aug",
            "Sep", "Oct", "Nov", "Dec"
        };

        // Allocate the result array
        xloResult.AllocArray(12, 3);

        // Fill each row 
        for ( unsigned short i = 0; i < 12; i++ )
        {
            // Put month number in first column
            xloResult.Cell(i, 0) = (double)(i + 1);
            // Put short month name in second column
            xloResult.Cell(i, 1) = apszMonths[i];
            // Put length of month in third column 
            int cDays;
            switch ( i + 1 ) {
            case 2:
                cDays = (Year % 4 != 0) ? 28 : 29;
                break;
            case 4: case 6: case 9: case 11:
                cDays = 30;
                break;
            default:
                cDays = 31;
                break;
            }
            xloResult.Cell(i, 2) = (double)cDays;
        }
    }

    return xloResult.Ret();
}

Uses

CXlOper::AllocArray | CXlOper::Cell