XLL+ Class Library (7.0)

Converter class

The converter class can be found in the header file extensions\DateConverter.h. If you examine your source code file after you have inserted a Date type, you will find that the following line has been inserted near the top of the code:

CopyC++
#include "extensions\DateConverter.h"

The file is located in the extensions sub-directory of the XLL+ include directory.

Let us first look at the whole class:

CopyC++
class DateConverter : public CXlUserConverterBase<long, double>
{
private:
    typedef long ElemType;
    typedef double XlNativeType;
public:
    virtual bool ConvertFromExcel(const XlNativeType& xlValue, 
        ElemType& outerValue, const ParamsType& params) const
    {
        if (!CheckDateValue(xlValue))
            return false;
        outerValue = (long)xlValue;
        return true;
    }
    virtual bool ConvertToExcel(const ElemType& outerValue, 
        XlNativeType &xlValue, const ParamsType& params) const
    {
        if (!CheckDateValue((double)outerValue))
            return false;
        xlValue = (double)outerValue;
        return true;
    }
protected:
    virtual std::string GetTypeName(const ParamsType& params) const 
    { 
        return "date"; 
    }
private:
    static bool CheckDateValue(double value)
    {
        return value >= 30000.0 && value <= (double)LONG_MAX;
    }
};

The salient points of the class are:

  1. It is derived from a template class:

    CopyC++
    CXlUserConverterBase<long, double>

    All converter classes are derived from CXlUserConverterBase<ElemType, XlNativeType>, where ElemType is the type of the target C++ variable and XlNativeType is one of Excel's native data types.

  2. The virtual function ConvertFromExcel() is implemented. This method is called by the extended type validation function, XlReadScalarEx. ConvertFromExcel returns true if it succeeds, and false if it fails. If it succeeds, the XlNativeType input is converted to the ElemType output. This virtual function must always be implemented.

Two other methods are implemented:

  1. ConvertToExcel() does the reverse conversion, converting a date to an Excel number. This method is not required, but it is useful if, for instance, you want to return a vector or matrix of dates to Excel. You can use the global methods XlWriteScalarEx, XlWriteVectorEx and XlWriteMatrixEx to convert an extended type (or a vector or a matrix of extended types) into a CXlOper which can be returned to Excel.

  2. GetTypeName() returns the name of the type, "date", which will be used in error messages.

Next: Creating an extended type >>