xref: /aoo41x/main/oox/source/xls/formulabase.cxx (revision ca5ec200)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "oox/xls/formulabase.hxx"
25 
26 #include <map>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/table/XCellRange.hpp>
29 #include <com/sun/star/sheet/AddressConvention.hpp>
30 #include <com/sun/star/sheet/ReferenceFlags.hpp>
31 #include <com/sun/star/sheet/SingleReference.hpp>
32 #include <com/sun/star/sheet/ComplexReference.hpp>
33 #include <com/sun/star/sheet/FormulaLanguage.hpp>
34 #include <com/sun/star/sheet/FormulaMapGroup.hpp>
35 #include <com/sun/star/sheet/FormulaMapGroupSpecialOffset.hpp>
36 #include <com/sun/star/sheet/XFormulaOpCodeMapper.hpp>
37 #include <com/sun/star/sheet/XFormulaParser.hpp>
38 #include <rtl/strbuf.hxx>
39 #include <rtl/ustrbuf.hxx>
40 #include "oox/core/filterbase.hxx"
41 #include "oox/helper/containerhelper.hxx"
42 #include "oox/xls/biffinputstream.hxx"
43 
44 namespace oox {
45 namespace xls {
46 
47 // ============================================================================
48 
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::sheet;
51 using namespace ::com::sun::star::table;
52 using namespace ::com::sun::star::uno;
53 
54 using ::rtl::OString;
55 using ::rtl::OStringBuffer;
56 using ::rtl::OStringToOUString;
57 using ::rtl::OUString;
58 using ::rtl::OUStringBuffer;
59 using ::rtl::OUStringToOString;
60 
61 // reference helpers ==========================================================
62 
63 BinSingleRef2d::BinSingleRef2d() :
64     mnCol( 0 ),
65     mnRow( 0 ),
66     mbColRel( false ),
67     mbRowRel( false )
68 {
69 }
70 
71 void BinSingleRef2d::setBiff12Data( sal_uInt16 nCol, sal_Int32 nRow, bool bRelativeAsOffset )
72 {
73     mnCol = nCol & BIFF12_TOK_REF_COLMASK;
74     mnRow = nRow & BIFF12_TOK_REF_ROWMASK;
75     mbColRel = getFlag( nCol, BIFF12_TOK_REF_COLREL );
76     mbRowRel = getFlag( nCol, BIFF12_TOK_REF_ROWREL );
77     if( bRelativeAsOffset && mbColRel && (mnCol > (BIFF12_TOK_REF_COLMASK >> 1)) )
78         mnCol -= (BIFF12_TOK_REF_COLMASK + 1);
79     if( bRelativeAsOffset && mbRowRel && (mnRow > (BIFF12_TOK_REF_ROWMASK >> 1)) )
80         mnRow -= (BIFF12_TOK_REF_ROWMASK + 1);
81 }
82 
83 void BinSingleRef2d::setBiff2Data( sal_uInt8 nCol, sal_uInt16 nRow, bool bRelativeAsOffset )
84 {
85     mnCol = nCol;
86     mnRow = nRow & BIFF_TOK_REF_ROWMASK;
87     mbColRel = getFlag( nRow, BIFF_TOK_REF_COLREL );
88     mbRowRel = getFlag( nRow, BIFF_TOK_REF_ROWREL );
89     if( bRelativeAsOffset && mbColRel && (mnCol >= 0x80) )
90         mnCol -= 0x100;
91     if( bRelativeAsOffset && mbRowRel && (mnRow > (BIFF_TOK_REF_ROWMASK >> 1)) )
92         mnRow -= (BIFF_TOK_REF_ROWMASK + 1);
93 }
94 
95 void BinSingleRef2d::setBiff8Data( sal_uInt16 nCol, sal_uInt16 nRow, bool bRelativeAsOffset )
96 {
97     mnCol = nCol & BIFF_TOK_REF_COLMASK;
98     mnRow = nRow;
99     mbColRel = getFlag( nCol, BIFF_TOK_REF_COLREL );
100     mbRowRel = getFlag( nCol, BIFF_TOK_REF_ROWREL );
101     if( bRelativeAsOffset && mbColRel && (mnCol > (BIFF_TOK_REF_COLMASK >> 1)) )
102         mnCol -= (BIFF_TOK_REF_COLMASK + 1);
103     if( bRelativeAsOffset && mbRowRel && (mnRow >= 0x8000) )
104         mnRow -= 0x10000;
105 }
106 
107 void BinSingleRef2d::readBiff12Data( SequenceInputStream& rStrm, bool bRelativeAsOffset )
108 {
109     sal_Int32 nRow;
110     sal_uInt16 nCol;
111     rStrm >> nRow >> nCol;
112     setBiff12Data( nCol, nRow, bRelativeAsOffset );
113 }
114 
115 void BinSingleRef2d::readBiff2Data( BiffInputStream& rStrm, bool bRelativeAsOffset )
116 {
117     sal_uInt16 nRow;
118     sal_uInt8 nCol;
119     rStrm >> nRow >> nCol;
120     setBiff2Data( nCol, nRow, bRelativeAsOffset );
121 }
122 
123 void BinSingleRef2d::readBiff8Data( BiffInputStream& rStrm, bool bRelativeAsOffset )
124 {
125     sal_uInt16 nRow, nCol;
126     rStrm >> nRow >> nCol;
127     setBiff8Data( nCol, nRow, bRelativeAsOffset );
128 }
129 
130 // ----------------------------------------------------------------------------
131 
132 void BinComplexRef2d::readBiff12Data( SequenceInputStream& rStrm, bool bRelativeAsOffset )
133 {
134     sal_Int32 nRow1, nRow2;
135     sal_uInt16 nCol1, nCol2;
136     rStrm >> nRow1 >> nRow2 >> nCol1 >> nCol2;
137     maRef1.setBiff12Data( nCol1, nRow1, bRelativeAsOffset );
138     maRef2.setBiff12Data( nCol2, nRow2, bRelativeAsOffset );
139 }
140 
141 void BinComplexRef2d::readBiff2Data( BiffInputStream& rStrm, bool bRelativeAsOffset )
142 {
143     sal_uInt16 nRow1, nRow2;
144     sal_uInt8 nCol1, nCol2;
145     rStrm >> nRow1 >> nRow2 >> nCol1 >> nCol2;
146     maRef1.setBiff2Data( nCol1, nRow1, bRelativeAsOffset );
147     maRef2.setBiff2Data( nCol2, nRow2, bRelativeAsOffset );
148 }
149 
150 void BinComplexRef2d::readBiff8Data( BiffInputStream& rStrm, bool bRelativeAsOffset )
151 {
152     sal_uInt16 nRow1, nRow2, nCol1, nCol2;
153     rStrm >> nRow1 >> nRow2 >> nCol1 >> nCol2;
154     maRef1.setBiff8Data( nCol1, nRow1, bRelativeAsOffset );
155     maRef2.setBiff8Data( nCol2, nRow2, bRelativeAsOffset );
156 }
157 
158 // token vector, sequence =====================================================
159 
160 ApiTokenVector::ApiTokenVector()
161 {
162 }
163 
164 Any& ApiTokenVector::append( sal_Int32 nOpCode )
165 {
166     resize( size() + 1 );
167     back().OpCode = nOpCode;
168     return back().Data;
169 }
170 
171 // token sequence iterator ====================================================
172 
173 ApiTokenIterator::ApiTokenIterator( const ApiTokenSequence& rTokens, sal_Int32 nSpacesOpCode, bool bSkipSpaces ) :
174     mpToken( rTokens.getConstArray() ),
175     mpTokenEnd( rTokens.getConstArray() + rTokens.getLength() ),
176     mnSpacesOpCode( nSpacesOpCode ),
177     mbSkipSpaces( bSkipSpaces )
178 {
179     skipSpaces();
180 }
181 
182 ApiTokenIterator::ApiTokenIterator( const ApiTokenIterator& rIter, bool bSkipSpaces ) :
183     mpToken( rIter.mpToken ),
184     mpTokenEnd( rIter.mpTokenEnd ),
185     mnSpacesOpCode( rIter.mnSpacesOpCode ),
186     mbSkipSpaces( bSkipSpaces )
187 {
188     skipSpaces();
189 }
190 
191 ApiTokenIterator& ApiTokenIterator::operator++()
192 {
193     if( is() )
194     {
195         ++mpToken;
196         skipSpaces();
197     }
198     return *this;
199 }
200 
201 void ApiTokenIterator::skipSpaces()
202 {
203     if( mbSkipSpaces )
204         while( is() && (mpToken->OpCode == mnSpacesOpCode) )
205             ++mpToken;
206 }
207 
208 // function data ==============================================================
209 
210 namespace {
211 
212 const size_t FUNCINFO_PARAMINFOCOUNT        = 5;        /// Number of parameter type entries.
213 
214 const sal_uInt16 FUNCFLAG_VOLATILE          = 0x0001;   /// Result is volatile (e.g. NOW() function).
215 const sal_uInt16 FUNCFLAG_IMPORTONLY        = 0x0002;   /// Only used in import filter.
216 const sal_uInt16 FUNCFLAG_EXPORTONLY        = 0x0004;   /// Only used in export filter.
217 const sal_uInt16 FUNCFLAG_MACROCALL         = 0x0008;   /// Function is stored as macro call in Excel (_xlfn. prefix). OOXML name MUST exist.
218 const sal_uInt16 FUNCFLAG_MACROCALLODF      = 0x0010;   /// ODF-only function stored as macro call in Excel (_xlfnodf. prefix). ODF name MUST exist.
219 const sal_uInt16 FUNCFLAG_EXTERNAL          = 0x0020;   /// Function is external in Calc.
220 const sal_uInt16 FUNCFLAG_MACROFUNC         = 0x0040;   /// Function is a macro-sheet function.
221 const sal_uInt16 FUNCFLAG_MACROCMD          = 0x0080;   /// Function is a macro-sheet command.
222 const sal_uInt16 FUNCFLAG_ALWAYSVAR         = 0x0100;   /// Function is always represented by a tFuncVar token.
223 const sal_uInt16 FUNCFLAG_PARAMPAIRS        = 0x0200;   /// Optional parameters are expected to appear in pairs.
224 
225 /// Converts a function library index (value of enum FunctionLibraryType) to function flags.
226 #define FUNCLIB_TO_FUNCFLAGS( funclib_index ) static_cast< sal_uInt16 >( static_cast< sal_uInt8 >( funclib_index ) << 12 )
227 /// Extracts a function library index (value of enum FunctionLibraryType) from function flags.
228 #define FUNCFLAGS_TO_FUNCLIB( func_flags ) extractValue< FunctionLibraryType >( func_flags, 12, 4 )
229 
230 typedef ::boost::shared_ptr< FunctionInfo > FunctionInfoRef;
231 
232 struct FunctionData
233 {
234     const sal_Char*     mpcOdfFuncName;     /// ODF function name.
235     const sal_Char*     mpcOoxFuncName;     /// OOXML function name.
236     sal_uInt16          mnBiff12FuncId;     /// BIFF12 function identifier.
237     sal_uInt16          mnBiffFuncId;       /// BIFF2-BIFF8 function identifier.
238     sal_uInt8           mnMinParamCount;    /// Minimum number of parameters.
239     sal_uInt8           mnMaxParamCount;    /// Maximum number of parameters.
240     sal_uInt8           mnRetClass;         /// BIFF token class of the return value.
241     FunctionParamInfo   mpParamInfos[ FUNCINFO_PARAMINFOCOUNT ]; /// Information about all parameters.
242     sal_uInt16          mnFlags;            /// Additional flags.
243 
244     inline bool         isSupported( bool bImportFilter ) const;
245 };
246 
247 inline bool FunctionData::isSupported( bool bImportFilter ) const
248 {
249     /*  For import filters: the FUNCFLAG_EXPORTONLY flag must not be set,
250         for export filters: the FUNCFLAG_IMPORTONLY flag must not be set. */
251     return !getFlag( mnFlags, bImportFilter ? FUNCFLAG_EXPORTONLY : FUNCFLAG_IMPORTONLY );
252 }
253 
254 const sal_uInt16 NOID = SAL_MAX_UINT16;     /// No BIFF function identifier available.
255 const sal_uInt8 MX    = SAL_MAX_UINT8;      /// Maximum parameter count.
256 
257 // abbreviations for function return token class
258 const sal_uInt8 R = BIFF_TOKCLASS_REF;
259 const sal_uInt8 V = BIFF_TOKCLASS_VAL;
260 const sal_uInt8 A = BIFF_TOKCLASS_ARR;
261 
262 // abbreviations for parameter infos
263 #define RO   { FUNC_PARAM_REGULAR,   FUNC_PARAMCONV_ORG, false }
264 #define RV   { FUNC_PARAM_REGULAR,   FUNC_PARAMCONV_VAL, false }
265 #define RA   { FUNC_PARAM_REGULAR,   FUNC_PARAMCONV_ARR, false }
266 #define RR   { FUNC_PARAM_REGULAR,   FUNC_PARAMCONV_RPT, false }
267 #define RX   { FUNC_PARAM_REGULAR,   FUNC_PARAMCONV_RPX, false }
268 #define VO   { FUNC_PARAM_REGULAR,   FUNC_PARAMCONV_ORG, true  }
269 #define VV   { FUNC_PARAM_REGULAR,   FUNC_PARAMCONV_VAL, true  }
270 #define VA   { FUNC_PARAM_REGULAR,   FUNC_PARAMCONV_ARR, true  }
271 #define VR   { FUNC_PARAM_REGULAR,   FUNC_PARAMCONV_RPT, true  }
272 #define VX   { FUNC_PARAM_REGULAR,   FUNC_PARAMCONV_RPX, true  }
273 #define RO_E { FUNC_PARAM_EXCELONLY, FUNC_PARAMCONV_ORG, false }
274 #define VR_E { FUNC_PARAM_EXCELONLY, FUNC_PARAMCONV_RPT, true  }
275 #define C    { FUNC_PARAM_CALCONLY,  FUNC_PARAMCONV_ORG, false }
276 
277 // Note: parameter types of all macro sheet functions (FUNCFLAG_MACROFUNC/FUNCFLAG_MACROCMD) untested!
278 
279 /** Functions new in BIFF2. */
280 static const FunctionData saFuncTableBiff2[] =
281 {
282     { "COUNT",                  "COUNT",                0,      0,      0,  MX, V, { RX }, 0 },
283     { "IF",                     "IF",                   1,      1,      2,  3,  R, { VO, RO }, 0 },
284     { "ISNA",                   "ISNA",                 2,      2,      1,  1,  V, { VR }, 0 },
285     { "ISERROR",                "ISERROR",              3,      3,      1,  1,  V, { VR }, 0 },
286     { "SUM",                    "SUM",                  4,      4,      0,  MX, V, { RX }, 0 },
287     { "AVERAGE",                "AVERAGE",              5,      5,      1,  MX, V, { RX }, 0 },
288     { "MIN",                    "MIN",                  6,      6,      1,  MX, V, { RX }, 0 },
289     { "MAX",                    "MAX",                  7,      7,      1,  MX, V, { RX }, 0 },
290     { "ROW",                    "ROW",                  8,      8,      0,  1,  V, { RO }, 0 },
291     { "COLUMN",                 "COLUMN",               9,      9,      0,  1,  V, { RO }, 0 },
292     { "NA",                     "NA",                   10,     10,     0,  0,  V, {}, 0 },
293     { "NPV",                    "NPV",                  11,     11,     2,  MX, V, { VR, RX }, 0 },
294     { "STDEV",                  "STDEV",                12,     12,     1,  MX, V, { RX }, 0 },
295     { "DOLLAR",                 "DOLLAR",               13,     13,     1,  2,  V, { VR }, 0 },
296     { "FIXED",                  "FIXED",                14,     14,     1,  2,  V, { VR, VR, C }, 0 },
297     { "SIN",                    "SIN",                  15,     15,     1,  1,  V, { VR }, 0 },
298     { "CSC",                    "SIN",                  15,     15,     1,  1,  V, { VR }, FUNCFLAG_EXPORTONLY },
299     { "COS",                    "COS",                  16,     16,     1,  1,  V, { VR }, 0 },
300     { "SEC",                    "COS",                  16,     16,     1,  1,  V, { VR }, FUNCFLAG_EXPORTONLY },
301     { "TAN",                    "TAN",                  17,     17,     1,  1,  V, { VR }, 0 },
302     { "COT",                    "TAN",                  17,     17,     1,  1,  V, { VR }, FUNCFLAG_EXPORTONLY },
303     { "ATAN",                   "ATAN",                 18,     18,     1,  1,  V, { VR }, 0 },
304     { "ACOT",                   "ATAN",                 18,     18,     1,  1,  V, { VR }, FUNCFLAG_EXPORTONLY },
305     { "PI",                     "PI",                   19,     19,     0,  0,  V, {}, 0 },
306     { "SQRT",                   "SQRT",                 20,     20,     1,  1,  V, { VR }, 0 },
307     { "EXP",                    "EXP",                  21,     21,     1,  1,  V, { VR }, 0 },
308     { "LN",                     "LN",                   22,     22,     1,  1,  V, { VR }, 0 },
309     { "LOG10",                  "LOG10",                23,     23,     1,  1,  V, { VR }, 0 },
310     { "ABS",                    "ABS",                  24,     24,     1,  1,  V, { VR }, 0 },
311     { "INT",                    "INT",                  25,     25,     1,  1,  V, { VR }, 0 },
312     { "SIGN",                   "SIGN",                 26,     26,     1,  1,  V, { VR }, 0 },
313     { "ROUND",                  "ROUND",                27,     27,     2,  2,  V, { VR }, 0 },
314     { "LOOKUP",                 "LOOKUP",               28,     28,     2,  3,  V, { VR, RA }, 0 },
315     { "INDEX",                  "INDEX",                29,     29,     2,  4,  R, { RA, VV }, 0 },
316     { "REPT",                   "REPT",                 30,     30,     2,  2,  V, { VR }, 0 },
317     { "MID",                    "MID",                  31,     31,     3,  3,  V, { VR }, 0 },
318     { "LEN",                    "LEN",                  32,     32,     1,  1,  V, { VR }, 0 },
319     { "VALUE",                  "VALUE",                33,     33,     1,  1,  V, { VR }, 0 },
320     { "TRUE",                   "TRUE",                 34,     34,     0,  0,  V, {}, 0 },
321     { "FALSE",                  "FALSE",                35,     35,     0,  0,  V, {}, 0 },
322     { "AND",                    "AND",                  36,     36,     1,  MX, V, { RX }, 0 },
323     { "OR",                     "OR",                   37,     37,     1,  MX, V, { RX }, 0 },
324     { "NOT",                    "NOT",                  38,     38,     1,  1,  V, { VR }, 0 },
325     { "MOD",                    "MOD",                  39,     39,     2,  2,  V, { VR }, 0 },
326     { "DCOUNT",                 "DCOUNT",               40,     40,     3,  3,  V, { RO, RR }, 0 },
327     { "DSUM",                   "DSUM",                 41,     41,     3,  3,  V, { RO, RR }, 0 },
328     { "DAVERAGE",               "DAVERAGE",             42,     42,     3,  3,  V, { RO, RR }, 0 },
329     { "DMIN",                   "DMIN",                 43,     43,     3,  3,  V, { RO, RR }, 0 },
330     { "DMAX",                   "DMAX",                 44,     44,     3,  3,  V, { RO, RR }, 0 },
331     { "DSTDEV",                 "DSTDEV",               45,     45,     3,  3,  V, { RO, RR }, 0 },
332     { "VAR",                    "VAR",                  46,     46,     1,  MX, V, { RX }, 0 },
333     { "DVAR",                   "DVAR",                 47,     47,     3,  3,  V, { RO, RR }, 0 },
334     { "TEXT",                   "TEXT",                 48,     48,     2,  2,  V, { VR }, 0 },
335     { "LINEST",                 "LINEST",               49,     49,     1,  2,  A, { RA, RA, C, C }, 0 },
336     { "TREND",                  "TREND",                50,     50,     1,  3,  A, { RA, RA, RA, C }, 0 },
337     { "LOGEST",                 "LOGEST",               51,     51,     1,  2,  A, { RA, RA, C, C }, 0 },
338     { "GROWTH",                 "GROWTH",               52,     52,     1,  3,  A, { RA, RA, RA, C }, 0 },
339     { "PV",                     "PV",                   56,     56,     3,  5,  V, { VR }, 0 },
340     { "FV",                     "FV",                   57,     57,     3,  5,  V, { VR }, 0 },
341     { "NPER",                   "NPER",                 58,     58,     3,  5,  V, { VR }, 0 },
342     { "PMT",                    "PMT",                  59,     59,     3,  5,  V, { VR }, 0 },
343     { "RATE",                   "RATE",                 60,     60,     3,  6,  V, { VR }, 0 },
344     { "MIRR",                   "MIRR",                 61,     61,     3,  3,  V, { RA, VR }, 0 },
345     { "IRR",                    "IRR",                  62,     62,     1,  2,  V, { RA, VR }, 0 },
346     { "RAND",                   "RAND",                 63,     63,     0,  0,  V, {}, FUNCFLAG_VOLATILE },
347     { "MATCH",                  "MATCH",                64,     64,     2,  3,  V, { VR, RX, RR }, 0 },
348     { "DATE",                   "DATE",                 65,     65,     3,  3,  V, { VR }, 0 },
349     { "TIME",                   "TIME",                 66,     66,     3,  3,  V, { VR }, 0 },
350     { "DAY",                    "DAY",                  67,     67,     1,  1,  V, { VR }, 0 },
351     { "MONTH",                  "MONTH",                68,     68,     1,  1,  V, { VR }, 0 },
352     { "YEAR",                   "YEAR",                 69,     69,     1,  1,  V, { VR }, 0 },
353     { "WEEKDAY",                "WEEKDAY",              70,     70,     1,  1,  V, { VR, C }, 0 },
354     { "HOUR",                   "HOUR",                 71,     71,     1,  1,  V, { VR }, 0 },
355     { "MINUTE",                 "MINUTE",               72,     72,     1,  1,  V, { VR }, 0 },
356     { "SECOND",                 "SECOND",               73,     73,     1,  1,  V, { VR }, 0 },
357     { "NOW",                    "NOW",                  74,     74,     0,  0,  V, {}, FUNCFLAG_VOLATILE },
358     { "AREAS",                  "AREAS",                75,     75,     1,  1,  V, { RO }, 0 },
359     { "ROWS",                   "ROWS",                 76,     76,     1,  1,  V, { RO }, 0 },
360     { "COLUMNS",                "COLUMNS",              77,     77,     1,  1,  V, { RO }, 0 },
361     { "OFFSET",                 "OFFSET",               78,     78,     3,  5,  R, { RO, VR }, FUNCFLAG_VOLATILE },
362     { "SEARCH",                 "SEARCH",               82,     82,     2,  3,  V, { VR }, 0 },
363     { "TRANSPOSE",              "TRANSPOSE",            83,     83,     1,  1,  A, { VO }, 0 },
364     { "TYPE",                   "TYPE",                 86,     86,     1,  1,  V, { VX }, 0 },
365     { "ATAN2",                  "ATAN2",                97,     97,     2,  2,  V, { VR }, 0 },
366     { "ASIN",                   "ASIN",                 98,     98,     1,  1,  V, { VR }, 0 },
367     { "ACOS",                   "ACOS",                 99,     99,     1,  1,  V, { VR }, 0 },
368     { "CHOOSE",                 "CHOOSE",               100,    100,    2,  MX, R, { VO, RO }, 0 },
369     { "HLOOKUP",                "HLOOKUP",              101,    101,    3,  3,  V, { VV, RO, RO, C }, 0 },
370     { "VLOOKUP",                "VLOOKUP",              102,    102,    3,  3,  V, { VV, RO, RO, C }, 0 },
371     { "ISREF",                  "ISREF",                105,    105,    1,  1,  V, { RX }, 0 },
372     { "LOG",                    "LOG",                  109,    109,    1,  2,  V, { VR }, 0 },
373     { "CHAR",                   "CHAR",                 111,    111,    1,  1,  V, { VR }, 0 },
374     { "LOWER",                  "LOWER",                112,    112,    1,  1,  V, { VR }, 0 },
375     { "UPPER",                  "UPPER",                113,    113,    1,  1,  V, { VR }, 0 },
376     { "PROPER",                 "PROPER",               114,    114,    1,  1,  V, { VR }, 0 },
377     { "LEFT",                   "LEFT",                 115,    115,    1,  2,  V, { VR }, 0 },
378     { "RIGHT",                  "RIGHT",                116,    116,    1,  2,  V, { VR }, 0 },
379     { "EXACT",                  "EXACT",                117,    117,    2,  2,  V, { VR }, 0 },
380     { "TRIM",                   "TRIM",                 118,    118,    1,  1,  V, { VR }, 0 },
381     { "REPLACE",                "REPLACE",              119,    119,    4,  4,  V, { VR }, 0 },
382     { "SUBSTITUTE",             "SUBSTITUTE",           120,    120,    3,  4,  V, { VR }, 0 },
383     { "CODE",                   "CODE",                 121,    121,    1,  1,  V, { VR }, 0 },
384     { "FIND",                   "FIND",                 124,    124,    2,  3,  V, { VR }, 0 },
385     { "CELL",                   "CELL",                 125,    125,    1,  2,  V, { VV, RO }, FUNCFLAG_VOLATILE },
386     { "ISERR",                  "ISERR",                126,    126,    1,  1,  V, { VR }, 0 },
387     { "ISTEXT",                 "ISTEXT",               127,    127,    1,  1,  V, { VR }, 0 },
388     { "ISNUMBER",               "ISNUMBER",             128,    128,    1,  1,  V, { VR }, 0 },
389     { "ISBLANK",                "ISBLANK",              129,    129,    1,  1,  V, { VR }, 0 },
390     { "T",                      "T",                    130,    130,    1,  1,  V, { RO }, 0 },
391     { "N",                      "N",                    131,    131,    1,  1,  V, { RO }, 0 },
392     { "DATEVALUE",              "DATEVALUE",            140,    140,    1,  1,  V, { VR }, 0 },
393     { "TIMEVALUE",              "TIMEVALUE",            141,    141,    1,  1,  V, { VR }, 0 },
394     { "SLN",                    "SLN",                  142,    142,    3,  3,  V, { VR }, 0 },
395     { "SYD",                    "SYD",                  143,    143,    4,  4,  V, { VR }, 0 },
396     { "DDB",                    "DDB",                  144,    144,    4,  5,  V, { VR }, 0 },
397     { "INDIRECT",               "INDIRECT",             148,    148,    1,  2,  R, { VR }, FUNCFLAG_VOLATILE },
398     { "CLEAN",                  "CLEAN",                162,    162,    1,  1,  V, { VR }, 0 },
399     { "MDETERM",                "MDETERM",              163,    163,    1,  1,  V, { VA }, 0 },
400     { "MINVERSE",               "MINVERSE",             164,    164,    1,  1,  A, { VA }, 0 },
401     { "MMULT",                  "MMULT",                165,    165,    2,  2,  A, { VA }, 0 },
402     { "IPMT",                   "IPMT",                 167,    167,    4,  6,  V, { VR }, 0 },
403     { "PPMT",                   "PPMT",                 168,    168,    4,  6,  V, { VR }, 0 },
404     { "COUNTA",                 "COUNTA",               169,    169,    0,  MX, V, { RX }, 0 },
405     { "PRODUCT",                "PRODUCT",              183,    183,    0,  MX, V, { RX }, 0 },
406     { "FACT",                   "FACT",                 184,    184,    1,  1,  V, { VR }, 0 },
407     { "DPRODUCT",               "DPRODUCT",             189,    189,    3,  3,  V, { RO, RR }, 0 },
408     { "ISNONTEXT",              "ISNONTEXT",            190,    190,    1,  1,  V, { VR }, 0 },
409     { "STDEVP",                 "STDEVP",               193,    193,    1,  MX, V, { RX }, 0 },
410     { "VARP",                   "VARP",                 194,    194,    1,  MX, V, { RX }, 0 },
411     { "DSTDEVP",                "DSTDEVP",              195,    195,    3,  3,  V, { RO, RR }, 0 },
412     { "DVARP",                  "DVARP",                196,    196,    3,  3,  V, { RO, RR }, 0 },
413     { "TRUNC",                  "TRUNC",                197,    197,    1,  1,  V, { VR, C }, 0 },
414     { "ISLOGICAL",              "ISLOGICAL",            198,    198,    1,  1,  V, { VR }, 0 },
415     { "DCOUNTA",                "DCOUNTA",              199,    199,    3,  3,  V, { RO, RR }, 0 },
416     { 0,                        "EXTERN.CALL",          255,    255,    1,  MX, R, { RO_E, RO }, FUNCFLAG_IMPORTONLY },
417 
418     // *** macro sheet commands ***
419 
420     { 0,                        "A1.R1C1",              30,     30,     0,  1,  V, { VR }, FUNCFLAG_MACROCMD },
421     { 0,                        "RETURN",               55,     55,     0,  1,  R, { RO }, FUNCFLAG_MACROFUNC },
422     { 0,                        "ABSREF",               79,     79,     2,  2,  R, { VR, RO }, FUNCFLAG_MACROFUNC },
423     { 0,                        "ADD.ARROW",            81,     81,     0,  0,  V, {}, FUNCFLAG_MACROCMD },
424     { 0,                        "ACTIVE.CELL",          94,     94,     0,  0,  R, {}, FUNCFLAG_MACROFUNC },
425     { 0,                        "ACTIVATE",             103,    103,    0,  2,  V, { VR }, FUNCFLAG_MACROCMD },
426     { 0,                        "ACTIVATE.NEXT",        104,    104,    0,  0,  V, {}, FUNCFLAG_MACROCMD },
427     { 0,                        "ACTIVATE.PREV",        105,    105,    0,  0,  V, {}, FUNCFLAG_MACROCMD },
428     { 0,                        "ADD.BAR",              151,    151,    0,  0,  V, {}, FUNCFLAG_MACROFUNC | FUNCFLAG_ALWAYSVAR },
429     { 0,                        "ADD.MENU",             152,    152,    2,  2,  V, { VR, RO }, FUNCFLAG_MACROFUNC | FUNCFLAG_ALWAYSVAR },
430     { 0,                        "ADD.COMMAND",          153,    153,    3,  3,  V, { VR, RO }, FUNCFLAG_MACROFUNC | FUNCFLAG_ALWAYSVAR }
431 };
432 
433 /** Functions new in BIFF3. */
434 static const FunctionData saFuncTableBiff3[] =
435 {
436     { "LINEST",                 "LINEST",               49,     49,     1,  4,  A, { RA, RA, VV }, 0 },             // BIFF2: 1-2, BIFF3: 1-4
437     { "TREND",                  "TREND",                50,     50,     1,  4,  A, { RA, RA, RA, VV }, 0 },             // BIFF2: 1-3, BIFF3: 1-4
438     { "LOGEST",                 "LOGEST",               51,     51,     1,  4,  A, { RA, RA, VV }, 0 },             // BIFF2: 1-2, BIFF3: 1-4
439     { "GROWTH",                 "GROWTH",               52,     52,     1,  4,  A, { RA, RA, RA, VV }, 0 },             // BIFF2: 1-3, BIFF3: 1-4
440     { "TRUNC",                  "TRUNC",                197,    197,    1,  2,  V, { VR }, 0 },                      // BIFF2: 1,   BIFF3: 1-2
441     { "DOLLAR",                 "USDOLLAR",             204,    204,    1,  2,  V, { VR }, FUNCFLAG_IMPORTONLY },
442     { 0/*"FIND"*/,              "FINDB",                205,    205,    2,  3,  V, { VR }, 0 },
443     { 0/*"SEARCH"*/,            "SEARCHB",              206,    206,    2,  3,  V, { VR }, 0 },
444     { 0/*"REPLACE"*/,           "REPLACEB",             207,    207,    4,  4,  V, { VR }, 0 },
445     { 0/*"LEFT"*/,              "LEFTB",                208,    208,    1,  2,  V, { VR }, 0 },
446     { 0/*"RIGHT"*/,             "RIGHTB",               209,    209,    1,  2,  V, { VR }, 0 },
447     { 0/*"MID"*/,               "MIDB",                 210,    210,    3,  3,  V, { VR }, 0 },
448     { 0/*"LEN"*/,               "LENB",                 211,    211,    1,  1,  V, { VR }, 0 },
449     { "ROUNDUP",                "ROUNDUP",              212,    212,    2,  2,  V, { VR }, 0 },
450     { "ROUNDDOWN",              "ROUNDDOWN",            213,    213,    2,  2,  V, { VR }, 0 },
451     { "ASC",                    "ASC",                  214,    214,    1,  1,  V, { VR }, 0 },
452     { "JIS",                    "DBCS",                 215,    215,    1,  1,  V, { VR }, 0 },
453     { "ADDRESS",                "ADDRESS",              219,    219,    2,  5,  V, { VR }, 0 },
454     { "DAYS360",                "DAYS360",              220,    220,    2,  2,  V, { VR, VR, C }, 0 },
455     { "TODAY",                  "TODAY",                221,    221,    0,  0,  V, {}, FUNCFLAG_VOLATILE },
456     { "VDB",                    "VDB",                  222,    222,    5,  7,  V, { VR }, 0 },
457     { "MEDIAN",                 "MEDIAN",               227,    227,    1,  MX, V, { RX }, 0 },
458     { "SUMPRODUCT",             "SUMPRODUCT",           228,    228,    1,  MX, V, { VA }, 0 },
459     { "SINH",                   "SINH",                 229,    229,    1,  1,  V, { VR }, 0 },
460     { "CSCH",                   "SINH",                 229,    229,    1,  1,  V, { VR }, FUNCFLAG_EXPORTONLY },
461     { "COSH",                   "COSH",                 230,    230,    1,  1,  V, { VR }, 0 },
462     { "SECH",                   "COSH",                 230,    230,    1,  1,  V, { VR }, FUNCFLAG_EXPORTONLY },
463     { "TANH",                   "TANH",                 231,    231,    1,  1,  V, { VR }, 0 },
464     { "COTH",                   "TANH",                 231,    231,    1,  1,  V, { VR }, FUNCFLAG_EXPORTONLY },
465     { "ASINH",                  "ASINH",                232,    232,    1,  1,  V, { VR }, 0 },
466     { "ACOSH",                  "ACOSH",                233,    233,    1,  1,  V, { VR }, 0 },
467     { "ATANH",                  "ATANH",                234,    234,    1,  1,  V, { VR }, 0 },
468     { "ACOTH",                  "ATANH",                234,    234,    1,  1,  V, { VR }, FUNCFLAG_EXPORTONLY },
469     { "DGET",                   "DGET",                 235,    235,    3,  3,  V, { RO, RR }, 0 },
470     { "INFO",                   "INFO",                 244,    244,    1,  1,  V, { VR }, FUNCFLAG_VOLATILE },
471 
472     // *** macro sheet commands ***
473 
474     { 0,                        "ADD.BAR",              151,    151,    0,  1,  V, { VR }, FUNCFLAG_MACROFUNC },    // BIFF2: 0,   BIFF3: 0-1
475     { 0,                        "ADD.MENU",             152,    152,    2,  3,  V, { VR, RO }, FUNCFLAG_MACROFUNC },  // BIFF2: 2,   BIFF3: 2-3
476     { 0,                        "ADD.COMMAND",          153,    153,    3,  4,  V, { VR, RO }, FUNCFLAG_MACROFUNC }   // BIFF2: 3,   BIFF3: 3-4
477 };
478 
479 /** Functions new in BIFF4. */
480 static const FunctionData saFuncTableBiff4[] =
481 {
482     { "FIXED",                  "FIXED",                14,     14,     1,  3,  V, { VR }, 0 },       // BIFF2-3: 1-2, BIFF4: 1-3
483     { "RANK",                   "RANK",                 216,    216,    2,  3,  V, { VR, RO, VR }, 0 },
484     { "DB",                     "DB",                   247,    247,    4,  5,  V, { VR }, 0 },
485     { "FREQUENCY",              "FREQUENCY",            252,    252,    2,  2,  A, { RA }, 0 },
486     { "ORG.OPENOFFICE.ERRORTYPE","ERROR.TYPE",          261,    261,    1,  1,  V, { VR }, 0 },
487     { "AVEDEV",                 "AVEDEV",               269,    269,    1,  MX, V, { RX }, 0 },
488     { "BETADIST",               "BETADIST",             270,    270,    3,  5,  V, { VR }, 0 },
489     { "GAMMALN",                "GAMMALN",              271,    271,    1,  1,  V, { VR }, 0 },
490     { "BETAINV",                "BETAINV",              272,    272,    3,  5,  V, { VR }, 0 },
491     { "BINOMDIST",              "BINOMDIST",            273,    273,    4,  4,  V, { VR }, 0 },
492     { "LEGACY.CHIDIST",         "CHIDIST",              274,    274,    2,  2,  V, { VR }, 0 },
493     { "LEGACY.CHIINV",          "CHIINV",               275,    275,    2,  2,  V, { VR }, 0 },
494     { "COMBIN",                 "COMBIN",               276,    276,    2,  2,  V, { VR }, 0 },
495     { "CONFIDENCE",             "CONFIDENCE",           277,    277,    3,  3,  V, { VR }, 0 },
496     { "CRITBINOM",              "CRITBINOM",            278,    278,    3,  3,  V, { VR }, 0 },
497     { "EVEN",                   "EVEN",                 279,    279,    1,  1,  V, { VR }, 0 },
498     { "EXPONDIST",              "EXPONDIST",            280,    280,    3,  3,  V, { VR }, 0 },
499     { "LEGACY.FDIST",           "FDIST",                281,    281,    3,  3,  V, { VR }, 0 },
500     { "LEGACY.FINV",            "FINV",                 282,    282,    3,  3,  V, { VR }, 0 },
501     { "FISHER",                 "FISHER",               283,    283,    1,  1,  V, { VR }, 0 },
502     { "FISHERINV",              "FISHERINV",            284,    284,    1,  1,  V, { VR }, 0 },
503     { "FLOOR",                  "FLOOR",                285,    285,    2,  2,  V, { VR, VR, C }, 0 },
504     { "GAMMADIST",              "GAMMADIST",            286,    286,    4,  4,  V, { VR }, 0 },
505     { "GAMMAINV",               "GAMMAINV",             287,    287,    3,  3,  V, { VR }, 0 },
506     { "CEILING",                "CEILING",              288,    288,    2,  2,  V, { VR, VR, C }, 0 },
507     { "HYPGEOMDIST",            "HYPGEOMDIST",          289,    289,    4,  4,  V, { VR }, 0 },
508     { "LOGNORMDIST",            "LOGNORMDIST",          290,    290,    3,  3,  V, { VR }, 0 },
509     { "LOGINV",                 "LOGINV",               291,    291,    3,  3,  V, { VR }, 0 },
510     { "NEGBINOMDIST",           "NEGBINOMDIST",         292,    292,    3,  3,  V, { VR }, 0 },
511     { "NORMDIST",               "NORMDIST",             293,    293,    4,  4,  V, { VR }, 0 },
512     { "LEGACY.NORMSDIST",       "NORMSDIST",            294,    294,    1,  1,  V, { VR }, 0 },
513     { "NORMINV",                "NORMINV",              295,    295,    3,  3,  V, { VR }, 0 },
514     { "LEGACY.NORMSINV",        "NORMSINV",             296,    296,    1,  1,  V, { VR }, 0 },
515     { "STANDARDIZE",            "STANDARDIZE",          297,    297,    3,  3,  V, { VR }, 0 },
516     { "ODD",                    "ODD",                  298,    298,    1,  1,  V, { VR }, 0 },
517     { "PERMUT",                 "PERMUT",               299,    299,    2,  2,  V, { VR }, 0 },
518     { "POISSON",                "POISSON",              300,    300,    3,  3,  V, { VR }, 0 },
519     { "TDIST",                  "TDIST",                301,    301,    3,  3,  V, { VR }, 0 },
520     { "WEIBULL",                "WEIBULL",              302,    302,    4,  4,  V, { VR }, 0 },
521     { "SUMXMY2",                "SUMXMY2",              303,    303,    2,  2,  V, { VA }, 0 },
522     { "SUMX2MY2",               "SUMX2MY2",             304,    304,    2,  2,  V, { VA }, 0 },
523     { "SUMX2PY2",               "SUMX2PY2",             305,    305,    2,  2,  V, { VA }, 0 },
524     { "LEGACY.CHITEST",         "CHITEST",              306,    306,    2,  2,  V, { VA }, 0 },
525     { "CORREL",                 "CORREL",               307,    307,    2,  2,  V, { VA }, 0 },
526     { "COVAR",                  "COVAR",                308,    308,    2,  2,  V, { VA }, 0 },
527     { "FORECAST",               "FORECAST",             309,    309,    3,  3,  V, { VR, VA }, 0 },
528     { "FTEST",                  "FTEST",                310,    310,    2,  2,  V, { VA }, 0 },
529     { "INTERCEPT",              "INTERCEPT",            311,    311,    2,  2,  V, { VA }, 0 },
530     { "PEARSON",                "PEARSON",              312,    312,    2,  2,  V, { VA }, 0 },
531     { "RSQ",                    "RSQ",                  313,    313,    2,  2,  V, { VA }, 0 },
532     { "STEYX",                  "STEYX",                314,    314,    2,  2,  V, { VA }, 0 },
533     { "SLOPE",                  "SLOPE",                315,    315,    2,  2,  V, { VA }, 0 },
534     { "TTEST",                  "TTEST",                316,    316,    4,  4,  V, { VA, VA, VR }, 0 },
535     { "PROB",                   "PROB",                 317,    317,    3,  4,  V, { VA, VA, VR }, 0 },
536     { "DEVSQ",                  "DEVSQ",                318,    318,    1,  MX, V, { RX }, 0 },
537     { "GEOMEAN",                "GEOMEAN",              319,    319,    1,  MX, V, { RX }, 0 },
538     { "HARMEAN",                "HARMEAN",              320,    320,    1,  MX, V, { RX }, 0 },
539     { "SUMSQ",                  "SUMSQ",                321,    321,    0,  MX, V, { RX }, 0 },
540     { "KURT",                   "KURT",                 322,    322,    1,  MX, V, { RX }, 0 },
541     { "SKEW",                   "SKEW",                 323,    323,    1,  MX, V, { RX }, 0 },
542     { "ZTEST",                  "ZTEST",                324,    324,    2,  3,  V, { RX, VR }, 0 },
543     { "LARGE",                  "LARGE",                325,    325,    2,  2,  V, { RX, VR }, 0 },
544     { "SMALL",                  "SMALL",                326,    326,    2,  2,  V, { RX, VR }, 0 },
545     { "QUARTILE",               "QUARTILE",             327,    327,    2,  2,  V, { RX, VR }, 0 },
546     { "PERCENTILE",             "PERCENTILE",           328,    328,    2,  2,  V, { RX, VR }, 0 },
547     { "PERCENTRANK",            "PERCENTRANK",          329,    329,    2,  3,  V, { RX, VR, VR_E }, 0 },
548     { "MODE",                   "MODE",                 330,    330,    1,  MX, V, { VA }, 0 },
549     { "TRIMMEAN",               "TRIMMEAN",             331,    331,    2,  2,  V, { RX, VR }, 0 },
550     { "TINV",                   "TINV",                 332,    332,    2,  2,  V, { VR }, 0 },
551 
552     // *** Analysis add-in ***
553 
554     { "HEX2BIN",                "HEX2BIN",              384,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
555     { "HEX2DEC",                "HEX2DEC",              385,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
556     { "HEX2OCT",                "HEX2OCT",              386,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
557     { "DEC2BIN",                "DEC2BIN",              387,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
558     { "DEC2HEX",                "DEC2HEX",              388,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
559     { "DEC2OCT",                "DEC2OCT",              389,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
560     { "OCT2BIN",                "OCT2BIN",              390,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
561     { "OCT2HEX",                "OCT2HEX",              391,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
562     { "OCT2DEC",                "OCT2DEC",              392,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
563     { "BIN2DEC",                "BIN2DEC",              393,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
564     { "BIN2OCT",                "BIN2OCT",              394,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
565     { "BIN2HEX",                "BIN2HEX",              395,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
566     { "IMSUB",                  "IMSUB",                396,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
567     { "IMDIV",                  "IMDIV",                397,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
568     { "IMPOWER",                "IMPOWER",              398,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
569     { "IMABS",                  "IMABS",                399,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
570     { "IMSQRT",                 "IMSQRT",               400,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
571     { "IMLN",                   "IMLN",                 401,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
572     { "IMLOG2",                 "IMLOG2",               402,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
573     { "IMLOG10",                "IMLOG10",              403,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
574     { "IMSIN",                  "IMSIN",                404,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
575     { "IMCOS",                  "IMCOS",                405,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
576     { "IMEXP",                  "IMEXP",                406,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
577     { "IMARGUMENT",             "IMARGUMENT",           407,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
578     { "IMCONJUGATE",            "IMCONJUGATE",          408,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
579     { "IMAGINARY",              "IMAGINARY",            409,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
580     { "IMREAL",                 "IMREAL",               410,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
581     { "COMPLEX",                "COMPLEX",              411,    NOID,   2,  3,  V, { RR }, FUNCFLAG_EXTERNAL },
582     { "IMSUM",                  "IMSUM",                412,    NOID,   1,  MX, V, { RX }, FUNCFLAG_EXTERNAL },
583     { "IMPRODUCT",              "IMPRODUCT",            413,    NOID,   1,  MX, V, { RX }, FUNCFLAG_EXTERNAL },
584     { "SERIESSUM",              "SERIESSUM",            414,    NOID,   4,  4,  V, { RR, RR, RR, RX }, FUNCFLAG_EXTERNAL },
585     { "FACTDOUBLE",             "FACTDOUBLE",           415,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
586     { "SQRTPI",                 "SQRTPI",               416,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
587     { "QUOTIENT",               "QUOTIENT",             417,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
588     { "DELTA",                  "DELTA",                418,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
589     { "GESTEP",                 "GESTEP",               419,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
590     { "ISEVEN",                 "ISEVEN",               420,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },       // Calc: builtin and add-in
591     { "ISODD",                  "ISODD",                421,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },       // Calc: builtin and add-in
592     { "MROUND",                 "MROUND",               422,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
593     { "ERF",                    "ERF",                  423,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
594     { "ERFC",                   "ERFC",                 424,    NOID,   1,  1,  V, { RR }, FUNCFLAG_EXTERNAL },
595     { "BESSELJ",                "BESSELJ",              425,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
596     { "BESSELK",                "BESSELK",              426,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
597     { "BESSELY",                "BESSELY",              427,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
598     { "BESSELI",                "BESSELI",              428,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
599     { "XIRR",                   "XIRR",                 429,    NOID,   2,  3,  V, { RX, RX, RR }, FUNCFLAG_EXTERNAL },
600     { "XNPV",                   "XNPV",                 430,    NOID,   3,  3,  V, { RR, RX, RX }, FUNCFLAG_EXTERNAL },
601     { "PRICEMAT",               "PRICEMAT",             431,    NOID,   5,  6,  V, { RR }, FUNCFLAG_EXTERNAL },
602     { "YIELDMAT",               "YIELDMAT",             432,    NOID,   5,  6,  V, { RR }, FUNCFLAG_EXTERNAL },
603     { "INTRATE",                "INTRATE",              433,    NOID,   4,  5,  V, { RR }, FUNCFLAG_EXTERNAL },
604     { "RECEIVED",               "RECEIVED",             434,    NOID,   4,  5,  V, { RR }, FUNCFLAG_EXTERNAL },
605     { "DISC",                   "DISC",                 435,    NOID,   4,  5,  V, { RR }, FUNCFLAG_EXTERNAL },
606     { "PRICEDISC",              "PRICEDISC",            436,    NOID,   4,  5,  V, { RR }, FUNCFLAG_EXTERNAL },
607     { "YIELDDISC",              "YIELDDISC",            437,    NOID,   4,  5,  V, { RR }, FUNCFLAG_EXTERNAL },
608     { "TBILLEQ",                "TBILLEQ",              438,    NOID,   3,  3,  V, { RR }, FUNCFLAG_EXTERNAL },
609     { "TBILLPRICE",             "TBILLPRICE",           439,    NOID,   3,  3,  V, { RR }, FUNCFLAG_EXTERNAL },
610     { "TBILLYIELD",             "TBILLYIELD",           440,    NOID,   3,  3,  V, { RR }, FUNCFLAG_EXTERNAL },
611     { "PRICE",                  "PRICE",                441,    NOID,   6,  7,  V, { RR }, FUNCFLAG_EXTERNAL },
612     { "YIELD",                  "YIELD",                442,    NOID,   6,  7,  V, { RR }, FUNCFLAG_EXTERNAL },
613     { "DOLLARDE",               "DOLLARDE",             443,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
614     { "DOLLARFR",               "DOLLARFR",             444,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
615     { "NOMINAL",                "NOMINAL",              445,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },       // Calc: builtin and add-in
616     { "EFFECT",                 "EFFECT",               446,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },       // Calc: builtin and add-in
617     { "CUMPRINC",               "CUMPRINC",             447,    NOID,   6,  6,  V, { RR }, FUNCFLAG_EXTERNAL },       // Calc: builtin and add-in
618     { "CUMIPMT",                "CUMIPMT",              448,    NOID,   6,  6,  V, { RR }, FUNCFLAG_EXTERNAL },       // Calc: builtin and add-in
619     { "EDATE",                  "EDATE",                449,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
620     { "EOMONTH",                "EOMONTH",              450,    NOID,   2,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
621     { "YEARFRAC",               "YEARFRAC",             451,    NOID,   2,  3,  V, { RR }, FUNCFLAG_EXTERNAL },
622     { "COUPDAYBS",              "COUPDAYBS",            452,    NOID,   3,  4,  V, { RR }, FUNCFLAG_EXTERNAL },
623     { "COUPDAYS",               "COUPDAYS",             453,    NOID,   3,  4,  V, { RR }, FUNCFLAG_EXTERNAL },
624     { "COUPDAYSNC",             "COUPDAYSNC",           454,    NOID,   3,  4,  V, { RR }, FUNCFLAG_EXTERNAL },
625     { "COUPNCD",                "COUPNCD",              455,    NOID,   3,  4,  V, { RR }, FUNCFLAG_EXTERNAL },
626     { "COUPNUM",                "COUPNUM",              456,    NOID,   3,  4,  V, { RR }, FUNCFLAG_EXTERNAL },
627     { "COUPPCD",                "COUPPCD",              457,    NOID,   3,  4,  V, { RR }, FUNCFLAG_EXTERNAL },
628     { "DURATION",               "DURATION",             458,    NOID,   5,  6,  V, { RR }, FUNCFLAG_EXTERNAL },       // Calc: builtin and add-in
629     { "MDURATION",              "MDURATION",            459,    NOID,   5,  6,  V, { RR }, FUNCFLAG_EXTERNAL },
630     { "ODDLPRICE",              "ODDLPRICE",            460,    NOID,   7,  8,  V, { RR }, FUNCFLAG_EXTERNAL },
631     { "ODDLYIELD",              "ODDLYIELD",            461,    NOID,   8,  9,  V, { RR }, FUNCFLAG_EXTERNAL },
632     { "ODDFPRICE",              "ODDFPRICE",            462,    NOID,   8,  9,  V, { RR }, FUNCFLAG_EXTERNAL },
633     { "ODDFYIELD",              "ODDFYIELD",            463,    NOID,   8,  9,  V, { RR }, FUNCFLAG_EXTERNAL },
634     { "RANDBETWEEN",            "RANDBETWEEN",          464,    NOID,   2,  2,  V, { RR }, FUNCFLAG_VOLATILE | FUNCFLAG_EXTERNAL },
635     { "WEEKNUM",                "WEEKNUM",              465,    NOID,   1,  2,  V, { RR }, FUNCFLAG_EXTERNAL },
636     { "AMORDEGRC",              "AMORDEGRC",            466,    NOID,   6,  7,  V, { RR }, FUNCFLAG_EXTERNAL },
637     { "AMORLINC",               "AMORLINC",             467,    NOID,   6,  7,  V, { RR }, FUNCFLAG_EXTERNAL },
638     { "CONVERT",                "CONVERT",              468,    NOID,   3,  3,  V, { RR }, FUNCFLAG_EXTERNAL },       // Calc: builtin and add-in
639     { "ACCRINT",                "ACCRINT",              469,    NOID,   6,  7,  V, { RR }, FUNCFLAG_EXTERNAL },
640     { "ACCRINTM",               "ACCRINTM",             470,    NOID,   4,  5,  V, { RR }, FUNCFLAG_EXTERNAL },
641     { "WORKDAY",                "WORKDAY",              471,    NOID,   2,  3,  V, { RR, RR, RX, C }, FUNCFLAG_EXTERNAL },
642     { "NETWORKDAYS",            "NETWORKDAYS",          472,    NOID,   2,  3,  V, { RR, RR, RX, C }, FUNCFLAG_EXTERNAL },
643     { "GCD",                    "GCD",                  473,    NOID,   1,  MX, V, { RX }, FUNCFLAG_EXTERNAL },       // Calc: builtin and add-in
644     { "MULTINOMIAL",            "MULTINOMIAL",          474,    NOID,   1,  MX, V, { RX }, FUNCFLAG_EXTERNAL },
645     { "LCM",                    "LCM",                  475,    NOID,   1,  MX, V, { RX }, FUNCFLAG_EXTERNAL },       // Calc: builtin and add-in
646     { "FVSCHEDULE",             "FVSCHEDULE",           476,    NOID,   2,  2,  V, { RR, RX }, FUNCFLAG_EXTERNAL },
647 
648     // *** macro sheet commands ***
649 
650     { 0,                        "ACTIVATE.NEXT",        104,    104,    0,  1,  V, { VR }, FUNCFLAG_MACROCMD },      // BIFF2-3: 0, BIFF4: 0-1
651     { 0,                        "ACTIVATE.PREV",        105,    105,    0,  1,  V, { VR }, FUNCFLAG_MACROCMD }       // BIFF2-3: 0, BIFF4: 0-1
652 };
653 
654 /** Functions new in BIFF5/BIFF7. */
655 static const FunctionData saFuncTableBiff5[] =
656 {
657     { "WEEKDAY",                "WEEKDAY",              70,     70,     1,  2,  V, { VR }, 0 },                              // BIFF2-4: 1,   BIFF5: 1-2
658     { "HLOOKUP",                "HLOOKUP",              101,    101,    3,  4,  V, { VV, RO, RO, VV }, 0 },                     // BIFF2-4: 3,   BIFF5: 3-4
659     { "VLOOKUP",                "VLOOKUP",              102,    102,    3,  4,  V, { VV, RO, RO, VV }, 0 },                     // BIFF2-4: 3,   BIFF5: 3-4
660     { "DAYS360",                "DAYS360",              220,    220,    2,  3,  V, { VR }, 0 },                              // BIFF3-4: 2,   BIFF5: 2-3
661     { 0,                        "EXTERN.CALL",          255,    255,    1,  MX, R, { RO_E, RO }, FUNCFLAG_EXPORTONLY },        // MACRO or EXTERNAL
662     { "CONCATENATE",            "CONCATENATE",          336,    336,    0,  MX, V, { VR }, 0 },
663     { "POWER",                  "POWER",                337,    337,    2,  2,  V, { VR }, 0 },
664     { "RADIANS",                "RADIANS",              342,    342,    1,  1,  V, { VR }, 0 },
665     { "DEGREES",                "DEGREES",              343,    343,    1,  1,  V, { VR }, 0 },
666     { "SUBTOTAL",               "SUBTOTAL",             344,    344,    2,  MX, V, { VR, RO }, 0 },
667     { "SUMIF",                  "SUMIF",                345,    345,    2,  3,  V, { RO, VR, RO }, 0 },
668     { "COUNTIF",                "COUNTIF",              346,    346,    2,  2,  V, { RO, VR }, 0 },
669     { "COUNTBLANK",             "COUNTBLANK",           347,    347,    1,  1,  V, { RO }, 0 },
670     { "ISPMT",                  "ISPMT",                350,    350,    4,  4,  V, { VR }, 0 },
671     { 0,                        "DATEDIF",              351,    351,    3,  3,  V, { VR }, FUNCFLAG_IMPORTONLY },   // not supported in Calc
672     { 0,                        "DATESTRING",           352,    352,    1,  1,  V, { VR }, FUNCFLAG_IMPORTONLY },   // not supported in Calc, missing in OOXML spec
673     { 0,                        "NUMBERSTRING",         353,    353,    2,  2,  V, { VR }, FUNCFLAG_IMPORTONLY },   // not supported in Calc, missing in OOXML spec
674     { "ROMAN",                  "ROMAN",                354,    354,    1,  2,  V, { VR }, 0 },
675 
676     // *** EuroTool add-in ***
677 
678     { "EUROCONVERT",            "EUROCONVERT",          NOID,   NOID,   3,  5,  V, { VR }, FUNCLIB_TO_FUNCFLAGS( FUNCLIB_EUROTOOL ) },
679 
680     // *** macro sheet commands ***
681 
682     { 0,                        "ADD.MENU",             152,    152,    2,  4,  V, { VR, RO, RO, VR }, FUNCFLAG_MACROFUNC },    // BIFF3-4: 2-3, BIFF5: 2-4
683     { 0,                        "ADD.COMMAND",          153,    153,    3,  5,  V, { VR, RO, RO, RO, VR }, FUNCFLAG_MACROFUNC }, // BIFF3-4: 3-4, BIFF5: 3-5
684     { 0,                        "ADD.CHART.AUTOFORMAT", 390,    390,    0,  2,  V, { VR }, FUNCFLAG_MACROCMD },
685     { 0,                        "ADD.LIST.ITEM",        451,    451,    0,  2,  V, { VR }, FUNCFLAG_MACROCMD },
686     { 0,                        "ACTIVE.CELL.FONT",     476,    476,    0,  14, V, { VR }, FUNCFLAG_MACROCMD }
687 };
688 
689 /** Functions new in BIFF8. */
690 static const FunctionData saFuncTableBiff8[] =
691 {
692     { "GETPIVOTDATA",           "GETPIVOTDATA",         358,    358,    2,  MX, V, { RR, RR, VR, VR }, FUNCFLAG_IMPORTONLY | FUNCFLAG_PARAMPAIRS },
693     { "HYPERLINK",              "HYPERLINK",            359,    359,    1,  2,  V, { VV, VO }, 0 },
694     { 0,                        "PHONETIC",             360,    360,    1,  1,  V, { RO }, FUNCFLAG_IMPORTONLY },
695     { "AVERAGEA",               "AVERAGEA",             361,    361,    1,  MX, V, { RX }, 0 },
696     { "MAXA",                   "MAXA",                 362,    362,    1,  MX, V, { RX }, 0 },
697     { "MINA",                   "MINA",                 363,    363,    1,  MX, V, { RX }, 0 },
698     { "STDEVPA",                "STDEVPA",              364,    364,    1,  MX, V, { RX }, 0 },
699     { "VARPA",                  "VARPA",                365,    365,    1,  MX, V, { RX }, 0 },
700     { "STDEVA",                 "STDEVA",               366,    366,    1,  MX, V, { RX }, 0 },
701     { "VARA",                   "VARA",                 367,    367,    1,  MX, V, { RX }, 0 },
702     { "COM.MICROSOFT.BAHTTEXT", "BAHTTEXT",             368,    368,    1,  1,  V, { VR }, FUNCFLAG_MACROCALL },
703     { 0,                        "THAIDAYOFWEEK",        369,    369,    1,  1,  V, { VR }, FUNCFLAG_MACROCALL },
704     { 0,                        "THAIDIGIT",            370,    370,    1,  1,  V, { VR }, FUNCFLAG_MACROCALL },
705     { 0,                        "THAIMONTHOFYEAR",      371,    371,    1,  1,  V, { VR }, FUNCFLAG_MACROCALL },
706     { 0,                        "THAINUMSOUND",         372,    372,    1,  1,  V, { VR }, FUNCFLAG_MACROCALL },
707     { 0,                        "THAINUMSTRING",        373,    373,    1,  1,  V, { VR }, FUNCFLAG_MACROCALL },
708     { 0,                        "THAISTRINGLENGTH",     374,    374,    1,  1,  V, { VR }, FUNCFLAG_MACROCALL },
709     { 0,                        "ISTHAIDIGIT",          375,    375,    1,  1,  V, { VR }, FUNCFLAG_MACROCALL },
710     { 0,                        "ROUNDBAHTDOWN",        376,    376,    1,  1,  V, { VR }, FUNCFLAG_MACROCALL },
711     { 0,                        "ROUNDBAHTUP",          377,    377,    1,  1,  V, { VR }, FUNCFLAG_MACROCALL },
712     { 0,                        "THAIYEAR",             378,    378,    1,  1,  V, { VR }, FUNCFLAG_MACROCALL },
713     { 0,                        "RTD",                  379,    379,    3,  3,  A, { VR, VR, RO }, 0 }
714 };
715 
716 /** Functions new in OOXML. */
717 static const FunctionData saFuncTableOox[] =
718 {
719     { 0,                        "CUBEVALUE",            380,    NOID,   1,  MX, V, { VR, RX }, 0 },
720     { 0,                        "CUBEMEMBER",           381,    NOID,   2,  3,  V, { VR, RX, VR }, 0 },
721     { 0,                        "CUBEMEMBERPROPERTY",   382,    NOID,   3,  3,  V, { VR }, 0 },
722     { 0,                        "CUBERANKEDMEMBER",     383,    NOID,   3,  4,  V, { VR }, 0 },
723     { 0,                        "CUBEKPIMEMBER",        477,    NOID,   3,  4,  V, { VR }, 0 },
724     { 0,                        "CUBESET",              478,    NOID,   2,  5,  V, { VR, RX, VR }, 0 },
725     { 0,                        "CUBESETCOUNT",         479,    NOID,   1,  1,  V, { VR }, 0 },
726     { 0,                        "IFERROR",              480,    NOID,   2,  2,  V, { VO, RO }, 0 },
727     { 0,                        "COUNTIFS",             481,    NOID,   2,  MX, V, { RO, VR }, FUNCFLAG_PARAMPAIRS },
728     { 0,                        "SUMIFS",               482,    NOID,   3,  MX, V, { RO, RO, VR }, FUNCFLAG_PARAMPAIRS },
729     { 0,                        "AVERAGEIF",            483,    NOID,   2,  3,  V, { RO, VR, RO }, 0 },
730     { 0,                        "AVERAGEIFS",           484,    NOID,   3,  MX, V, { RO, RO, VR }, 0 }
731 };
732 
733 /** Functions defined by OpenFormula, but not supported by Calc or by Excel. */
734 static const FunctionData saFuncTableOdf[] =
735 {
736     { "ARABIC",                 0,                      NOID,   NOID,   1,  1,  V, { VR }, FUNCFLAG_MACROCALLODF },
737     { "B",                      0,                      NOID,   NOID,   3,  4,  V, { VR }, FUNCFLAG_MACROCALLODF },
738     { "BASE",                   0,                      NOID,   NOID,   2,  3,  V, { VR }, FUNCFLAG_MACROCALLODF },
739     { "BITAND",                 0,                      NOID,   NOID,   2,  2,  V, { VR }, FUNCFLAG_MACROCALLODF },
740     { "BITLSHIFT",              0,                      NOID,   NOID,   2,  2,  V, { VR }, FUNCFLAG_MACROCALLODF },
741     { "BITOR",                  0,                      NOID,   NOID,   2,  2,  V, { VR }, FUNCFLAG_MACROCALLODF },
742     { "BITRSHIFT",              0,                      NOID,   NOID,   2,  2,  V, { VR }, FUNCFLAG_MACROCALLODF },
743     { "BITXOR",                 0,                      NOID,   NOID,   2,  2,  V, { VR }, FUNCFLAG_MACROCALLODF },
744     { "CHISQDIST",              0,                      NOID,   NOID,   2,  3,  V, { VR }, FUNCFLAG_MACROCALLODF },
745     { "CHISQINV",               0,                      NOID,   NOID,   2,  2,  V, { VR }, FUNCFLAG_MACROCALLODF },
746     { "COMBINA",                0,                      NOID,   NOID,   2,  2,  V, { VR }, FUNCFLAG_MACROCALLODF },
747     { "DAYS",                   0,                      NOID,   NOID,   2,  2,  V, { VR }, FUNCFLAG_MACROCALLODF },
748     { "DECIMAL",                0,                      NOID,   NOID,   2,  2,  V, { VR }, FUNCFLAG_MACROCALLODF },
749     { "FDIST",                  0,                      NOID,   NOID,   3,  4,  V, { VR }, FUNCFLAG_MACROCALLODF },
750     { "FINV",                   0,                      NOID,   NOID,   3,  3,  V, { VR }, FUNCFLAG_MACROCALLODF },
751     { "FORMULA",                0,                      NOID,   NOID,   1,  1,  V, { RO }, FUNCFLAG_MACROCALLODF },
752     { "GAMMA",                  0,                      NOID,   NOID,   1,  1,  V, { VR }, FUNCFLAG_MACROCALLODF },
753     { "GAUSS",                  0,                      NOID,   NOID,   1,  1,  V, { VR }, FUNCFLAG_MACROCALLODF },
754     { "IFNA",                   0,                      NOID,   NOID,   2,  2,  V, { VR, RO }, FUNCFLAG_MACROCALLODF },
755     { "ISFORMULA",              0,                      NOID,   NOID,   1,  1,  V, { RO }, FUNCFLAG_MACROCALLODF },
756     { "ISOWEEKNUM",             0,                      NOID,   NOID,   1,  2,  V, { VR }, FUNCFLAG_MACROCALLODF },
757     { "MUNIT",                  0,                      NOID,   NOID,   1,  1,  A, { VR }, FUNCFLAG_MACROCALLODF },
758     { "NUMBERVALUE",            0,                      NOID,   NOID,   2,  2,  V, { VR }, FUNCFLAG_MACROCALLODF },
759     { "PDURATION",              0,                      NOID,   NOID,   3,  3,  V, { VR }, FUNCFLAG_MACROCALLODF },
760     { "PERMUTATIONA",           0,                      NOID,   NOID,   2,  2,  V, { VR }, FUNCFLAG_MACROCALLODF },
761     { "PHI",                    0,                      NOID,   NOID,   1,  1,  V, { VR }, FUNCFLAG_MACROCALLODF },
762     { "RRI",                    0,                      NOID,   NOID,   3,  3,  V, { VR }, FUNCFLAG_MACROCALLODF },
763     { "SHEET",                  0,                      NOID,   NOID,   0,  1,  V, { RO }, FUNCFLAG_MACROCALLODF },
764     { "SHEETS",                 0,                      NOID,   NOID,   0,  1,  V, { RO }, FUNCFLAG_MACROCALLODF },
765     { "SKEWP",                  0,                      NOID,   NOID,   1,  MX, V, { RX }, FUNCFLAG_MACROCALLODF },
766     { "UNICHAR",                0,                      NOID,   NOID,   1,  1,  V, { VR }, FUNCFLAG_MACROCALLODF },
767     { "UNICODE",                0,                      NOID,   NOID,   1,  1,  V, { VR }, FUNCFLAG_MACROCALLODF },
768     { "XOR",                    0,                      NOID,   NOID,   1,  MX, V, { RX }, FUNCFLAG_MACROCALLODF }
769 };
770 
771 // ----------------------------------------------------------------------------
772 
773 const sal_Unicode API_TOKEN_OPEN            = '(';
774 const sal_Unicode API_TOKEN_CLOSE           = ')';
775 const sal_Unicode API_TOKEN_SEP             = ';';
776 
777 const sal_Unicode API_TOKEN_ARRAY_OPEN      = '{';
778 const sal_Unicode API_TOKEN_ARRAY_CLOSE     = '}';
779 const sal_Unicode API_TOKEN_ARRAY_ROWSEP    = '|';
780 const sal_Unicode API_TOKEN_ARRAY_COLSEP    = ';';
781 
782 } // namespace
783 
784 // function info parameter class iterator =====================================
785 
786 FunctionParamInfoIterator::FunctionParamInfoIterator( const FunctionInfo& rFuncInfo ) :
787     mpParamInfo( rFuncInfo.mpParamInfos ),
788     mpParamInfoEnd( rFuncInfo.mpParamInfos + FUNCINFO_PARAMINFOCOUNT ),
789     mbParamPairs( rFuncInfo.mbParamPairs )
790 {
791     OSL_ENSURE( !mbParamPairs || (mpParamInfo + 1 < mpParamInfoEnd),
792         "FunctionParamInfoIterator::FunctionParamInfoIterator - expecting at least 2 infos for paired parameters" );
793 }
794 
795 const FunctionParamInfo& FunctionParamInfoIterator::getParamInfo() const
796 {
797     static const FunctionParamInfo saInvalidInfo = { FUNC_PARAM_NONE, FUNC_PARAMCONV_ORG, false };
798     return mpParamInfo ? *mpParamInfo : saInvalidInfo;
799 }
800 
801 bool FunctionParamInfoIterator::isCalcOnlyParam() const
802 {
803     return mpParamInfo && (mpParamInfo->meValid == FUNC_PARAM_CALCONLY);
804 }
805 
806 bool FunctionParamInfoIterator::isExcelOnlyParam() const
807 {
808     return mpParamInfo && (mpParamInfo->meValid == FUNC_PARAM_EXCELONLY);
809 }
810 
811 FunctionParamInfoIterator& FunctionParamInfoIterator::operator++()
812 {
813     if( mpParamInfo )
814     {
815         // move pointer to next entry, if something explicit follows
816         if( (mpParamInfo + 1 < mpParamInfoEnd) && (mpParamInfo[ 1 ].meValid != FUNC_PARAM_NONE) )
817             ++mpParamInfo;
818         // points to last info, but parameter pairs expected, move to previous info
819         else if( mbParamPairs )
820             --mpParamInfo;
821         // if last parameter type is 'Excel-only' or 'Calc-only', do not repeat it
822         else if( isExcelOnlyParam() || isCalcOnlyParam() )
823             mpParamInfo = 0;
824         // otherwise: repeat last parameter class
825     }
826     return *this;
827 }
828 
829 // function provider ==========================================================
830 
831 struct FunctionProviderImpl
832 {
833     typedef RefMap< OUString, FunctionInfo >    FuncNameMap;
834     typedef RefMap< sal_uInt16, FunctionInfo >  FuncIdMap;
835 
836     FunctionInfoVector  maFuncs;            /// All function infos in one list.
837     FuncNameMap         maOdfFuncs;         /// Maps ODF function names to function data.
838     FuncNameMap         maOoxFuncs;         /// Maps OOXML function names to function data.
839     FuncIdMap           maBiff12Funcs;      /// Maps BIFF12 function indexes to function data.
840     FuncIdMap           maBiffFuncs;        /// Maps BIFF2-BIFF8 function indexes to function data.
841     FuncNameMap         maMacroFuncs;       /// Maps macro function names to function data.
842 
843     explicit            FunctionProviderImpl( FilterType eFilter, BiffType eBiff, bool bImportFilter );
844 
845 private:
846     /** Creates and inserts a function info struct from the passed function data. */
847     void                initFunc( const FunctionData& rFuncData, sal_uInt8 nMaxParam );
848 
849     /** Initializes the members from the passed function data list. */
850     void                initFuncs(
851                             const FunctionData* pBeg, const FunctionData* pEnd,
852                             sal_uInt8 nMaxParam, bool bImportFilter );
853 };
854 
855 // ----------------------------------------------------------------------------
856 
857 FunctionProviderImpl::FunctionProviderImpl( FilterType eFilter, BiffType eBiff, bool bImportFilter )
858 {
859     OSL_ENSURE( bImportFilter, "FunctionProviderImpl::FunctionProviderImpl - need special handling for macro call functions" );
860     sal_uInt8 nMaxParam = 0;
861     switch( eFilter )
862     {
863         case FILTER_OOXML:
864             nMaxParam = OOX_MAX_PARAMCOUNT;
865             eBiff = BIFF8;  // insert all BIFF function tables, then the OOXML table
866         break;
867         case FILTER_BIFF:
868             nMaxParam = BIFF_MAX_PARAMCOUNT;
869         break;
870         case FILTER_UNKNOWN:
871             OSL_ENSURE( false, "FunctionProviderImpl::FunctionProviderImpl - invalid filter type" );
872         break;
873     }
874     OSL_ENSURE( eBiff != BIFF_UNKNOWN, "FunctionProviderImpl::FunctionProviderImpl - invalid BIFF type" );
875 
876     /*  Add functions supported in the current BIFF version only. Function
877         tables from later BIFF versions may overwrite single functions from
878         earlier tables. */
879     if( eBiff >= BIFF2 )
880         initFuncs( saFuncTableBiff2, STATIC_ARRAY_END( saFuncTableBiff2 ), nMaxParam, bImportFilter );
881     if( eBiff >= BIFF3 )
882         initFuncs( saFuncTableBiff3, STATIC_ARRAY_END( saFuncTableBiff3 ), nMaxParam, bImportFilter );
883     if( eBiff >= BIFF4 )
884         initFuncs( saFuncTableBiff4, STATIC_ARRAY_END( saFuncTableBiff4 ), nMaxParam, bImportFilter );
885     if( eBiff >= BIFF5 )
886         initFuncs( saFuncTableBiff5, STATIC_ARRAY_END( saFuncTableBiff5 ), nMaxParam, bImportFilter );
887     if( eBiff >= BIFF8 )
888         initFuncs( saFuncTableBiff8, STATIC_ARRAY_END( saFuncTableBiff8 ), nMaxParam, bImportFilter );
889     if( eFilter == FILTER_OOXML )
890         initFuncs( saFuncTableOox, STATIC_ARRAY_END( saFuncTableOox ), nMaxParam, bImportFilter );
891     initFuncs( saFuncTableOdf, STATIC_ARRAY_END( saFuncTableOdf ), nMaxParam, bImportFilter );
892 }
893 
894 void FunctionProviderImpl::initFunc( const FunctionData& rFuncData, sal_uInt8 nMaxParam )
895 {
896     // create a function info object
897     FunctionInfoRef xFuncInfo( new FunctionInfo );
898     if( rFuncData.mpcOdfFuncName )
899         xFuncInfo->maOdfFuncName = OUString::createFromAscii( rFuncData.mpcOdfFuncName );
900     if( rFuncData.mpcOoxFuncName )
901         xFuncInfo->maOoxFuncName = OUString::createFromAscii( rFuncData.mpcOoxFuncName );
902 
903     if( getFlag( rFuncData.mnFlags, FUNCFLAG_MACROCALL ) )
904     {
905         OSL_ENSURE( xFuncInfo->maOoxFuncName.getLength() > 0, "FunctionProviderImpl::initFunc - missing OOXML function name" );
906         OSL_ENSURE( !getFlag( rFuncData.mnFlags, FUNCFLAG_MACROCALLODF ), "FunctionProviderImpl::initFunc - unexpected flag FUNCFLAG_MACROCALLODF" );
907         xFuncInfo->maBiffMacroName = CREATE_OUSTRING( "_xlfn." ) + xFuncInfo->maOoxFuncName;
908     }
909     else if( getFlag( rFuncData.mnFlags, FUNCFLAG_MACROCALLODF ) )
910     {
911         OSL_ENSURE( xFuncInfo->maOdfFuncName.getLength() > 0, "FunctionProviderImpl::initFunc - missing ODF function name" );
912         xFuncInfo->maBiffMacroName = CREATE_OUSTRING( "_xlfnodf." ) + xFuncInfo->maOdfFuncName;
913     }
914 
915     xFuncInfo->meFuncLibType = FUNCFLAGS_TO_FUNCLIB( rFuncData.mnFlags );
916     xFuncInfo->mnApiOpCode = -1;
917     xFuncInfo->mnBiff12FuncId = rFuncData.mnBiff12FuncId;
918     xFuncInfo->mnBiffFuncId = rFuncData.mnBiffFuncId;
919     xFuncInfo->mnMinParamCount = rFuncData.mnMinParamCount;
920     xFuncInfo->mnMaxParamCount = (rFuncData.mnMaxParamCount == MX) ? nMaxParam : rFuncData.mnMaxParamCount;
921     xFuncInfo->mnRetClass = rFuncData.mnRetClass;
922     xFuncInfo->mpParamInfos = rFuncData.mpParamInfos;
923     xFuncInfo->mbParamPairs = getFlag( rFuncData.mnFlags, FUNCFLAG_PARAMPAIRS );
924     xFuncInfo->mbVolatile = getFlag( rFuncData.mnFlags, FUNCFLAG_VOLATILE );
925     xFuncInfo->mbExternal = getFlag( rFuncData.mnFlags, FUNCFLAG_EXTERNAL );
926     bool bMacroCmd = getFlag( rFuncData.mnFlags, FUNCFLAG_MACROCMD );
927     xFuncInfo->mbMacroFunc = bMacroCmd || getFlag( rFuncData.mnFlags, FUNCFLAG_MACROFUNC );
928     xFuncInfo->mbVarParam = bMacroCmd || (rFuncData.mnMinParamCount != rFuncData.mnMaxParamCount) || getFlag( rFuncData.mnFlags, FUNCFLAG_ALWAYSVAR );
929 
930     setFlag( xFuncInfo->mnBiff12FuncId, BIFF_TOK_FUNCVAR_CMD, bMacroCmd );
931     setFlag( xFuncInfo->mnBiffFuncId, BIFF_TOK_FUNCVAR_CMD, bMacroCmd );
932 
933     // insert the function info into the member maps
934     maFuncs.push_back( xFuncInfo );
935     if( xFuncInfo->maOdfFuncName.getLength() > 0 )
936         maOdfFuncs[ xFuncInfo->maOdfFuncName ] = xFuncInfo;
937     if( xFuncInfo->maOoxFuncName.getLength() > 0 )
938         maOoxFuncs[ xFuncInfo->maOoxFuncName ] = xFuncInfo;
939     if( xFuncInfo->mnBiff12FuncId != NOID )
940         maBiff12Funcs[ xFuncInfo->mnBiff12FuncId ] = xFuncInfo;
941     if( xFuncInfo->mnBiffFuncId != NOID )
942         maBiffFuncs[ xFuncInfo->mnBiffFuncId ] = xFuncInfo;
943     if( xFuncInfo->maBiffMacroName.getLength() > 0 )
944         maMacroFuncs[ xFuncInfo->maBiffMacroName ] = xFuncInfo;
945 }
946 
947 void FunctionProviderImpl::initFuncs( const FunctionData* pBeg, const FunctionData* pEnd, sal_uInt8 nMaxParam, bool bImportFilter )
948 {
949     for( const FunctionData* pIt = pBeg; pIt != pEnd; ++pIt )
950         if( pIt->isSupported( bImportFilter ) )
951             initFunc( *pIt, nMaxParam );
952 }
953 
954 // ----------------------------------------------------------------------------
955 
956 FunctionProvider::FunctionProvider( FilterType eFilter, BiffType eBiff, bool bImportFilter ) :
957     mxFuncImpl( new FunctionProviderImpl( eFilter, eBiff, bImportFilter ) )
958 {
959 }
960 
961 FunctionProvider::~FunctionProvider()
962 {
963 }
964 
965 const FunctionInfo* FunctionProvider::getFuncInfoFromOdfFuncName( const OUString& rFuncName ) const
966 {
967     return mxFuncImpl->maOdfFuncs.get( rFuncName ).get();
968 }
969 
970 const FunctionInfo* FunctionProvider::getFuncInfoFromOoxFuncName( const OUString& rFuncName ) const
971 {
972     return mxFuncImpl->maOoxFuncs.get( rFuncName ).get();
973 }
974 
975 const FunctionInfo* FunctionProvider::getFuncInfoFromBiff12FuncId( sal_uInt16 nFuncId ) const
976 {
977     return mxFuncImpl->maBiff12Funcs.get( nFuncId ).get();
978 }
979 
980 const FunctionInfo* FunctionProvider::getFuncInfoFromBiffFuncId( sal_uInt16 nFuncId ) const
981 {
982     return mxFuncImpl->maBiffFuncs.get( nFuncId ).get();
983 }
984 
985 const FunctionInfo* FunctionProvider::getFuncInfoFromMacroName( const OUString& rFuncName ) const
986 {
987     return mxFuncImpl->maMacroFuncs.get( rFuncName ).get();
988 }
989 
990 FunctionLibraryType FunctionProvider::getFuncLibTypeFromLibraryName( const OUString& rLibraryName ) const
991 {
992 #define OOX_XLS_IS_LIBNAME( libname, basename ) (libname.equalsIgnoreAsciiCaseAscii( basename ".XLA" ) || libname.equalsIgnoreAsciiCaseAscii( basename ".XLAM" ))
993 
994     // the EUROTOOL add-in containing the EUROCONVERT function
995     if( OOX_XLS_IS_LIBNAME( rLibraryName, "EUROTOOL" ) )
996         return FUNCLIB_EUROTOOL;
997 
998 #undef OOX_XLS_IS_LIBNAME
999 
1000     // default: unknown library
1001     return FUNCLIB_UNKNOWN;
1002 }
1003 
1004 const FunctionInfoVector& FunctionProvider::getFuncs() const
1005 {
1006     return mxFuncImpl->maFuncs;
1007 }
1008 
1009 // op-code and function provider ==============================================
1010 
1011 struct OpCodeProviderImpl : public ApiOpCodes
1012 {
1013     typedef RefMap< sal_Int32, FunctionInfo >       OpCodeFuncMap;
1014     typedef RefMap< OUString, FunctionInfo >        FuncNameMap;
1015     typedef ::std::vector< FormulaOpCodeMapEntry >  OpCodeEntryVector;
1016 
1017     OpCodeFuncMap       maOpCodeFuncs;      /// Maps API function op-codes to function data.
1018     FuncNameMap         maExtProgFuncs;     /// Maps programmatical API function names to function data.
1019     OpCodeEntryVector   maParserMap;        /// OOXML token mapping for formula parser service.
1020 
1021     explicit            OpCodeProviderImpl(
1022                             const FunctionInfoVector& rFuncInfos,
1023                             const Reference< XMultiServiceFactory >& rxModelFactory );
1024 
1025 private:
1026     typedef ::std::map< OUString, ApiToken >    ApiTokenMap;
1027     typedef Sequence< FormulaOpCodeMapEntry >   OpCodeEntrySequence;
1028 
1029     static bool         fillEntrySeq( OpCodeEntrySequence& orEntrySeq, const Reference< XFormulaOpCodeMapper >& rxMapper, sal_Int32 nMapGroup );
1030     static bool         fillTokenMap( ApiTokenMap& orTokenMap, OpCodeEntrySequence& orEntrySeq, const Reference< XFormulaOpCodeMapper >& rxMapper, sal_Int32 nMapGroup );
1031     bool                fillFuncTokenMaps( ApiTokenMap& orIntFuncTokenMap, ApiTokenMap& orExtFuncTokenMap, OpCodeEntrySequence& orEntrySeq, const Reference< XFormulaOpCodeMapper >& rxMapper ) const;
1032 
1033     static bool         initOpCode( sal_Int32& ornOpCode, const OpCodeEntrySequence& rEntrySeq, sal_Int32 nSpecialId );
1034     bool                initOpCode( sal_Int32& ornOpCode, const ApiTokenMap& rTokenMap, const OUString& rOdfName, const OUString& rOoxName );
1035     bool                initOpCode( sal_Int32& ornOpCode, const ApiTokenMap& rTokenMap, const sal_Char* pcOdfName, const sal_Char* pcOoxName );
1036     bool                initOpCode( sal_Int32& ornOpCode, const ApiTokenMap& rTokenMap, sal_Unicode cOdfName, sal_Unicode cOoxName );
1037 
1038     bool                initFuncOpCode( FunctionInfo& orFuncInfo, const ApiTokenMap& rFuncTokenMap );
1039     bool                initFuncOpCodes( const ApiTokenMap& rIntFuncTokenMap, const ApiTokenMap& rExtFuncTokenMap, const FunctionInfoVector& rFuncInfos );
1040 };
1041 
1042 // ----------------------------------------------------------------------------
1043 
1044 OpCodeProviderImpl::OpCodeProviderImpl( const FunctionInfoVector& rFuncInfos,
1045         const Reference< XMultiServiceFactory >& rxModelFactory )
1046 {
1047     if( rxModelFactory.is() ) try
1048     {
1049         Reference< XFormulaOpCodeMapper > xMapper( rxModelFactory->createInstance(
1050             CREATE_OUSTRING( "com.sun.star.sheet.FormulaOpCodeMapper" ) ), UNO_QUERY_THROW );
1051 
1052         // op-codes provided as attributes
1053         OPCODE_UNKNOWN = xMapper->getOpCodeUnknown();
1054         OPCODE_EXTERNAL = xMapper->getOpCodeExternal();
1055 
1056         using namespace ::com::sun::star::sheet::FormulaMapGroup;
1057         using namespace ::com::sun::star::sheet::FormulaMapGroupSpecialOffset;
1058 
1059         OpCodeEntrySequence aEntrySeq;
1060         ApiTokenMap aTokenMap, aExtFuncTokenMap;
1061         bool bIsValid =
1062             // special
1063             fillEntrySeq( aEntrySeq, xMapper, SPECIAL ) &&
1064             initOpCode( OPCODE_PUSH,          aEntrySeq, PUSH ) &&
1065             initOpCode( OPCODE_MISSING,       aEntrySeq, MISSING ) &&
1066             initOpCode( OPCODE_SPACES,        aEntrySeq, SPACES ) &&
1067             initOpCode( OPCODE_NAME,          aEntrySeq, NAME ) &&
1068             initOpCode( OPCODE_DBAREA,        aEntrySeq, DB_AREA ) &&
1069             initOpCode( OPCODE_NLR,           aEntrySeq, COL_ROW_NAME ) &&
1070             initOpCode( OPCODE_MACRO,         aEntrySeq, MACRO ) &&
1071             initOpCode( OPCODE_BAD,           aEntrySeq, BAD ) &&
1072             initOpCode( OPCODE_NONAME,        aEntrySeq, NO_NAME ) &&
1073             // separators
1074             fillTokenMap( aTokenMap, aEntrySeq, xMapper, SEPARATORS ) &&
1075             initOpCode( OPCODE_OPEN,          aTokenMap, API_TOKEN_OPEN,  '('  ) &&
1076             initOpCode( OPCODE_CLOSE,         aTokenMap, API_TOKEN_CLOSE, ')'  ) &&
1077             initOpCode( OPCODE_SEP,           aTokenMap, API_TOKEN_SEP,   ','  ) &&
1078             // array separators
1079             fillTokenMap( aTokenMap, aEntrySeq, xMapper, ARRAY_SEPARATORS ) &&
1080             initOpCode( OPCODE_ARRAY_OPEN,    aTokenMap, API_TOKEN_ARRAY_OPEN,   '{'  ) &&
1081             initOpCode( OPCODE_ARRAY_CLOSE,   aTokenMap, API_TOKEN_ARRAY_CLOSE,  '}'  ) &&
1082             initOpCode( OPCODE_ARRAY_ROWSEP,  aTokenMap, API_TOKEN_ARRAY_ROWSEP, ';'  ) &&
1083             initOpCode( OPCODE_ARRAY_COLSEP,  aTokenMap, API_TOKEN_ARRAY_COLSEP, ','  ) &&
1084             // unary operators
1085             fillTokenMap( aTokenMap, aEntrySeq, xMapper, UNARY_OPERATORS ) &&
1086             initOpCode( OPCODE_PLUS_SIGN,     aTokenMap, '+',  '\0' ) && // same op-code as OPCODE_ADD
1087             initOpCode( OPCODE_MINUS_SIGN,    aTokenMap, '-',  '-'  ) &&
1088             initOpCode( OPCODE_PERCENT,       aTokenMap, '%',  '%'  ) &&
1089             // binary operators
1090             fillTokenMap( aTokenMap, aEntrySeq, xMapper, BINARY_OPERATORS ) &&
1091             initOpCode( OPCODE_ADD,           aTokenMap, '+',  '+'  ) &&
1092             initOpCode( OPCODE_SUB,           aTokenMap, '-',  '-'  ) &&
1093             initOpCode( OPCODE_MULT,          aTokenMap, '*',  '*'  ) &&
1094             initOpCode( OPCODE_DIV,           aTokenMap, '/',  '/'  ) &&
1095             initOpCode( OPCODE_POWER,         aTokenMap, '^',  '^'  ) &&
1096             initOpCode( OPCODE_CONCAT,        aTokenMap, '&',  '&'  ) &&
1097             initOpCode( OPCODE_EQUAL,         aTokenMap, '=',  '='  ) &&
1098             initOpCode( OPCODE_NOT_EQUAL,     aTokenMap, "<>", "<>" ) &&
1099             initOpCode( OPCODE_LESS,          aTokenMap, '<',  '<'  ) &&
1100             initOpCode( OPCODE_LESS_EQUAL,    aTokenMap, "<=", "<=" ) &&
1101             initOpCode( OPCODE_GREATER,       aTokenMap, '>',  '>'  ) &&
1102             initOpCode( OPCODE_GREATER_EQUAL, aTokenMap, ">=", ">=" ) &&
1103             initOpCode( OPCODE_INTERSECT,     aTokenMap, '!',  ' '  ) &&
1104             initOpCode( OPCODE_LIST,          aTokenMap, '~',  ','  ) &&
1105             initOpCode( OPCODE_RANGE,         aTokenMap, ':',  ':'  ) &&
1106             // functions
1107             fillFuncTokenMaps( aTokenMap, aExtFuncTokenMap, aEntrySeq, xMapper ) &&
1108             initFuncOpCodes( aTokenMap, aExtFuncTokenMap, rFuncInfos ) &&
1109             initOpCode( OPCODE_DDE,           aTokenMap, "DDE", 0 );
1110 
1111         OSL_ENSURE( bIsValid, "OpCodeProviderImpl::OpCodeProviderImpl - opcodes not initialized" );
1112         (void)bIsValid;
1113 
1114         // OPCODE_PLUS_SIGN and OPCODE_ADD should be equal, otherwise "+" has to be passed above
1115         OSL_ENSURE( OPCODE_PLUS_SIGN == OPCODE_ADD, "OpCodeProviderImpl::OpCodeProviderImpl - need opcode mapping for OPCODE_PLUS_SIGN" );
1116     }
1117     catch( Exception& )
1118     {
1119         OSL_ENSURE( false, "OpCodeProviderImpl::OpCodeProviderImpl - cannot receive formula opcode mapper" );
1120     }
1121 }
1122 
1123 bool OpCodeProviderImpl::fillEntrySeq( OpCodeEntrySequence& orEntrySeq,
1124         const Reference< XFormulaOpCodeMapper >& rxMapper, sal_Int32 nMapGroup )
1125 {
1126     try
1127     {
1128         orEntrySeq = rxMapper->getAvailableMappings( ::com::sun::star::sheet::FormulaLanguage::ODFF, nMapGroup );
1129         return orEntrySeq.hasElements();
1130     }
1131     catch( Exception& )
1132     {
1133     }
1134     return false;
1135 }
1136 
1137 bool OpCodeProviderImpl::fillTokenMap( ApiTokenMap& orTokenMap, OpCodeEntrySequence& orEntrySeq,
1138         const Reference< XFormulaOpCodeMapper >& rxMapper, sal_Int32 nMapGroup )
1139 {
1140     orTokenMap.clear();
1141     if( fillEntrySeq( orEntrySeq, rxMapper, nMapGroup ) )
1142     {
1143         const FormulaOpCodeMapEntry* pEntry = orEntrySeq.getConstArray();
1144         const FormulaOpCodeMapEntry* pEntryEnd = pEntry + orEntrySeq.getLength();
1145         for( ; pEntry != pEntryEnd; ++pEntry )
1146             orTokenMap[ pEntry->Name ] = pEntry->Token;
1147     }
1148     return orEntrySeq.hasElements();
1149 }
1150 
1151 bool OpCodeProviderImpl::fillFuncTokenMaps( ApiTokenMap& orIntFuncTokenMap, ApiTokenMap& orExtFuncTokenMap, OpCodeEntrySequence& orEntrySeq, const Reference< XFormulaOpCodeMapper >& rxMapper ) const
1152 {
1153     orIntFuncTokenMap.clear();
1154     orExtFuncTokenMap.clear();
1155     if( fillEntrySeq( orEntrySeq, rxMapper, ::com::sun::star::sheet::FormulaMapGroup::FUNCTIONS ) )
1156     {
1157         const FormulaOpCodeMapEntry* pEntry = orEntrySeq.getConstArray();
1158         const FormulaOpCodeMapEntry* pEntryEnd = pEntry + orEntrySeq.getLength();
1159         for( ; pEntry != pEntryEnd; ++pEntry )
1160             ((pEntry->Token.OpCode == OPCODE_EXTERNAL) ? orExtFuncTokenMap : orIntFuncTokenMap)[ pEntry->Name ] = pEntry->Token;
1161     }
1162     return orEntrySeq.hasElements();
1163 }
1164 
1165 bool OpCodeProviderImpl::initOpCode( sal_Int32& ornOpCode, const OpCodeEntrySequence& rEntrySeq, sal_Int32 nSpecialId )
1166 {
1167     if( (0 <= nSpecialId) && (nSpecialId < rEntrySeq.getLength()) )
1168     {
1169         ornOpCode = rEntrySeq[ nSpecialId ].Token.OpCode;
1170         return true;
1171     }
1172     OSL_ENSURE( false,
1173         OStringBuffer( "OpCodeProviderImpl::initOpCode - opcode for special offset " ).
1174         append( nSpecialId ).append( " not found" ).getStr() );
1175     return false;
1176 }
1177 
1178 bool OpCodeProviderImpl::initOpCode( sal_Int32& ornOpCode, const ApiTokenMap& rTokenMap, const OUString& rOdfName, const OUString& rOoxName )
1179 {
1180     ApiTokenMap::const_iterator aIt = rTokenMap.find( rOdfName );
1181     if( aIt != rTokenMap.end() )
1182     {
1183         ornOpCode = aIt->second.OpCode;
1184         if( rOoxName.getLength() > 0 )
1185         {
1186             FormulaOpCodeMapEntry aEntry;
1187             aEntry.Name = rOoxName;
1188             aEntry.Token.OpCode = ornOpCode;
1189             maParserMap.push_back( aEntry );
1190         }
1191         return true;
1192     }
1193     OSL_ENSURE( false,
1194         OStringBuffer( "OpCodeProviderImpl::initOpCode - opcode for \"" ).
1195         append( OUStringToOString( rOdfName, RTL_TEXTENCODING_ASCII_US ) ).
1196         append( "\" not found" ).getStr() );
1197     return false;
1198 }
1199 
1200 bool OpCodeProviderImpl::initOpCode( sal_Int32& ornOpCode, const ApiTokenMap& rTokenMap, const sal_Char* pcOdfName, const sal_Char* pcOoxName )
1201 {
1202     OUString aOoxName;
1203     if( pcOoxName ) aOoxName = OUString::createFromAscii( pcOoxName );
1204     return initOpCode( ornOpCode, rTokenMap, OUString::createFromAscii( pcOdfName ), aOoxName );
1205 }
1206 
1207 bool OpCodeProviderImpl::initOpCode( sal_Int32& ornOpCode, const ApiTokenMap& rTokenMap, sal_Unicode cOdfName, sal_Unicode cOoxName )
1208 {
1209     OUString aOoxName;
1210     if( cOoxName ) aOoxName = OUString( cOoxName );
1211     return initOpCode( ornOpCode, rTokenMap, OUString( cOdfName ), aOoxName );
1212 }
1213 
1214 bool OpCodeProviderImpl::initFuncOpCode( FunctionInfo& orFuncInfo, const ApiTokenMap& rFuncTokenMap )
1215 {
1216     bool bIsValid = false;
1217     if( orFuncInfo.maOdfFuncName.getLength() > 0 )
1218     {
1219         ApiTokenMap::const_iterator aIt = rFuncTokenMap.find( orFuncInfo.maOdfFuncName );
1220         if( aIt != rFuncTokenMap.end() )
1221         {
1222             orFuncInfo.mnApiOpCode = aIt->second.OpCode;
1223             bIsValid =
1224                 (orFuncInfo.mnApiOpCode >= 0) &&
1225                 (orFuncInfo.mnApiOpCode != OPCODE_UNKNOWN) &&
1226                 (orFuncInfo.mnApiOpCode != OPCODE_NONAME);
1227             OSL_ENSURE( bIsValid,
1228                 OStringBuffer( "OpCodeProviderImpl::initFuncOpCode - no valid opcode for ODF function \"" ).
1229                 append( OUStringToOString( orFuncInfo.maOdfFuncName, RTL_TEXTENCODING_ASCII_US ) ).
1230                 append( '"' ).getStr() );
1231 
1232             if( bIsValid && (orFuncInfo.mnApiOpCode == OPCODE_EXTERNAL) )
1233             {
1234                 bIsValid = (aIt->second.Data >>= orFuncInfo.maExtProgName) && (orFuncInfo.maExtProgName.getLength() > 0);
1235                 OSL_ENSURE( bIsValid,
1236                     OStringBuffer( "OpCodeProviderImpl::initFuncOpCode - no programmatical name for external function \"" ).
1237                     append( OUStringToOString( orFuncInfo.maOdfFuncName, RTL_TEXTENCODING_ASCII_US ) ).
1238                     append( '"' ).getStr() );
1239             }
1240 
1241             // add to parser map, if OOXML function name exists
1242             if( bIsValid && (orFuncInfo.maOoxFuncName.getLength() > 0) )
1243             {
1244                 // create the parser map entry
1245                 FormulaOpCodeMapEntry aEntry;
1246                 aEntry.Name = orFuncInfo.maOoxFuncName;
1247                 aEntry.Token = aIt->second;
1248                 maParserMap.push_back( aEntry );
1249             }
1250         }
1251         else
1252         {
1253             // ignore entries for functions unknown by Calc *and* by Excel
1254             bIsValid = orFuncInfo.maOoxFuncName.getLength() == 0;
1255         }
1256     }
1257     else if( orFuncInfo.mnBiffFuncId == BIFF_FUNC_EXTERNCALL )
1258     {
1259         orFuncInfo.mnApiOpCode = OPCODE_EXTERNAL;
1260         bIsValid = true;
1261     }
1262     else if( orFuncInfo.maOoxFuncName.getLength() > 0 )
1263     {
1264         orFuncInfo.mnApiOpCode = OPCODE_BAD;
1265         bIsValid = true;
1266     }
1267 
1268     if( !bIsValid || (orFuncInfo.mnApiOpCode == OPCODE_UNKNOWN) || (orFuncInfo.mnApiOpCode < 0) )
1269         orFuncInfo.mnApiOpCode = OPCODE_NONAME;
1270     return bIsValid;
1271 }
1272 
1273 bool OpCodeProviderImpl::initFuncOpCodes( const ApiTokenMap& rIntFuncTokenMap, const ApiTokenMap& rExtFuncTokenMap, const FunctionInfoVector& rFuncInfos )
1274 {
1275     bool bIsValid = true;
1276     for( FunctionInfoVector::const_iterator aIt = rFuncInfos.begin(), aEnd = rFuncInfos.end(); aIt != aEnd; ++aIt )
1277     {
1278         FunctionInfoRef xFuncInfo = *aIt;
1279         // set API opcode from ODF function name
1280         bIsValid &= initFuncOpCode( *xFuncInfo, xFuncInfo->mbExternal ? rExtFuncTokenMap : rIntFuncTokenMap );
1281         // insert the function info into the maps
1282         if( (xFuncInfo->mnApiOpCode != OPCODE_NONAME) && (xFuncInfo->mnApiOpCode != OPCODE_BAD) )
1283         {
1284             if( (xFuncInfo->mnApiOpCode == OPCODE_EXTERNAL) && (xFuncInfo->maExtProgName.getLength() > 0) )
1285                 maExtProgFuncs[ xFuncInfo->maExtProgName ] = xFuncInfo;
1286             else
1287                 maOpCodeFuncs[ xFuncInfo->mnApiOpCode ] = xFuncInfo;
1288         }
1289     }
1290     return bIsValid;
1291 }
1292 
1293 // ----------------------------------------------------------------------------
1294 
1295 OpCodeProvider::OpCodeProvider( const Reference< XMultiServiceFactory >& rxModelFactory,
1296         FilterType eFilter, BiffType eBiff, bool bImportFilter ) :
1297     FunctionProvider( eFilter, eBiff, bImportFilter ),
1298     mxOpCodeImpl( new OpCodeProviderImpl( getFuncs(), rxModelFactory ) )
1299 {
1300 }
1301 
1302 OpCodeProvider::~OpCodeProvider()
1303 {
1304 }
1305 
1306 const ApiOpCodes& OpCodeProvider::getOpCodes() const
1307 {
1308     return *mxOpCodeImpl;
1309 }
1310 
1311 const FunctionInfo* OpCodeProvider::getFuncInfoFromApiToken( const ApiToken& rToken ) const
1312 {
1313     const FunctionInfo* pFuncInfo = 0;
1314     if( (rToken.OpCode == mxOpCodeImpl->OPCODE_EXTERNAL) && rToken.Data.has< OUString >() )
1315         pFuncInfo = mxOpCodeImpl->maExtProgFuncs.get( rToken.Data.get< OUString >() ).get();
1316     else if( (rToken.OpCode == mxOpCodeImpl->OPCODE_MACRO) && rToken.Data.has< OUString >() )
1317         pFuncInfo = getFuncInfoFromMacroName( rToken.Data.get< OUString >() );
1318     else if( (rToken.OpCode == mxOpCodeImpl->OPCODE_BAD) && rToken.Data.has< OUString >() )
1319         pFuncInfo = getFuncInfoFromOoxFuncName( rToken.Data.get< OUString >() );
1320     else
1321         pFuncInfo = mxOpCodeImpl->maOpCodeFuncs.get( rToken.OpCode ).get();
1322     return pFuncInfo;
1323 }
1324 
1325 Sequence< FormulaOpCodeMapEntry > OpCodeProvider::getOoxParserMap() const
1326 {
1327     return ContainerHelper::vectorToSequence( mxOpCodeImpl->maParserMap );
1328 }
1329 
1330 // API formula parser wrapper =================================================
1331 
1332 ApiParserWrapper::ApiParserWrapper(
1333         const Reference< XMultiServiceFactory >& rxModelFactory, const OpCodeProvider& rOpCodeProv ) :
1334     OpCodeProvider( rOpCodeProv )
1335 {
1336     if( rxModelFactory.is() ) try
1337     {
1338         mxParser.set( rxModelFactory->createInstance( CREATE_OUSTRING( "com.sun.star.sheet.FormulaParser" ) ), UNO_QUERY_THROW );
1339     }
1340     catch( Exception& )
1341     {
1342     }
1343     OSL_ENSURE( mxParser.is(), "ApiParserWrapper::ApiParserWrapper - cannot create API formula parser object" );
1344     maParserProps.set( mxParser );
1345     maParserProps.setProperty( PROP_CompileEnglish, true );
1346     maParserProps.setProperty( PROP_FormulaConvention, ::com::sun::star::sheet::AddressConvention::XL_OOX );
1347     maParserProps.setProperty( PROP_IgnoreLeadingSpaces, false );
1348     maParserProps.setProperty( PROP_OpCodeMap, getOoxParserMap() );
1349 }
1350 
1351 ApiTokenSequence ApiParserWrapper::parseFormula( const OUString& rFormula, const CellAddress& rRefPos )
1352 {
1353     ApiTokenSequence aTokenSeq;
1354     if( mxParser.is() ) try
1355     {
1356         aTokenSeq = mxParser->parseFormula( rFormula, rRefPos );
1357     }
1358     catch( Exception& )
1359     {
1360     }
1361     return aTokenSeq;
1362 }
1363 
1364 // formula parser/printer base class for filters ==============================
1365 
1366 namespace {
1367 
1368 bool lclConvertToCellAddress( CellAddress& orAddress, const SingleReference& rSingleRef, sal_Int32 nForbiddenFlags, sal_Int32 nFilterBySheet )
1369 {
1370     orAddress = CellAddress( static_cast< sal_Int16 >( rSingleRef.Sheet ),
1371         rSingleRef.Column, rSingleRef.Row );
1372     return
1373         !getFlag( rSingleRef.Flags, nForbiddenFlags ) &&
1374         ((nFilterBySheet < 0) || (nFilterBySheet == rSingleRef.Sheet));
1375 }
1376 
1377 bool lclConvertToCellRange( CellRangeAddress& orRange, const ComplexReference& rComplexRef, sal_Int32 nForbiddenFlags, sal_Int32 nFilterBySheet )
1378 {
1379     orRange = CellRangeAddress( static_cast< sal_Int16 >( rComplexRef.Reference1.Sheet ),
1380         rComplexRef.Reference1.Column, rComplexRef.Reference1.Row,
1381         rComplexRef.Reference2.Column, rComplexRef.Reference2.Row );
1382     return
1383         !getFlag( rComplexRef.Reference1.Flags, nForbiddenFlags ) &&
1384         !getFlag( rComplexRef.Reference2.Flags, nForbiddenFlags ) &&
1385         (rComplexRef.Reference1.Sheet == rComplexRef.Reference2.Sheet) &&
1386         ((nFilterBySheet < 0) || (nFilterBySheet == rComplexRef.Reference1.Sheet));
1387 }
1388 
1389 enum TokenToRangeListState { STATE_REF, STATE_SEP, STATE_OPEN, STATE_CLOSE, STATE_ERROR };
1390 
1391 TokenToRangeListState lclProcessRef( ApiCellRangeList& orRanges, const Any& rData, bool bAllowRelative, sal_Int32 nFilterBySheet )
1392 {
1393     using namespace ::com::sun::star::sheet::ReferenceFlags;
1394     const sal_Int32 FORBIDDEN_FLAGS_DEL = COLUMN_DELETED | ROW_DELETED | SHEET_DELETED;
1395     const sal_Int32 FORBIDDEN_FLAGS_REL = FORBIDDEN_FLAGS_DEL | COLUMN_RELATIVE | ROW_RELATIVE | SHEET_RELATIVE | RELATIVE_NAME;
1396 
1397     sal_Int32 nForbiddenFlags = bAllowRelative ? FORBIDDEN_FLAGS_DEL : FORBIDDEN_FLAGS_REL;
1398     SingleReference aSingleRef;
1399     if( rData >>= aSingleRef )
1400     {
1401         CellAddress aAddress;
1402         // ignore invalid addresses (with #REF! errors), but do not stop parsing
1403         if( lclConvertToCellAddress( aAddress, aSingleRef, nForbiddenFlags, nFilterBySheet ) )
1404             orRanges.push_back( CellRangeAddress( aAddress.Sheet, aAddress.Column, aAddress.Row, aAddress.Column, aAddress.Row ) );
1405         return STATE_REF;
1406     }
1407     ComplexReference aComplexRef;
1408     if( rData >>= aComplexRef )
1409     {
1410         CellRangeAddress aRange;
1411         // ignore invalid ranges (with #REF! errors), but do not stop parsing
1412         if( lclConvertToCellRange( aRange, aComplexRef, nForbiddenFlags, nFilterBySheet ) )
1413             orRanges.push_back( aRange );
1414         return STATE_REF;
1415     }
1416     return STATE_ERROR;
1417 }
1418 
1419 TokenToRangeListState lclProcessOpen( sal_Int32& ornParenLevel )
1420 {
1421     ++ornParenLevel;
1422     return STATE_OPEN;
1423 }
1424 
1425 TokenToRangeListState lclProcessClose( sal_Int32& ornParenLevel )
1426 {
1427     --ornParenLevel;
1428     return (ornParenLevel >= 0) ? STATE_CLOSE : STATE_ERROR;
1429 }
1430 
1431 } // namespace
1432 
1433 // ----------------------------------------------------------------------------
1434 
1435 FormulaProcessorBase::FormulaProcessorBase( const WorkbookHelper& rHelper ) :
1436     OpCodeProvider( rHelper.getBaseFilter().getModelFactory(), rHelper.getFilterType(), rHelper.getBiff(), rHelper.getBaseFilter().isImportFilter() ),
1437     ApiOpCodes( getOpCodes() ),
1438     WorkbookHelper( rHelper )
1439 {
1440 }
1441 
1442 // ----------------------------------------------------------------------------
1443 
1444 OUString FormulaProcessorBase::generateAddress2dString( const CellAddress& rAddress, bool bAbsolute )
1445 {
1446     return generateAddress2dString( BinAddress( rAddress ), bAbsolute );
1447 }
1448 
1449 OUString FormulaProcessorBase::generateAddress2dString( const BinAddress& rAddress, bool bAbsolute )
1450 {
1451     OUStringBuffer aBuffer;
1452     // column
1453     for( sal_Int32 nTemp = rAddress.mnCol; nTemp >= 0; (nTemp /= 26) -= 1 )
1454         aBuffer.insert( 0, sal_Unicode( 'A' + (nTemp % 26) ) );
1455     if( bAbsolute )
1456         aBuffer.insert( 0, sal_Unicode( '$' ) );
1457     // row
1458     if( bAbsolute )
1459         aBuffer.append( sal_Unicode( '$' ) );
1460     aBuffer.append( static_cast< sal_Int32 >( rAddress.mnRow + 1 ) );
1461     return aBuffer.makeStringAndClear();
1462 }
1463 
1464 OUString FormulaProcessorBase::generateRange2dString( const CellRangeAddress& rRange, bool bAbsolute )
1465 {
1466     return generateRange2dString( BinRange( rRange ), bAbsolute );
1467 }
1468 
1469 OUString FormulaProcessorBase::generateRange2dString( const BinRange& rRange, bool bAbsolute )
1470 {
1471     OUStringBuffer aBuffer( generateAddress2dString( rRange.maFirst, bAbsolute ) );
1472     if( (rRange.getColCount() > 1) || (rRange.getRowCount() > 1) )
1473         aBuffer.append( sal_Unicode( ':' ) ).append( generateAddress2dString( rRange.maLast, bAbsolute ) );
1474     return aBuffer.makeStringAndClear();
1475 }
1476 
1477 OUString FormulaProcessorBase::generateRangeList2dString( const ApiCellRangeList& rRanges,
1478         bool bAbsolute, sal_Unicode cSeparator, bool bEncloseMultiple )
1479 {
1480     OUStringBuffer aBuffer;
1481     for( ApiCellRangeList::const_iterator aIt = rRanges.begin(), aEnd = rRanges.end(); aIt != aEnd; ++aIt )
1482     {
1483         if( aBuffer.getLength() > 0 )
1484             aBuffer.append( cSeparator );
1485         aBuffer.append( generateRange2dString( *aIt, bAbsolute ) );
1486     }
1487     if( bEncloseMultiple && (rRanges.size() > 1) )
1488         aBuffer.insert( 0, sal_Unicode( '(' ) ).append( sal_Unicode( ')' ) );
1489     return aBuffer.makeStringAndClear();
1490 }
1491 
1492 // ----------------------------------------------------------------------------
1493 
1494 OUString FormulaProcessorBase::generateApiAddressString( const CellAddress& rAddress ) const
1495 {
1496     OUString aCellName;
1497     PropertySet aCellProp( getCellFromDoc( rAddress ) );
1498     aCellProp.getProperty( aCellName, PROP_AbsoluteName );
1499     OSL_ENSURE( aCellName.getLength() > 0, "FormulaProcessorBase::generateApiAddressString - cannot create cell address string" );
1500     return aCellName;
1501 }
1502 
1503 OUString FormulaProcessorBase::generateApiRangeString( const CellRangeAddress& rRange ) const
1504 {
1505     OUString aRangeName;
1506     PropertySet aRangeProp( getCellRangeFromDoc( rRange ) );
1507     aRangeProp.getProperty( aRangeName, PROP_AbsoluteName );
1508     OSL_ENSURE( aRangeName.getLength() > 0, "FormulaProcessorBase::generateApiRangeString - cannot create cell range string" );
1509     return aRangeName;
1510 }
1511 
1512 OUString FormulaProcessorBase::generateApiRangeListString( const ApiCellRangeList& rRanges ) const
1513 {
1514     OUStringBuffer aBuffer;
1515     for( ApiCellRangeList::const_iterator aIt = rRanges.begin(), aEnd = rRanges.end(); aIt != aEnd; ++aIt )
1516     {
1517         OUString aRangeName = generateApiRangeString( *aIt );
1518         if( aRangeName.getLength() > 0 )
1519         {
1520             if( aBuffer.getLength() > 0 )
1521                 aBuffer.append( API_TOKEN_SEP );
1522             aBuffer.append( aRangeName );
1523         }
1524     }
1525     return aBuffer.makeStringAndClear();
1526 }
1527 
1528 OUString FormulaProcessorBase::generateApiString( const OUString& rString )
1529 {
1530     OUString aRetString = rString;
1531     sal_Int32 nQuotePos = aRetString.getLength();
1532     while( (nQuotePos = aRetString.lastIndexOf( '"', nQuotePos )) >= 0 )
1533         aRetString = aRetString.replaceAt( nQuotePos, 1, CREATE_OUSTRING( "\"\"" ) );
1534     return OUStringBuffer().append( sal_Unicode( '"' ) ).append( aRetString ).append( sal_Unicode( '"' ) ).makeStringAndClear();
1535 }
1536 
1537 OUString FormulaProcessorBase::generateApiArray( const Matrix< Any >& rMatrix )
1538 {
1539     OSL_ENSURE( !rMatrix.empty(), "FormulaProcessorBase::generateApiArray - missing matrix values" );
1540     OUStringBuffer aBuffer;
1541     aBuffer.append( API_TOKEN_ARRAY_OPEN );
1542     for( size_t nRow = 0, nHeight = rMatrix.height(); nRow < nHeight; ++nRow )
1543     {
1544         if( nRow > 0 )
1545             aBuffer.append( API_TOKEN_ARRAY_ROWSEP );
1546         for( Matrix< Any >::const_iterator aBeg = rMatrix.row_begin( nRow ), aIt = aBeg, aEnd = rMatrix.row_end( nRow ); aIt != aEnd; ++aIt )
1547         {
1548             double fValue = 0.0;
1549             OUString aString;
1550             if( aIt != aBeg )
1551                 aBuffer.append( API_TOKEN_ARRAY_COLSEP );
1552             if( *aIt >>= fValue )
1553                 aBuffer.append( fValue );
1554             else if( *aIt >>= aString )
1555                 aBuffer.append( generateApiString( aString ) );
1556             else
1557                 aBuffer.appendAscii( "\"\"" );
1558         }
1559     }
1560     aBuffer.append( API_TOKEN_ARRAY_CLOSE );
1561     return aBuffer.makeStringAndClear();
1562 }
1563 
1564 // ----------------------------------------------------------------------------
1565 
1566 Any FormulaProcessorBase::extractReference( const ApiTokenSequence& rTokens ) const
1567 {
1568     ApiTokenIterator aTokenIt( rTokens, OPCODE_SPACES, true );
1569     if( aTokenIt.is() && (aTokenIt->OpCode == OPCODE_PUSH) )
1570     {
1571         Any aRefAny = aTokenIt->Data;
1572         if( !(++aTokenIt).is() && (aRefAny.has< SingleReference >() || aRefAny.has< ComplexReference >()) )
1573             return aRefAny;
1574     }
1575     return Any();
1576 }
1577 
1578 bool FormulaProcessorBase::extractCellAddress( CellAddress& orAddress,
1579         const ApiTokenSequence& rTokens, bool bAllowRelative ) const
1580 {
1581     CellRangeAddress aRange;
1582     if( extractCellRange( aRange, rTokens, bAllowRelative ) && (aRange.StartColumn == aRange.EndColumn) && (aRange.StartRow == aRange.EndRow) )
1583     {
1584         orAddress.Sheet = aRange.Sheet;
1585         orAddress.Column = aRange.StartColumn;
1586         orAddress.Row = aRange.StartRow;
1587         return true;
1588     }
1589     return false;
1590 }
1591 
1592 bool FormulaProcessorBase::extractCellRange( CellRangeAddress& orRange,
1593         const ApiTokenSequence& rTokens, bool bAllowRelative ) const
1594 {
1595     ApiCellRangeList aRanges;
1596     lclProcessRef( aRanges, extractReference( rTokens ), bAllowRelative, -1 );
1597     if( !aRanges.empty() )
1598     {
1599         orRange = aRanges.front();
1600         return true;
1601     }
1602     return false;
1603 }
1604 
1605 void FormulaProcessorBase::extractCellRangeList( ApiCellRangeList& orRanges,
1606         const ApiTokenSequence& rTokens, bool bAllowRelative, sal_Int32 nFilterBySheet ) const
1607 {
1608     orRanges.clear();
1609     TokenToRangeListState eState = STATE_OPEN;
1610     sal_Int32 nParenLevel = 0;
1611     for( ApiTokenIterator aIt( rTokens, OPCODE_SPACES, true ); aIt.is() && (eState != STATE_ERROR); ++aIt )
1612     {
1613         sal_Int32 nOpCode = aIt->OpCode;
1614         switch( eState )
1615         {
1616             // #i107275# accept OPCODE_SEP and OPCODE_LIST as separator token
1617             case STATE_REF:
1618                      if( nOpCode == OPCODE_SEP )   eState = STATE_SEP;
1619                 else if( nOpCode == OPCODE_LIST )  eState = STATE_SEP;
1620                 else if( nOpCode == OPCODE_CLOSE ) eState = lclProcessClose( nParenLevel );
1621                 else                               eState = STATE_ERROR;
1622             break;
1623             case STATE_SEP:
1624                      if( nOpCode == OPCODE_PUSH )  eState = lclProcessRef( orRanges, aIt->Data, bAllowRelative, nFilterBySheet );
1625                 else if( nOpCode == OPCODE_SEP )   eState = STATE_SEP;
1626                 else if( nOpCode == OPCODE_LIST )  eState = STATE_SEP;
1627                 else if( nOpCode == OPCODE_OPEN )  eState = lclProcessOpen( nParenLevel );
1628                 else if( nOpCode == OPCODE_CLOSE ) eState = lclProcessClose( nParenLevel );
1629                 else                               eState = STATE_ERROR;
1630             break;
1631             case STATE_OPEN:
1632                      if( nOpCode == OPCODE_PUSH )  eState = lclProcessRef( orRanges, aIt->Data, bAllowRelative, nFilterBySheet );
1633                 else if( nOpCode == OPCODE_SEP )   eState = STATE_SEP;
1634                 else if( nOpCode == OPCODE_LIST )  eState = STATE_SEP;
1635                 else if( nOpCode == OPCODE_OPEN )  eState = lclProcessOpen( nParenLevel );
1636                 else if( nOpCode == OPCODE_CLOSE ) eState = lclProcessClose( nParenLevel );
1637                 else                               eState = STATE_ERROR;
1638             break;
1639             case STATE_CLOSE:
1640                      if( nOpCode == OPCODE_SEP )   eState = STATE_SEP;
1641                 else if( nOpCode == OPCODE_LIST )  eState = STATE_SEP;
1642                 else if( nOpCode == OPCODE_CLOSE ) eState = lclProcessClose( nParenLevel );
1643                 else                               eState = STATE_ERROR;
1644             break;
1645             default:;
1646         }
1647     }
1648 
1649     if( eState == STATE_ERROR )
1650         orRanges.clear();
1651     else
1652         getAddressConverter().validateCellRangeList( orRanges, false );
1653 }
1654 
1655 bool FormulaProcessorBase::extractString( OUString& orString, const ApiTokenSequence& rTokens ) const
1656 {
1657     ApiTokenIterator aTokenIt( rTokens, OPCODE_SPACES, true );
1658     return aTokenIt.is() && (aTokenIt->OpCode == OPCODE_PUSH) && (aTokenIt->Data >>= orString) && !(++aTokenIt).is();
1659 }
1660 
1661 bool FormulaProcessorBase::extractSpecialTokenInfo( ApiSpecialTokenInfo& orTokenInfo, const ApiTokenSequence& rTokens ) const
1662 {
1663     ApiTokenIterator aTokenIt( rTokens, OPCODE_SPACES, true );
1664     return aTokenIt.is() && (aTokenIt->OpCode == OPCODE_BAD) && (aTokenIt->Data >>= orTokenInfo);
1665 }
1666 
1667 void FormulaProcessorBase::convertStringToStringList(
1668         ApiTokenSequence& orTokens, sal_Unicode cStringSep, bool bTrimLeadingSpaces ) const
1669 {
1670     OUString aString;
1671     if( extractString( aString, orTokens ) && (aString.getLength() > 0) )
1672     {
1673         ::std::vector< ApiToken > aNewTokens;
1674         sal_Int32 nPos = 0;
1675         sal_Int32 nLen = aString.getLength();
1676         while( (0 <= nPos) && (nPos < nLen) )
1677         {
1678             OUString aEntry = aString.getToken( 0, cStringSep, nPos );
1679             if( bTrimLeadingSpaces )
1680             {
1681                 sal_Int32 nStart = 0;
1682                 while( (nStart < aEntry.getLength()) && (aEntry[ nStart ] == ' ') ) ++nStart;
1683                 aEntry = aEntry.copy( nStart );
1684             }
1685             if( !aNewTokens.empty() )
1686                 aNewTokens.push_back( ApiToken( OPCODE_SEP, Any() ) );
1687             aNewTokens.push_back( ApiToken( OPCODE_PUSH, Any( aEntry ) ) );
1688         }
1689         orTokens = ContainerHelper::vectorToSequence( aNewTokens );
1690     }
1691 }
1692 
1693 // ============================================================================
1694 
1695 } // namespace xls
1696 } // namespace oox
1697