Introduction

Let's assume you want to implement a new spreadsheet function and the function
was defined by the
OASIS OpenDocument Format Formula subcommittee,
see latest revision of the
specification (draft).
Let's further assume the function's name is MYFUNC and will take
2 parameters, of which the second parameter is optional and defaulted to 0, and
returns a number, as following:

Make the formula compiler know the function

formula/inc/formula/compiler.hrc

sc/inc/compiler.hrc up to and including version DEV300_m38

These are the defines used by the resources for function names and the Function
Wizard, and the numerical values of OpCode for the formula compiler and
interpreter. Note that once defined the namesmust not be changed
because they are used by the localization tools as identifiers.

Add a new define, in this case for 2 parameters append it to the section for
functions with more than 1 parameter near the end of the file. Name the define
SC_OPCODE_MYFUNC and insert it right before the define of
SC_OPCODE_STOP_2_PAR, give it the value SC_OPCODE_STOP_2_PAR had, and
increment the values of SC_OPCODE_STOP_2_PAR and SC_OPCODE_LAST_OPCODE_ID. If
before the section looked like

formula/inc/formula/opcode.hxx

sc/inc/opcode.hxx up to and including version DEV300_m38

Here the OpCodeEnum values are defined. Note that in a non-product build
(--enable-dbgutil during configure) there is a
typedef OpCodeEnum OpCode;
to show enum names in the debugger, while in a product build it is
typedef OpCodeEnum USHORT;
to save some memory, since compilers tend to produce an int for an enum.

Find a "right" place for the new enum. Although the way the definitions are
setup the placement doesn't matter, there are sections with different topics,
such as String functions and Statistical functions. Maybe the correct
place for MYFUNC would be under miscellaneous. Best practice is to add
a new OpCode to the end of such section. Name the OpCode ocMyFunc and add
the line

ocMyFunc = SC_OPCODE_MYFUNC,

formula/source/core/resource/core_resource.src

sc/source/core/src/compiler.src up to and including version DEV300_m38

These are the resources for function names. There are 3 resource bundles:

RID_SC_FUNCTION_NAMES

English UI display names. These get localized for the UI of other languages.

RID_SC_FUNCTION_NAMES_ENGLISH

These English names are used internally to store/load ODF v1.0/v1.1 and for APIXFunctionAccess. Usually the name is identical to that in RID_SC_FUNCTION_NAMES. Once defined and "in the wild", the name must not be changed.

RID_SC_FUNCTION_NAMES_ENGLISH_ODFF

These English names are used internally to store/load ODFF aka OpenFormula as of ODF v1.2. Once defined, the name must not be changed.

The new function name must be defined for all 3 resource bundles.

To the end of Resource RID_SC_FUNCTION_NAMES
add

String SC_OPCODE_MYFUNC
{
Text [ en-US ]="MYFUNC";};

The [ en-US ] field tells the localization tools that the name may
be localized.

To the end of Resource RID_SC_FUNCTION_NAMES_ENGLISH
add

String SC_OPCODE_MYFUNC { Text ="MYFUNC";};

The absence of the [ en-US ] field tells the localization tools
that the name must not be localized.

To the end of Resource RID_SC_FUNCTION_NAMES_ENGLISH_ODFF
add

String SC_OPCODE_MYFUNC { Text ="MYFUNC";};

Again, the absence of the [ en-US ] field tells the localization
tools that the name must not be localized.

The compiler knows the function

After having added the necessary entries to formula/inc/formula/compiler.hrc, formula/inc/formula/opcode.hxx, and formula/source/core/resource/core_resource.src (sc/inc/compiler.hrc,
sc/inc/opcode.hxx, and sc/source/core/src/compiler.src respectively), the
formula compiler now knows the new function name and is able to compile an
expression where it is used, and it can be stored in and loaded from
a document. Of course nothing else works, the interpreter doesn't know how to
handle it and will generate an error if encountered. The function and its
parameters will not appear in the Function Wizard.

Publish the function to the Function Wizard

sc/inc/scfuncs.hrc

Till DEV300m90

Function groups (categories) and HelpIDs for functions are defined here. Lookup
the group where functions of ID_FUNCTION_GRP_... matching the new function's
category are defined and append an entry, incrementing the offset of the last
entry by one. For our function that could be

sc/inc/helpids.h

Since DEV300m91.

With Issue 111874 (not issue 111784 as in comment in CWS changehid) the helpid system has been changed from numerical to byte string. The help ids are no longer defined in file scfuncs.hrc but in file helpids.h

For our function that could be

#define HID_FUNC_MYFUNC "SC_HID_FUNC_MYFUNC"

Notice the prefix SC_. Use a position that is similar to the position of the new function in the other files like compiler.hrc or core_resource.src.

sc/source/ui/src/scfuncs.src

This large resource file contains all elements necessary to display functions
in the Function Wizard. It defines the function's short description, the number
of parameters, whether they are optional, and the description of each
parameter. For a detailed description of fields see the comment on top of the
file.

Add the new function to the end of one of the two resource blocks
RID_SC_FUNCTION_DESCRIPTIONS1 or RID_SC_FUNCTION_DESCRIPTIONS2. Which one
doesn't really matter, but functions should be more or less equally distributed
over the two blocks. There are two blocks because one resource block couldn't
have more than 64k data, just another legacy from Win16 times.. looked up again
right now (2008-08-23) in the meantime this restriction seems to have been
obsoleted, which would have to be verified by a test build using one resource
block only though.

Some comments on the ExtraData block, for more details see scfuncs.src:

ExtraData =
{
0; // The function is not suppressed and available in UI.
ID_FUNCTION_GRP_MATH; // The category in which the function is displayed.
U2S( HID_FUNC_MYFUNC ); // The HelpID of this function.
2; 0; 1; // 2 parameters, of which the 2nd is optional.
0; // None of the parameters are suppressed in the UI.
};

sc/util/hidother.src

Here the HelpIDs to be used within the Function Wizard are propagated to the
help system. Go to the end of the section containing HID_FUNC_... and append an
entry,

hidspecial HID_FUNC_MYFUNC { HelpID = HID_FUNC_MYFUNC;};

The Function Wizard knows the function

Now the Function Wizard can display the function and its parameters, and
online-help may be authored.

Let the interpreter handle the function

sc/source/core/inc/interpre.hxx

Add a member method to class ScInterpreter that will handle the new
function. Here this would be

void ScMyFunc();

Take care that the new member function doesn't resemble the name of some
already existing class, for example the method for the ADDRESS() function is
named ScAddressFunc() because ScAddress(), if called without
this-> prefix, would be the ctor of class ScAddress instead. If in
doubt, add ...Func() to the name.

sc/source/core/tool/interpr4.cxx

In method ScInterpreter::Interpret() add to switch ( eOp ) the
call to the member function for the OpCode:

case ocMyFunc : ScMyFunc();break;

sc/source/core/tool/interpr?.cxx

Pick one of the interpr?.cxx source files where the new method may fit. There
is no general advice which file exactly that might be, be sensible. As a guide line

Some number crunching that in the past had to be compiled without optimizations, though in the mean time code changed and this probably is not necessary anymore.

For our function, since that expects two, one optional, numerical scalar
arguments, this would be:

void ScInterpreter::ScMyFunc(){
BYTE nParamCount = GetByte();// The MustHaveParamCount...() functions check the number of parameters and// if they do not fit push an error on the stack, if the method fails// (returns false) we return immediately.if(!MustHaveParamCount( nParamCount, 1, 2))return;// Arguments are popped from the stack from right to left.double fParam2;if(nParamCount ==2)
fParam2 = GetDouble();else
fParam2 =0.0;double fVal = GetDouble();if(/* does fVal meet all constraints */){double fResult =/* calculate foo */;
PushDouble( fResult );}else
PushIllegalArgument();}

The not so easy case of non-scalar arguments

StackVar argument types

If the new function would handle parameters that are not scalar values, for
example a NumberSequence or matrix/array, they would have to be treated
explicitly, checking and reacting on the type of each argument. Lookout for
functions that use the GetType() call and handle StackVarsvDoubleRef or similar. Ask on the dev@sc mailing list if in
doubt.

The most common StackVar types obtainable with GetStackType()are:

svDouble

A double value.

svString

A literal string text.

svSingleRef

A single cell reference.

svDoubleRef

A cell range reference.

svRefList

A list of cell range references.

svMatrix

A matrix/array.

There are a few other types available with GetRawStackType() for
specific situations, these are converted to svDouble in
GetStackType() and returned as 0.0 by PopDouble() and
GetDouble():

svMissing

An empty parameter without value, if that should not be converted to 0.0 use GetDoubleWithDefault(double).

svEmptyCell

A previous expression returned an empty cell, it may depend on context whether that is to be interpreted as double or string.

The general template to act on each parameter in sequence from right to left is:

Of course your function may require specific treatment of parameters depending
on parameter types of other parameters, or bail out if a parameter type does
not match an expected type, this template is meant as guidance only.

sc/source/core/tool/parclass.cxx

Describes how parameters are to be treated if the function is used in an array
context expression, AKA array formula, where cell range references may be
treated differently from non-array context. If the function does not accept
other than scalar arguments, nothing has to be done here and the function does
not need an entry. Else the type of each parameter has to be set to one of:

Value

Function expects a single scalar value.

Reference

A cell range reference is passed as reference and not converted to array.

Array

In array context a cell range reference is converted to array.

ForceArray

A cell range reference is always converted to an array and the ForceArray context is propagated to all functions and operators in this parameter's expression.

ReferenceOrForceArray

A cell range reference is not converted to an array, but a ForceArray context is propagated to all functions and operators in this parameter's expression. Only LOOKUP() uses this, other functions should not need it.

For a more detailed description of these types please read the comments for
ScParameterClassification::Type in
sc/source/core/inc/parclass.hxx. Ask on the dev@sc mailing list if in
doubt.

Set the ScParameterClassification::CommonData.bRepeatLast member
variable to true if the function accepts multiple parameters that
are of the same type as the last one specified, e.g. for SUM() that accepts
multiple Reference parameters. If the function does not accept multiple
parameters of the same type, set the value to false.

Microsoft Excel® import and export

If the new function is also supported by Microsoft Excel®, for import/export it has to be added to the filter code as well. OpenOffice.org supports various versions of the file format used by Excel.

Currently, import and export of the old binary formats (file extension ".xls", internal names "BIFF2" for Excel 2.x, "BIFF3" for Excel 3.0, "BIFF4" for Excel 4.0, "BIFF5" for Excel 5.0 and Excel 95, and "BIFF8" for Excel 97-2003) are implemented in the sc code module in the directory sc/source/filter/excel. While the import filters support all BIFF versions, there are export filters for BIFF5 and BIFF8 only.

Import of the new Office Open XML formats (file extensions ".xlsx" and ".xlsm" used by Excel 2007 and later, internal name "OOXML") and the new binary format (file extension ".xlsb" used by Excel 2007 and later, internal name "BIFF12") are implemented in the oox code module in the directory oox/source/xls.

In the future, it is planned to move the old BIFF2-BIFF8 filters to the oox code module too, to be able to share more source code for all filters. The import of functions and formulas will take benefit from that too.

Old binary file formats (BIFF2-BIFF8)

Overview

Import and export of BIFF formulas is spread over a number of files in the sc code module.

Source code used by the import and export filters is located in the files sc/source/filter/inc/xlformula.hxx and sc/source/filter/excel/xlformula.cxx. The cxx file contains the most important detail for functions: the function tables, which will be described in detail below.

The source code to read formulas from BIFF is located in sc/source/filter/inc/excform.hxx, sc/source/filter/excel/excform.cxx (for BIFF2-BIFF5), and sc/source/filter/excel/excform8.cxx (additional code for BIFF8). Note that these source files have not been touched seriously for more than a decade, so the code quality might not be very high.

The source code to write formulas to BIFF is located in sc/source/filter/inc/xeformula.hxx and sc/source/filter/excel/xeformula.cxx.

It is important to know the BIFF version the function has been added to. Some functions are not supported by very old versions of Excel, and some functions even change the number of supported parameters between BIFF versions.

Basics - function tables

The file sc/source/filter/excel/xlformula.cxx contains a function table for each BIFF version, and an additional table for functions unknown to Excel. Every line in these tables is basically a structure of type XclFunctionInfo as defined in sc/source/filter/inc/xlformula.hxx and describes a single spreadsheet function. If a function description is added in a table for a specific BIFF version, it will be used for all newer BIFF versions too, there is no need to repeat the function description in the following tables (unless the function changes somehow in a newer BIFF version, this will be discussed in the next chapter).

We will continue to take a look at our example function MYFUNC. We will assume that this function has been introduced in BIFF3. Furthermore, we will assume that the function is built-in in Excel and has the BIFF function index 200. The BIFF function index is the counterpart of Calc's function op-code used to identify the function in the binary file format. Later on this page, handling of add-in functions (which do not have a specific function index) will be described too.

First, we have to add a new line in the function table for BIFF3, called saFuncTable_3. By convention, the tables are sorted by BIFF function index (second column in the tables), here we should use the appropriate position for our index 200.

Op-code

The first entry in the function description is Calc's op-code of the function, which is ocMyFunc here:

{ ocMyFunc,

BIFF function index

The second entry in the function description is the BIFF function index:

{ ocMyFunc, 200,

Minimum parameter count

The third entry in the function description is the minimum parameter count of the function. MYFUNC expects at least one parameter:

{ ocMyFunc, 200, 1,

Maximum parameter count

The fourth entry in the function description is the maximum parameter count of the function. MYFUNC expects at most two parameters:

{ ocMyFunc, 200, 1, 2,

Return type

The fifth entry in the function description is the type of the value the function returns. Usually, a function returns a single value (scalar), which might be a number or a string. But a few functions return an array of values (e.g. the function MTRANS that transposes a matrix), or a cell range address (e.g. the function INDIRECT that converts a string to a range address). Allowed values for this entry are V for scalar values, A for arrays, and R for range addresses (references). MYFUNC returns a scalar value, thus we have to add the V type:

{ ocMyFunc, 200, 1, 2, V,

Parameter types

The sixth entry in the function description is a C array describing the type of all function parameters. This is the most complicated part of the function description, as sometimes the parameter type is not obvious. Simple functions taking scalar values usually will have parameters of type VR which is short for "value/repeated". See the inline documentation of the structure XclFuncParamInfo in the file sc/source/filter/inc/xlformula.hxx for more details. Our function takes two scalar value parameter:

{ ocMyFunc, 200, 1, 2, V, { VR, VR },

Additional flags

The seventh entry in the function description contains additional flags that control the behaviour of the formula filters. Later on, these flags will be described. Our function does not need any special behaviour:

{ ocMyFunc, 200, 1, 2, V, { VR, VR }, 0,

Alternative name

The eighth entry in the function description is a C string specifying an alternative function name to be used in BIFF for add-in functions. If not used, it can be set to null:

{ ocMyFunc, 200, 1, 2, V, { VR, VR }, 0, 0},

That's it. The function will now be imported correctly from BIFF3-BIFF8 files and exported to BIFF5 and BIFF8 files. As an optimization or simplification, it is allowed to leave out the parameter type of trailing parameters, if they are equal to the type of their predecessor. In our case, we can write the parameter list as { VR }:

{ ocMyFunc, 200, 1, 2, V, { VR }, 0, 0},

This is especially useful for functions that take more arguments, e.g. the function REPLACE that takes 4 parameters of type VR:

{ ocReplace, 119, 4, 4, V, { VR }, 0, 0},

Advanced - function tables and filter source code extensions

There are a number of special cases to be aware of. The following section describes them in detail and gives examples of related existing functions. If they do not apply to your new function, just go ahead to the next chapter New file formats (OOXML and BIFF12).

Maximum parameter count

A few functions allow to pass the maximum number of supported parameters, which is 30 in the BIFF2-BIFF8 file formats. There is a placeholder MX that can be used for the maximum parameter count entry, e.g. for the SUM or CHOOSE function. Note the short parameter type lists. The entry { RX } means that all 30 parameters are of type RX, the entry { VO, RO } means that the first parameter is of type VO, and that all following parameters starting from second are of type RO. The MX placeholder becomes more important in the OOXML function tables (described below) where the maximum parameter count is not fixed to 30 anymore, but changes to 255 in OOXML and BIFF12 files.

A few functions do not expect any parameters, e.g. the functions TRUE, FALSE, and PI. In this case, the parameter type list can be left empty.

{ ocPi, 19, 0, 0, V, {}, 0, 0},

Volatile functions

If the result of a function may change on every evaluation of the function, the function has to be marked as volatile. For example, the functions NOW, TODAY, and RANDOM are volatile. To mark a volatile function, the flag EXC_FUNCFLAG_VOLATILE has to be passed in the seventh entry.

{ ocRandom, 63, 0, 0, V, {}, EXC_FUNCFLAG_VOLATILE, 0},

Restrict to import

Sometimes it is needed to restrict a function description to the import filter (hide it from the export filter). For that, the flag EXC_FUNCFLAG_IMPORTONLY can be used. For example, in Excel there exist the functions DOLLAR (BIFF index 13) and USDOLLAR (BIFF index 204). These functions behave equally (as far as we know) and are both mapped to the ODF function DOLLAR with the op-code ocCurrency. To prevent confusion in the export filter, the entry for the function DOLLAR is not marked (visible in import and export filters), but the entry for the function USDOLLAR is marked as import-only. This way, the export filter will always write the op-code ocCurrency as BIFF function DOLLAR with function index 13.

A few functions accept more parameters in Calc than in Excel. For example, the functions FLOOR and CEILING (introduced in BIFF4) are restricted to two parameters in Excel, but Calc supports a third parameters. These parameters have to be marked with the parameter type C. The import filter may react on this parameter type and add a default value for the parameter (e.g. for FLOOR and CEILING it is needed to add the value 1 to get the same behaviour as in Excel, see ExcelToSc::DoMulArgs() in sc/source/filter/excel/excform.cxx). The export filter will skip the parameter instead of producing an error for the entire function (e.g. the formula =FLOOR(1;1;1) will be exported as =FLOOR(1;1)).

If a BIFF version extends a function of a former BIFF version with new parameters, the respective function table needs to repeat the function description with the changed settings. For example, the function WEEKDAY (op-code ocGetDayOfWeek) has been introduced in BIFF2 with only one parameter. In BIFF5, a second optional parameter has been added, which is supported by Calc too. In consequence, the BIFF2 function table contains a function description for this function with a maximum parameter count of 1, which will be used in the following BIFF versions too. The second parameter is marked as Calc-only. The BIFF5 function table will contain a new function description with the new maximum parameter count of 2.

A few functions accept more parameters in Excel than in Calc. For example, the function PERCENTRANK accepts an optional third parameter in Excel, and the function EXTERN.CALL used internally by Excel for add-in function calls needs a hidden leading parameter containing the actual function name (more details below). These parameters have to be marked by appending _E to the actual parameter type, e.g. VR_E. On import, these parameters will be ignored, or (in case of the function EXTERN.CALL) evaluated internally. On export, the filter has to insert an appropriate value for the parameter if required, see XclExpFmlaCompImpl::AppendDefaultParam() in sc/source/filter/excel/xeformula.cxx.

A few functions are not supported by Excel, but the export filter may simulate them easily by using a similar functions. For example, the cotangent functions COT (cotangent), ACOT (arcos cotangent), COTH (cotangent hyperbolicus), and ACOTH (arcus cotangent hyperbolicus) do not exist in Excel, but COT(x) can be written as 1/TAN(x), ACOT(x) can be written as PI/2-ATAN(x), COTH(x) can be written as 1/TANH(x), and ACOTH(x) can be written as ATANH(1/x). To do that, some more work has to be done in the export filter in addition to the function description. In the following, handling of the functions COT and ACOTH will be discussed. First, we add function descriptions to the function table, containing the BIFF funtion index of the function used to simulate our new function (TAN with BIFF index 17 for COT, and ATANH with BIFF index 234 for ACOTH). The function will be marked as export-only, so that the import filter will ignore these function descriptions:

Next, the export filter has to be extended to add the new tokens needed to simulate our function. To be sure to not change the meaning of the entire formula, parentheses have to be inserted appropriately.

The term COT(x) has to be replaced with (1/TAN(x)), otherwise e.g. the formula =2^COT(x) will not be evaluated correctly as =2^(1/TAN(x)), but as =(2^1)/TAN(x).

The term ACOTH(x) has to be replaced with ATANH(1/(x)), otherwise e.g. the formula =ACOTH(1+1) will not be evaluated correctly as =ATANH(1/(1+1)), but as =ATANH((1/1)+1).

Formulas are stored in reverse polish notation (RPN). The term (1/TAN(x)) will be stored as token sequence "1,x,TAN,DIV,()" where x can be an arbitrarily complex expression. The term ATANH(1/(x)) will be stored as "1,x,(),DIV,ATANH" respectively. Currently, the export filter will already find the new function descriptions but would just write the specified BIFF function index (e.g. TAN(x) for COT(x)). The new tokens have to be added manually in the export filter code. All this is done in the file sc/source/filter/excel/xeformula.cxx.

COT: In the RPN array "1,x,TAN,DIV,()" (representing the function COT(x)), the token 1 has to be inserted in front of the entire function (consisting of the tokens "x,TAN"). To do this, a case will be inserted in the method XclExpFmlaCompImpl::PrepareFunction():

The division operator and the parentheses have to be appended to the entire function. This is done in the method XclExpFmlaCompImpl::FinishFunction(). The boolean value true in the call of AppendBinaryOperatorToken() specifies that the operator works on scalar values (in difference to e.g. the range operator ":").

ACOTH: In the token array "1,x,(),DIV,ATANH" (representing the function ACOTH(x)), the token 1 has to be inserted in front of the function parameter x (but not in front of the entire function). This is done in the method XclExpFmlaCompImpl::PrepareParam(). The variable nParamIdx contains the zero-based index of the current parameter:

The parentheses and the division operator have to be appended to the function parameter x. This is done in the method XclExpFmlaCompImpl::FinishParam(). Again, the variable nParamIdx contains the zero-based index of the current parameter:

Functions appearing as built-in function in Excel might in fact be stored as a call to an internal defined name. For example, the function BAHTTEXT is not stored with a BIFF function index, but internally as EXTERN.CALL("_xlfn.BAHTTEXT",parameter) where "_xlfn.BAHTTEXT" is a hidden parameter and contains a reference to the internal defined name (also known as named range) named "_xlfn.BAHTTEXT". This defined name is flagged to be a function call instead of a regular named range. If a formula calls a VBA macro, the function call is stored the same way using a dedicated defined name containing the name of the VBA macro. To add support for the BAHTTEXT function, the following function description can be used. It has to refer to the function EXTERN.CALL with the BIFF index 255, and has to specify the parameter type for the first hidden parameter explicitly (type RO_E). Note that all parameters of the function EXTERN.CALL have to be of type RO. The preprocessor macro EXC_FUNCNAME adds the "_xlfn." prefix to the function name:

The description for the export filter still has to refer to the function EXTERN.CALL. When loading a file with the BAHTTEXT function, internally the import filter will encounter the function EXTERN.CALL and will find the (already existing) function description

After reading the first hidden parameter of the function, the filter will find the function name "_xlfn.BAHTTEXT", will search the function table for a function description containing this name, will find our new description mentioned above, and will finally use the op-code ocBahttext.

Functions from external add-ins

In Excel, it is possible to call functions that are part of an external add-in. These add-ins are located in a special directory of the Excel installation, called the library directory. If such a function is built-in in Calc, some conversion has to be done. Currently, the only existing example is the function EUROCONVERT added to Excel with the add-in EUROTOOL (add-in library file eurotool.xla). All add-in functions (also the functions from the Analysis add-in) are stored using the internal function EXTERN.CALL (see previous section) which will contain a description of the add-in name and function name in its first hidden parameter. Thus, the formula =EUROCONVERT(1;"DEM";"EUR") will appear as =EXTERN.CALL("EUROTOOL.XLA!EUROCONVERT";1;"DEM";"EUR") in the Excel file. First, we need a function description in the BIFF8 function table, using the function EXTERN.CALL again. Again, all parameters of this function have to be of type RO:

The first hidden parameter is a reference to an external name being part of the description of the external workbook EUROTOOL.XLA. A detailed description would go beyond the scope here. To get an idea, grep the files sc/source/filter/excel/xilink.cxx, sc/source/filter/excel/excform8.cxx, and sc/source/filter/excel/xelink.cxx for xlExtEuroConvert and EXC_SBTYPE_EUROTOOL.

Functions not supported by Excel

The export filter can write functions to the Excel file format even if Excel does not know them. The import filter will restore these functions when loading the file. The function table saFuncTable_Odf contains all functions that will be handled this way. For example, the function BASE will be inserted as following:

EXC_FUNCENTRY_ODF( ocBase, 2, 3, 0, "BASE"),

The preprocessor macro EXC_FUNCENTRY_ODF converts this entry to the appropriate function descriptions (see the definition of the macro above the table). The first entry is the Calc op-code, the second entry is the minimum parameter count, the third entry is the maximum parameter count, the fourth entry will be used for additional flags, and the last entry is the ODF function name.

New file formats (OOXML and BIFF12)

Overview

As if that wasn't enough, nearly the same changes have to be done in the oox code module implementing filters for the Microsoft Office Open XML (OOXML) file format and the binary BIFF12 format. As already mentioned, in the future this filter will handle import and export of all Excel file formats including the old BIFF2-BIFF8 formats. Therefore, the function tables have been prepared to contain all information needed to import and export BIFF2-BIFF12 and OOXML files. The following files contain the source code for formula handling:

Source code used by the import and export filters is located in the files oox/inc/oox/xls/formulabase.hxx and oox/source/xls/formulabase.cxx. The cxx file contains all function tables.

The source code for formula import is located in the files oox/inc/oox/xls/formulaparser.hxx and oox/source/xls/formulaparser.cxx.

Currently, there is no source code for formula export.

Function tables

The structure of the function tables is similar to the structure of the old function tables in module sc described above. The main difference is, that the filter implementation is based completely on the OpenOffice.org API and therefore does not have access to Calc internals such as the function op-codes. Following a short overview of the entries in a function description. The function descriptions are structures of type ::oox::xls::FunctionData defined locally above the tables. But first, an example description for the function SUM:

{"SUM", "SUM", 4, 4, 0, MX, V, { RX }, 0},

ODF function name

The first entry is the function name as defined in OpenFormula. May be null, if not available (functions available in Excel only).

OOXML function name

The second entry is the function name as it appears in the OOXML file format. May be null, if not available (functions available in Calc only).

BIFF12 function index

The third entry is the function index used in BIFF12 files (file extension ".xlsb").

BIFF2-BIFF8 function index

The fourth entry is the function index used in BIFF2-BIFF8 files (file extension ".xls").

Minimum parameter count

The fifth entry is the minimum number of parameters required by Excel.

Maximum parameter count

The sixth entry is the maximum number of parameters allowed in Excel. The constant MX can be used to specify the maximum number allowed by the current file format, which is 30 in BIFF2-BIFF8 and 255 in BIFF12 and OOXML. The filters will find and use the correct maximum value internally.

Return type

The seventh entry is the type of the return value. As before, can be V for scalar values, A for arrays of values, or R for cell range addresses (references).

Parameter types

The eighth entry is a C array describing the type of all function parameters. This entry is equal to the arrays from the old BIFF filters described above.

Additional flags

The ninth and last entry contains additional flags controlling the behaviour of the filters. The three flags known from the old BIFF filters are available as well as some new flags:

FUNCFLAG_VOLATILE: The function result is volatile.

FUNCFLAG_IMPORTONLY: The function description is available for the import filters only.

FUNCFLAG_EXPORTONLY: The function description is available for the export filters only.

FUNCFLAG_MACROCALL: The BIFF filters will handle the function as call to a defined name (see previous chapter for details; the OOXML name will be prefixed with "_xlfn."). Has no effect on the OOXML filter.

FUNCFLAG_MACROCALLODF: Used to mark functions not available in Excel but in Calc only, to be able to preserve them in a roundtrip scenario.

FUNCFLAG_EXTERNAL: Used to mark functions that are stored externally in Calc (with op-code ocExternal). Currently, all functions from the Analysis add-in are marked with this flag.

FUNCFLAG_MACROFUNC: The function is a macrosheet function. This kind of functions is not supported in Calc, and is available in Excel macrosheets only (not in regular worksheets).

FUNCFLAG_MACROCMD: The function is a macrosheet command. This kind of functions is not supported in Calc, and is available in Excel macrosheets only (not in regular worksheets).

FUNCFLAG_ALWAYSVAR: The function is always handled as function with variable number of parameters, even if minimum and maximum number of parameters are equal.

FUNCFLAG_PARAMPAIRS: The function repeats the last two parameter types instead of only the last type for additional parameters. This is used e.g. for the function COUNTIFS (added in OOXML/BIFF12) that supports up to 255 parameters and always expects pairs of parameter with types RO, VR.

Advanced

Calc-only function parameters

Parameters marked with the C parameter type are only available in Calc (e.g. the third parameter of the functions FLOOR and CEILING). Everytime the import filter encounters such a parameter, it calls the method FormulaFinalizer::appendCalcOnlyParameter() in oox/source/xls/formulaparser.cxx allowing to set a default value for the parameter.

Add values for optional parameters

After a function has been imported completely, the method FormulaFinalizer::appendRequiredParameters() will be called allowing to extend the parameter list. This is useful for functions with parameters optional in Excel but required in Calc, e.g. the second parameter of the function WEEKNUM.

Add values for empty parameters

If the import filter encounters an empty parameter, it calls the method FormulaFinalizer::appendEmptyParameter(). The implementation may add a value in case Calc does not support an empty parameter here. Currently, this is used for the function IF. For example, the import filter replaces =IF(cond;) with =IF(cond;0) and =IF(cond;;) with =IF(cond;0;0).

Functions stored as call to an internal defined name

The function flag FUNCFLAG_MACROCALL is used to mark functions that are stored as call to an internal defined name (see previous chapter for details). It is still possible to specify an aternative BIFF function index.

In BIFF2-BIFF8, these functions are handled internally and do not need descriptions in the function tables. In BIFF12 and OOXML, these functions are stored as internal functions (there is no Analysis add-in anymore in Excel 2007 and later), and therefore get their own BIFF12 function index. Following an example for the description of the Analysis add-in function COMPLEX. Note the constant NOID used for the BIFF2-BIFF8 function index to specify that these formats do not support a BIFF index here. The import filter will find the syntax using the function EXTERN.CALL, and will resolve the function by the specified OOXML function name.

It is possible to call functions that are part of an external add-in. These add-ins are located in a special directory of the Excel installation, called the library directory. The following function description is for the function EUROCONVERT from the external add-in EUROTOOL which is located either in the file eurotool.xla (Excel 2003 and earlier) or in the file eurotool.xlam (Excel 2007 and later). Every add-in has a corresponding value in the enumeration FunctionLibraryType defined in oox/inc/oox/xls/formulabase.hxx. The macro FUNCLIB_TO_FUNCFLAGS converts this value to the appropriate function flags:

The function library flag triggers internal code that resolves a reference to the add-in file name. Adding a new add-in is quite easy compared to the old BIFF filters. Assuming the new add-in is called MYADDIN (file name myaddin.xla or myaddin.xlam) and contains the function MYADDINFUNC.

In oox/inc/oox/xls/formulabase.hxx, add a new value to the enumeration FunctionLibraryType:

In oox/source/xls/formulabase.cxx, add an entry in the method FunctionProvider::getFuncLibTypeFromLibraryName() that converts the passed library file name to a value of the enumeration FunctionLibraryType: