1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <memory>
#include <defnamesbuffer.hxx>
#include <com/sun/star/sheet/NamedRangeFlag.hpp>
#include <com/sun/star/sheet/XPrintAreas.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <osl/diagnose.h>
#include <rtl/ustrbuf.hxx>
#include <oox/helper/binaryinputstream.hxx>
#include <oox/helper/attributelist.hxx>
#include <oox/token/tokens.hxx>
#include <addressconverter.hxx>
#include <biffhelper.hxx>
#include <externallinkbuffer.hxx>
#include <formulabase.hxx>
#include <formulaparser.hxx>
#include <worksheetbuffer.hxx>
#include <tokenarray.hxx>
#include <tokenuno.hxx>
#include <compiler.hxx>
#include <document.hxx>
namespace oox::xls {
using namespace ::com::sun::star::sheet;
using namespace ::com::sun::star::table;
using namespace ::com::sun::star::uno;
namespace {
const sal_uInt32 BIFF12_DEFNAME_HIDDEN = 0x00000001;
const sal_uInt32 BIFF12_DEFNAME_FUNC = 0x00000002;
const sal_uInt32 BIFF12_DEFNAME_VBNAME = 0x00000004;
const sal_uInt32 BIFF12_DEFNAME_MACRO = 0x00000008;
const sal_uInt32 BIFF12_DEFNAME_BUILTIN = 0x00000020;
constexpr OUStringLiteral spcOoxPrefix(u"_xlnm.");
const char* const sppcBaseNames[] =
{
"Consolidate_Area",
"Auto_Open",
"Auto_Close",
"Extract",
"Database",
"Criteria",
"Print_Area",
"Print_Titles",
"Recorder",
"Data_Form",
"Auto_Activate",
"Auto_Deactivate",
"Sheet_Title",
"_FilterDatabase"
};
OUString lclGetBaseName( sal_Unicode cBuiltinId )
{
OSL_ENSURE( cBuiltinId < SAL_N_ELEMENTS( sppcBaseNames ), "lclGetBaseName - unsupported built-in identifier" );
OUStringBuffer aBuffer;
if( cBuiltinId < SAL_N_ELEMENTS( sppcBaseNames ) )
aBuffer.appendAscii( sppcBaseNames[ cBuiltinId ] );
else
aBuffer.append( static_cast< sal_Int32 >( cBuiltinId ) );
return aBuffer.makeStringAndClear();
}
OUString lclGetPrefixedName( sal_Unicode cBuiltinId )
{
return spcOoxPrefix + lclGetBaseName( cBuiltinId );
}
/** returns the built-in name identifier from a prefixed built-in name, e.g. '_xlnm.Print_Area'. */
sal_Unicode lclGetBuiltinIdFromPrefixedName( const OUString& rModelName )
{
if( rModelName.matchIgnoreAsciiCase( spcOoxPrefix ) )
{
for( sal_Unicode cBuiltinId = 0; cBuiltinId < SAL_N_ELEMENTS( sppcBaseNames ); ++cBuiltinId )
{
OUString aBaseName = lclGetBaseName( cBuiltinId );
sal_Int32 nBaseNameLen = aBaseName.getLength();
if( (rModelName.getLength() == spcOoxPrefix.getLength() + nBaseNameLen) && rModelName.matchIgnoreAsciiCase( aBaseName, spcOoxPrefix.getLength() ) )
return cBuiltinId;
}
}
return BIFF_DEFNAME_UNKNOWN;
}
/** returns the built-in name identifier from a built-in base name, e.g. 'Print_Area'. */
sal_Unicode lclGetBuiltinIdFromBaseName( const OUString& rModelName )
{
for( sal_Unicode cBuiltinId = 0; cBuiltinId < SAL_N_ELEMENTS( sppcBaseNames ); ++cBuiltinId )
if( rModelName.equalsIgnoreAsciiCaseAscii( sppcBaseNames[ cBuiltinId ] ) )
return cBuiltinId;
return BIFF_DEFNAME_UNKNOWN;
}
OUString lclGetUpcaseModelName( const OUString& rModelName )
{
// TODO: i18n?
return rModelName.toAsciiUpperCase();
}
} // namespace
DefinedNameModel::DefinedNameModel() :
mnSheet( -1 ),
mnFuncGroupId( -1 ),
mbMacro( false ),
mbFunction( false ),
mbVBName( false ),
mbHidden( false )
{
}
DefinedNameBase::DefinedNameBase( const WorkbookHelper& rHelper ) :
WorkbookHelper( rHelper )
{
}
const OUString& DefinedNameBase::getUpcaseModelName() const
{
if( maUpModelName.isEmpty() )
maUpModelName = lclGetUpcaseModelName( maModel.maName );
return maUpModelName;
}
DefinedName::DefinedName( const WorkbookHelper& rHelper ) :
DefinedNameBase( rHelper ),
maScRangeData(nullptr, false),
mnTokenIndex( -1 ),
mnCalcSheet( 0 ),
mcBuiltinId( BIFF_DEFNAME_UNKNOWN )
{
}
void DefinedName::importDefinedName( const AttributeList& rAttribs )
{
maModel.maName = rAttribs.getXString( XML_name, OUString() );
maModel.mnSheet = rAttribs.getInteger( XML_localSheetId, -1 );
maModel.mnFuncGroupId = rAttribs.getInteger( XML_functionGroupId, -1 );
maModel.mbMacro = rAttribs.getBool( XML_xlm, false );
maModel.mbFunction = rAttribs.getBool( XML_function, false );
maModel.mbVBName = rAttribs.getBool( XML_vbProcedure, false );
maModel.mbHidden = rAttribs.getBool( XML_hidden, false );
mnCalcSheet = (maModel.mnSheet >= 0) ? getWorksheets().getCalcSheetIndex( maModel.mnSheet ) : -1;
/* Detect built-in state from name itself, there is no built-in flag.
Built-in names are prefixed with '_xlnm.' instead. */
mcBuiltinId = lclGetBuiltinIdFromPrefixedName( maModel.maName );
}
void DefinedName::setFormula( const OUString& rFormula )
{
maModel.maFormula = rFormula;
}
void DefinedName::importDefinedName( SequenceInputStream& rStrm )
{
sal_uInt32 nFlags;
nFlags = rStrm.readuInt32();
rStrm.skip( 1 ); // keyboard shortcut
maModel.mnSheet = rStrm.readInt32();
rStrm >> maModel.maName;
mnCalcSheet = (maModel.mnSheet >= 0) ? getWorksheets().getCalcSheetIndex( maModel.mnSheet ) : -1;
// macro function/command, hidden flag
maModel.mnFuncGroupId = extractValue< sal_Int32 >( nFlags, 6, 9 );
maModel.mbMacro = getFlag( nFlags, BIFF12_DEFNAME_MACRO );
maModel.mbFunction = getFlag( nFlags, BIFF12_DEFNAME_FUNC );
maModel.mbVBName = getFlag( nFlags, BIFF12_DEFNAME_VBNAME );
maModel.mbHidden = getFlag( nFlags, BIFF12_DEFNAME_HIDDEN );
// get built-in name index from name
if( getFlag( nFlags, BIFF12_DEFNAME_BUILTIN ) )
mcBuiltinId = lclGetBuiltinIdFromBaseName( maModel.maName );
// store token array data
sal_Int64 nRecPos = rStrm.tell();
sal_Int32 nFmlaSize = rStrm.readInt32();
rStrm.skip( nFmlaSize );
sal_Int32 nAddDataSize = rStrm.readInt32();
if( !rStrm.isEof() && (nFmlaSize > 0) && (nAddDataSize >= 0) && (rStrm.getRemaining() >= nAddDataSize) )
{
sal_Int32 nTotalSize = 8 + nFmlaSize + nAddDataSize;
mxFormula.reset( new StreamDataSequence );
rStrm.seek( nRecPos );
rStrm.readData( *mxFormula, nTotalSize );
}
}
void DefinedName::createNameObject( sal_Int32 nIndex )
{
// do not create names for (macro) functions or VBA procedures
// #163146# do not ignore hidden names (may be regular names created by VBA scripts)
if( /*maModel.mbHidden ||*/ maModel.mbFunction || maModel.mbVBName )
return;
// convert original name to final Calc name (TODO: filter invalid characters from model name)
maCalcName = isBuiltinName() ? lclGetPrefixedName( mcBuiltinId ) : maModel.maName;
// #163146# do not rename sheet-local names by default, this breaks VBA scripts
// special flags for this name
sal_Int32 nNameFlags = 0;
using namespace ::com::sun::star::sheet::NamedRangeFlag;
if( !isGlobalName() ) switch( mcBuiltinId )
{
case BIFF_DEFNAME_CRITERIA:
case BIFF_DEFNAME_FILTERDATABASE: nNameFlags = FILTER_CRITERIA; break;
case BIFF_DEFNAME_PRINTAREA: nNameFlags = PRINT_AREA; break;
case BIFF_DEFNAME_PRINTTITLES: nNameFlags = COLUMN_HEADER | ROW_HEADER; break;
}
// create the name and insert it into the document, maCalcName will be changed to the resulting name
if (maModel.mnSheet >= 0)
maScRangeData = createLocalNamedRangeObject( maCalcName, ApiTokenSequence(), nIndex, nNameFlags, maModel.mnSheet, maModel.mbHidden );
else
maScRangeData = createNamedRangeObject( maCalcName, ApiTokenSequence(), nIndex, nNameFlags, maModel.mbHidden );
mnTokenIndex = nIndex;
}
bool DefinedName::isValid(
const css::uno::Sequence<css::sheet::ExternalLinkInfo>& rExternalLinks) const
{
ScRange aRange;
OUString aExternDocName;
OUString aStartTabName;
OUString aEndTabName;
ScRefFlags nFlags = ScRefFlags::VALID | ScRefFlags::TAB_VALID;
aRange.Parse_XL_Header(maModel.maFormula.getStr(), getScDocument(), aExternDocName,
aStartTabName, aEndTabName, nFlags, /*bOnlyAcceptSingle=*/false,
&rExternalLinks);
// aExternDocName is something like 'file:///path/to/my.xlsx' in the valid case, and it's an int
// when it's invalid.
bool bInvalidExternalRef = aExternDocName.toInt32() > 0;
return !bInvalidExternalRef;
}
std::unique_ptr<ScTokenArray> DefinedName::getScTokens(
const css::uno::Sequence<css::sheet::ExternalLinkInfo>& rExternalLinks )
{
ScCompiler aCompiler(getScDocument(), ScAddress(0, 0, mnCalcSheet), formula::FormulaGrammar::GRAM_OOXML);
aCompiler.SetExternalLinks( rExternalLinks);
std::unique_ptr<ScTokenArray> pArray(aCompiler.CompileString(maModel.maFormula));
// Compile the tokens into RPN once to populate information into tokens
// where necessary, e.g. for TableRef inner reference. RPN can be discarded
// after, a resulting error must be reset.
FormulaError nErr = pArray->GetCodeError();
aCompiler.CompileTokenArray();
getScDocument().CheckLinkFormulaNeedingCheck( *pArray);
pArray->DelRPN();
pArray->SetCodeError(nErr);
return pArray;
}
void DefinedName::convertFormula( const css::uno::Sequence<css::sheet::ExternalLinkInfo>& rExternalLinks )
{
ScRangeData* pScRangeData = maScRangeData.first;
// macro function or vba procedure
if (!pScRangeData)
return;
// convert and set formula of the defined name
{
std::unique_ptr<ScTokenArray> pTokenArray = getScTokens( rExternalLinks);
pScRangeData->SetCode( *pTokenArray );
}
ScTokenArray* pTokenArray = pScRangeData->GetCode();
Sequence< FormulaToken > aFTokenSeq;
ScTokenConversion::ConvertToTokenSequence( getScDocument(), aFTokenSeq, *pTokenArray );
// set built-in names (print ranges, repeated titles, filter ranges)
if( isGlobalName() )
return;
switch( mcBuiltinId )
{
case BIFF_DEFNAME_PRINTAREA:
{
Reference< XPrintAreas > xPrintAreas( getSheetFromDoc( mnCalcSheet ), UNO_QUERY );
ScRangeList aPrintRanges;
getFormulaParser().extractCellRangeList( aPrintRanges, aFTokenSeq, mnCalcSheet );
if( xPrintAreas.is() && !aPrintRanges.empty() )
xPrintAreas->setPrintAreas( AddressConverter::toApiSequence(aPrintRanges) );
}
break;
case BIFF_DEFNAME_PRINTTITLES:
{
Reference< XPrintAreas > xPrintAreas( getSheetFromDoc( mnCalcSheet ), UNO_QUERY );
ScRangeList aTitleRanges;
getFormulaParser().extractCellRangeList( aTitleRanges, aFTokenSeq, mnCalcSheet );
if( xPrintAreas.is() && !aTitleRanges.empty() )
{
bool bHasRowTitles = false;
bool bHasColTitles = false;
const ScAddress& rMaxPos = getAddressConverter().getMaxAddress();
for (size_t i = 0, nSize = aTitleRanges.size(); i < nSize; ++i)
{
const ScRange& rRange = aTitleRanges[i];
bool bFullRow = (rRange.aStart.Col() == 0) && ( rRange.aEnd.Col() >= rMaxPos.Col() );
bool bFullCol = (rRange.aStart.Row() == 0) && ( rRange.aEnd.Row() >= rMaxPos.Row() );
if( !bHasRowTitles && bFullRow && !bFullCol )
{
xPrintAreas->setTitleRows( CellRangeAddress(rRange.aStart.Tab(),
rRange.aStart.Col(), rRange.aStart.Row(),
rRange.aEnd.Col(), rRange.aEnd.Row()) );
xPrintAreas->setPrintTitleRows( true );
bHasRowTitles = true;
}
else if( !bHasColTitles && bFullCol && !bFullRow )
{
xPrintAreas->setTitleColumns( CellRangeAddress(rRange.aStart.Tab(),
rRange.aStart.Col(), rRange.aStart.Row(),
rRange.aEnd.Col(), rRange.aEnd.Row()) );
xPrintAreas->setPrintTitleColumns( true );
bHasColTitles = true;
}
}
}
}
break;
}
}
bool DefinedName::getAbsoluteRange( ScRange& orRange ) const
{
ScRangeData* pScRangeData = maScRangeData.first;
ScTokenArray* pTokenArray = pScRangeData->GetCode();
Sequence< FormulaToken > aFTokenSeq;
ScTokenConversion::ConvertToTokenSequence(getScDocument(), aFTokenSeq, *pTokenArray);
return getFormulaParser().extractCellRange( orRange, aFTokenSeq );
}
DefinedName::~DefinedName()
{
// this kind of field is owned by us - see lcl_addNewByNameAndTokens
bool bOwned = maScRangeData.second;
if (bOwned)
delete maScRangeData.first;
}
DefinedNamesBuffer::DefinedNamesBuffer( const WorkbookHelper& rHelper ) :
WorkbookHelper( rHelper )
{
}
DefinedNameRef DefinedNamesBuffer::importDefinedName( const AttributeList& rAttribs )
{
DefinedNameRef xDefName = createDefinedName();
xDefName->importDefinedName( rAttribs );
return xDefName;
}
void DefinedNamesBuffer::importDefinedName( SequenceInputStream& rStrm )
{
createDefinedName()->importDefinedName( rStrm );
}
void DefinedNamesBuffer::finalizeImport()
{
// first insert all names without formula definition into the document, and insert them into the maps
int index = 0;
for( DefinedNameRef& xDefName : maDefNames )
{
if (!xDefName->isValid(getExternalLinks().getLinkInfos()))
{
continue;
}
xDefName->createNameObject( ++index );
// map by sheet index and original model name
maModelNameMap[ SheetNameKey( xDefName->getLocalCalcSheet(), xDefName->getUpcaseModelName() ) ] = xDefName;
// map by sheet index and built-in identifier
if( !xDefName->isGlobalName() && xDefName->isBuiltinName() )
maBuiltinMap[ BuiltinKey( xDefName->getLocalCalcSheet(), xDefName->getBuiltinId() ) ] = xDefName;
// map by API formula token identifier
sal_Int32 nTokenIndex = xDefName->getTokenIndex();
if( nTokenIndex >= 0 )
maTokenIdMap[ nTokenIndex ] = xDefName;
}
/* Now convert all name formulas, so that the formula parser can find all
names in case of circular dependencies. */
maDefNames.forEachMem( &DefinedName::convertFormula, getExternalLinks().getLinkInfos());
}
DefinedNameRef DefinedNamesBuffer::getByIndex( sal_Int32 nIndex ) const
{
return maDefNames.get( nIndex );
}
DefinedNameRef DefinedNamesBuffer::getByTokenIndex( sal_Int32 nIndex ) const
{
return maTokenIdMap.get( nIndex );
}
DefinedNameRef DefinedNamesBuffer::getByModelName( const OUString& rModelName, sal_Int16 nCalcSheet ) const
{
OUString aUpcaseName = lclGetUpcaseModelName( rModelName );
DefinedNameRef xDefName = maModelNameMap.get( SheetNameKey( nCalcSheet, aUpcaseName ) );
// lookup global name, if no local name exists
if( !xDefName && (nCalcSheet >= 0) )
xDefName = maModelNameMap.get( SheetNameKey( -1, aUpcaseName ) );
return xDefName;
}
DefinedNameRef DefinedNamesBuffer::getByBuiltinId( sal_Unicode cBuiltinId, sal_Int16 nCalcSheet ) const
{
return maBuiltinMap.get( BuiltinKey( nCalcSheet, cBuiltinId ) );
}
DefinedNameRef DefinedNamesBuffer::createDefinedName()
{
DefinedNameRef xDefName = std::make_shared<DefinedName>( *this );
maDefNames.push_back( xDefName );
return xDefName;
}
} // namespace oox::xls
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|