XLL+ Class Library

MyMonthsInYear Example

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

// 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_XLLFN2(MyMonthsInYear, "RH", "MyMonthsInYear", "Year",
    "User Defined", "Return array describing each month in a"
    " given year", "Year as a number (eg 2005)\000", "\0", 1)

extern "C" __declspec( dllexport )
LPXLOPER MyMonthsInYear(USHORT usYear)
{
    CXlOper xloResult;
//}}XLP_SRC

    if ( usYear < 1901 || usYear > 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 = (usYear % 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