Ejemplo n.º 1
0
xlw::XlfOper4& xlw::XlfOper4::Set(const CellMatrix& cells)
{
    size_t r= cells.RowsInStructure();
    size_t c= cells.ColumnsInStructure();

    c= c<  255 ? c :255;


    lpxloper_->xltype = xltypeMulti;
    lpxloper_->val.array.rows = static_cast<WORD>(r);
    lpxloper_->val.array.columns = static_cast<WORD>(c);

    lpxloper_->val.array.lparray
            = (LPXLOPER)XlfExcel::Instance().GetMemory(r*c*sizeof(XLOPER));

    for (size_t i=0; i < r; i++)
        for (size_t j=0; j < c; j++)
        {
            size_t k = i*c +j;
            if (cells(i,j).IsANumber())
                lpxloper_->val.array.lparray[k] = *(LPXLOPER)XlfOper4(cells(i,j).NumericValue());
            else
                if (cells(i,j).IsAString())
                    lpxloper_->val.array.lparray[k] = *(LPXLOPER)XlfOper4(cells(i,j).StringValue().c_str());
                else
                    if (cells(i,j).IsBoolean())
                            lpxloper_->val.array.lparray[k] = *(LPXLOPER)XlfOper4(cells(i,j).BooleanValue());
                    else
                        if (cells(i,j).IsError())
                             lpxloper_->val.array.lparray[k] = *(LPXLOPER)XlfOper4(static_cast<short>(cells(i,j).ErrorValue()),true);
                        else
                                lpxloper_->val.array.lparray[k] = *(LPXLOPER)XlfOper4("");
        }

    return *this;


}
Ejemplo n.º 2
0
// function Excel4 calls always as using XLOPER12 seem problematic
int xlw::XlfFuncDesc::RegisterAs(const std::string& dllName, const std::string& suggestedHelpId, double mode_) const
{
    // alias arguments
    XlfArgDescList& arguments = impl_->arguments_;

    int nbargs = static_cast<int>(arguments.size());
    std::string args(returnTypeCode_);
    std::string argnames;

    // the synchronous part of an asynchronous function returns void
    if (XlfExcel::Instance().excel14() && impl_->Asynchronous_)
    {
        args = ">";
    }

    XlfArgDescList::const_iterator it = arguments.begin();
    while (it != arguments.end())
    {
        argnames += (*it).GetName();
        args += (*it).GetType();
        ++it;
        if (it != arguments.end())
            argnames+=", ";
    }
    
    // When the arguments add up to more then 255 char is problematic. the functions
    // will not register see  see BUG ID: 2834715 on sourceforge - nc
    if(argnames.length() > 255)
    {
        argnames = "Too many arguments for Function Wizard";
    }

    // the synchronous part of an asynchronous function have an extra 
    // bigdata xloper on the end containing the handle
    if (XlfExcel::Instance().excel14() && impl_->Asynchronous_)
    {
        args += "X";
    }
    if (impl_->recalcPolicy_ == xlw::XlfFuncDesc::Volatile)
    {
        args+="!";
    }
    if (XlfExcel::Instance().excel12() && impl_->Threadsafe_)
    {
        args+="$";
    }
    if (XlfExcel::Instance().excel14() && impl_->ClusterSafe_)
    {
        args+="&";
    }
    if (impl_->MacroSheetEquivalent_)
    {
        args+="#";
    }

    args+='\0'; // null termination for C string

    std::vector<LPXLOPER> argArray(10 + nbargs);
    LPXLOPER *px = &argArray[0];
    std::string functionName(GetName());

    // We need to have 2 functions exposed one for less than
    // version 14 and that it the normal function, we also need
    // the Synchronous part that returns void and takes an extra int
    // By convension this is the same as the normal function but with 
    // Sync on the end
    if (XlfExcel::Instance().excel14() && impl_->Asynchronous_)
    {
        functionName += "Sync";
    }

    (*px++) = XlfOper4(dllName);
    (*px++) = XlfOper4(functionName);
    (*px++) = XlfOper4(args);
    (*px++) = XlfOper4(GetAlias());
    (*px++) = XlfOper4(argnames);
    (*px++) = XlfOper4(mode_);
    (*px++) = XlfOper4(impl_->category_);
    (*px++) = XlfOper4(""); // shortcut
    // use best help context
    if(!helpID_.empty() && helpID_ != "auto")
    {
        (*px++) = XlfOper4(helpID_);
    }
    else
    {
        (*px++) = XlfOper4(suggestedHelpId); 
    }
    (*px++) = XlfOper4(GetComment());
    int counter(0);
    for (it = arguments.begin(); it != arguments.end(); ++it)
    {
        ++counter;
        if(counter < nbargs)
        {
            (*px++) = XlfOper4((*it).GetComment());
        }
        else
        {
            // add dot space to last comment to work around known excel bug
            // see http://msdn.microsoft.com/en-us/library/bb687841.aspx
            (*px++) = XlfOper4((*it).GetComment() + ". ");
        }
    }

    if(XlfExcel::Instance().excel12())
    {
        // total number of arguments limited to 255
        // so we can't send more than 245 argument comments
        nbargs = std::min(nbargs, 245);
    }
    else
    {
        // you can't send more than 30 arguments to the register function
        // in up to version 2003, so just only send help for up to the first 20 parameters
        nbargs = std::min(nbargs, 20);
    }

    XlfOper4 res;
    int err = XlfExcel::Instance().Call4v(xlfRegister, res, 10 + nbargs, &argArray[0]);
    if(err == xlretSuccess && res.IsNumber())
    {
        funcId_ = res.AsDouble();
    }
    else
    {
        funcId_ = InvalidFunctionId;
    }

    return err;
}
Ejemplo n.º 3
0
xlw::XlfOper4 xlw::XlfOper4::operator()(WORD row, WORD col) {
    if (!(lpxloper_->xltype & xltypeMulti))
        throw XlfException("attempt to perform an xltypeMulti operation on a value of type " + lpxloper_->xltype);
    return XlfOper4(&lpxloper_->val.array.lparray[row * lpxloper_->val.array.columns + col]);
}
Ejemplo n.º 4
0
/*!
Registers the command as a macro in excel.
\sa XlfExcel, XlfFuncDesc.
*/
int xlw::XlfCmdDesc::DoRegister(const std::string& dllName, const std::string& suggestedHelpId) const
{
    XlfArgDescList arguments = GetArguments();

    // 2 = normal macro, 0 = hidden command
    double type = hidden_ ? 0 : 2;

    int nbargs = static_cast<int>(arguments.size());
    std::string args("A");
    std::string argnames;

    XlfArgDescList::const_iterator it = arguments.begin();
    while (it != arguments.end())
    {
        argnames += (*it).GetName();
        args += (*it).GetType();
        ++it;
        if (it != arguments.end())
            argnames+=", ";
    }

    // When the arguments add up to more then 255 char is problematic. the functions
    // will not register see  see BUG ID: 2834715 on sourceforge - nc
    if(argnames.length() > 255)
    {
        argnames = "Too many arguments for Function Wizard";
    }

    std::vector<LPXLOPER> argArray(10 + nbargs);
    LPXLOPER *px = &argArray[0];

    (*px++) = XlfOper4(dllName);
    (*px++) = XlfOper4(GetName());
    (*px++) = XlfOper4(args);
    (*px++) = XlfOper4(GetAlias());
    (*px++) = XlfOper4(argnames);
    (*px++) = XlfOper4(type);
    (*px++) = XlfOper4("");
    (*px++) = XlfOper4("");
    (*px++) = XlfOper4("");
    (*px++) = XlfOper4(GetComment());
    int counter(0);
    for (it = arguments.begin(); it != arguments.end(); ++it)
    {
        ++counter;
        if(counter < nbargs)
        {
            (*px++) = XlfOper4((*it).GetComment());
        }
        else
        {
            // add dot space to last comment to work around known excel bug
            // see http://msdn.microsoft.com/en-us/library/bb687841.aspx
            (*px++) = XlfOper4((*it).GetComment() + ". ");
        }
    }

    if(XlfExcel::Instance().excel12())
    {
        // total number of arguments limited to 255
        // so we can't send more than 245 argument comments
        nbargs = std::min(nbargs, 245);
    }
    else
    {
        // you can't send more than 30 arguments to the register function
        // in up to version 2003, so just only send help for up to the first 20 parameters
        nbargs = std::min(nbargs, 20);
    }

    XlfOper4 res;
    int err = XlfExcel::Instance().Call4v(xlfRegister, res, 10 + nbargs, &argArray[0]);
    if(err == xlretSuccess && res.IsNumber())
    {
        funcId_ = res.AsDouble();
    }
    else
    {
        funcId_ = InvalidFunctionId;
    }
    return err;
}