LibreOffice Module sc (master) 1
xename.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <xename.hxx>
21
22#include <map>
23
24#include <document.hxx>
25#include <rangenam.hxx>
26#include <tokenarray.hxx>
27#include <xehelper.hxx>
28#include <xelink.hxx>
29#include <excrecds.hxx>
30#include <xlname.hxx>
31#include <xeformula.hxx>
32#include <xestring.hxx>
33#include <xltools.hxx>
34
35#include <formula/grammar.hxx>
36#include <oox/export/utils.hxx>
37#include <oox/token/tokens.hxx>
38
39using namespace ::oox;
40
41// *** Helper classes ***
42
43namespace {
44
46class XclExpName : public XclExpRecord, protected XclExpRoot
47{
48public:
50 explicit XclExpName( const XclExpRoot& rRoot, const OUString& rName );
52 explicit XclExpName( const XclExpRoot& rRoot, sal_Unicode cBuiltIn );
53
55 void SetTokenArray( const XclTokenArrayRef& xTokArr );
57 void SetLocalTab( SCTAB nScTab );
59 void SetHidden( bool bHidden = true );
63 void SetMacroCall( bool bVBasic, bool bFunc );
64
67 void SetSymbol( const OUString& rValue );
68
70 const OUString& GetOrigName() const { return maOrigName; }
73 sal_Unicode GetBuiltInName() const { return mcBuiltIn; }
74
76 const OUString& GetSymbol() const { return msSymbol; }
77
79 bool IsGlobal() const { return mnXclTab == EXC_NAME_GLOBAL; }
81 SCTAB GetScTab() const { return mnScTab; }
82
84 bool IsVolatile() const;
87 bool IsMacroCall( bool bVBasic, bool bFunc ) const;
88
90 virtual void Save( XclExpStream& rStrm ) override;
91
92 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
93
94private:
96 virtual void WriteBody( XclExpStream& rStrm ) override;
98 OUString GetWithDefaultRangeSeparator( const OUString& rSymbol ) const;
99
100private:
101 OUString maOrigName;
102 OUString msSymbol;
103 XclExpStringRef mxName;
104 XclTokenArrayRef mxTokArr;
105 sal_Unicode mcBuiltIn;
106 SCTAB mnScTab;
107 sal_uInt16 mnFlags;
108 sal_uInt16 mnExtSheet;
109 sal_uInt16 mnXclTab;
110};
111
112}
113
116{
117public:
118 explicit XclExpNameManagerImpl( const XclExpRoot& rRoot );
119
121 void Initialize();
122
124 sal_uInt16 InsertName( SCTAB nTab, sal_uInt16 nScNameIdx, SCTAB nCurrTab );
125
127 sal_uInt16 InsertBuiltInName( sal_Unicode cBuiltIn, const XclTokenArrayRef& xTokArr, SCTAB nScTab, const ScRangeList& aRangeList );
128 sal_uInt16 InsertBuiltInName( sal_Unicode cBuiltIn, const XclTokenArrayRef& xTokArr, const ScRange& aRange );
130 sal_uInt16 InsertUniqueName( const OUString& rName, const XclTokenArrayRef& xTokArr, SCTAB nScTab );
132 sal_uInt16 InsertRawName( const OUString& rName );
136 sal_uInt16 InsertMacroCall( const OUString& rMacroName, bool bVBasic, bool bFunc, bool bHidden );
137
139 const XclExpName* GetName( sal_uInt16 nNameIdx ) const;
140
145 void Save( XclExpStream& rStrm );
146
148
149private:
152
153 typedef ::std::map< ::std::pair<SCTAB, OUString>, sal_uInt16> NamedExpMap;
154
155private:
162 sal_uInt16 FindNamedExp( SCTAB nTab, OUString sName );
163
165 sal_uInt16 FindBuiltInNameIdx( const OUString& rName,
166 const OUString& sSymbol ) const;
168 OUString GetUnusedName( const OUString& rName ) const;
169
172 sal_uInt16 Append( XclExpName* pName );
173 sal_uInt16 Append( XclExpNameRef const & rxName ) { return Append(rxName.get()); }
176 sal_uInt16 CreateName( SCTAB nTab, const ScRangeData& rRangeData );
177
179 void CreateBuiltInNames();
181 void CreateUserNames();
182
183private:
192};
193
194// *** Implementation ***
195
196XclExpName::XclExpName( const XclExpRoot& rRoot, const OUString& rName ) :
198 XclExpRoot( rRoot ),
199 maOrigName( rName ),
200 mxName( XclExpStringHelper::CreateString( rRoot, rName, XclStrFlags::EightBitLength ) ),
201 mcBuiltIn( EXC_BUILTIN_UNKNOWN ),
202 mnScTab( SCTAB_GLOBAL ),
204 mnExtSheet( EXC_NAME_GLOBAL ),
205 mnXclTab( EXC_NAME_GLOBAL )
206{
207}
208
209XclExpName::XclExpName( const XclExpRoot& rRoot, sal_Unicode cBuiltIn ) :
211 XclExpRoot( rRoot ),
212 mcBuiltIn( cBuiltIn ),
213 mnScTab( SCTAB_GLOBAL ),
215 mnExtSheet( EXC_NAME_GLOBAL ),
216 mnXclTab( EXC_NAME_GLOBAL )
217{
218 // filter source range is hidden in Excel
219 if( cBuiltIn == EXC_BUILTIN_FILTERDATABASE )
220 SetHidden();
221
222 // special case for BIFF5/7 filter source range - name appears as plain text without built-in flag
223 if( (GetBiff() <= EXC_BIFF5) && (cBuiltIn == EXC_BUILTIN_FILTERDATABASE) )
224 {
227 maOrigName = XclTools::GetXclBuiltInDefName( cBuiltIn );
228 }
229 else
230 {
231 maOrigName = XclTools::GetBuiltInDefNameXml( cBuiltIn ) ;
234 }
235}
236
237void XclExpName::SetTokenArray( const XclTokenArrayRef& xTokArr )
238{
239 mxTokArr = xTokArr;
240}
241
242void XclExpName::SetLocalTab( SCTAB nScTab )
243{
244 OSL_ENSURE( GetTabInfo().IsExportTab( nScTab ), "XclExpName::SetLocalTab - invalid sheet index" );
245 if( !GetTabInfo().IsExportTab( nScTab ) )
246 return;
247
248 mnScTab = nScTab;
249 GetGlobalLinkManager().FindExtSheet( mnExtSheet, mnXclTab, nScTab );
250
251 // special handling for NAME record
252 switch( GetBiff() )
253 {
254 case EXC_BIFF5: // EXTERNSHEET index is positive in NAME record
255 mnExtSheet = ~mnExtSheet + 1;
256 break;
257 case EXC_BIFF8: // EXTERNSHEET index not used, but must be created in link table
258 mnExtSheet = 0;
259 break;
260 default: DBG_ERROR_BIFF();
261 }
262
263 // Excel sheet index is 1-based
264 ++mnXclTab;
265}
266
267void XclExpName::SetHidden( bool bHidden )
268{
270}
271
272void XclExpName::SetMacroCall( bool bVBasic, bool bFunc )
273{
275 ::set_flag( mnFlags, EXC_NAME_VB, bVBasic );
277}
278
279void XclExpName::SetSymbol( const OUString& rSymbol )
280{
281 msSymbol = rSymbol;
282}
283
284bool XclExpName::IsVolatile() const
285{
286 return mxTokArr && mxTokArr->IsVolatile();
287}
288
289bool XclExpName::IsMacroCall( bool bVBasic, bool bFunc ) const
290{
291 return
292 (::get_flag( mnFlags, EXC_NAME_VB ) == bVBasic) &&
293 (::get_flag( mnFlags, EXC_NAME_FUNC ) == bFunc);
294}
295
296void XclExpName::Save( XclExpStream& rStrm )
297{
298 OSL_ENSURE( mxName && (mxName->Len() > 0), "XclExpName::Save - missing name" );
299 OSL_ENSURE( !(IsGlobal() && ::get_flag( mnFlags, EXC_NAME_BUILTIN )), "XclExpName::Save - global built-in name" );
300 SetRecSize( 11 + mxName->GetSize() + (mxTokArr ? mxTokArr->GetSize() : 2) );
301 XclExpRecord::Save( rStrm );
302}
303
304OUString XclExpName::GetWithDefaultRangeSeparator( const OUString& rSymbol ) const
305{
306 sal_Int32 nPos = rSymbol.indexOf(';');
307 if ( nPos > -1 )
308 {
309 // convert with validation
310 ScRange aRange;
312 ScRefFlags nRes = aRange.Parse( rSymbol.copy(0, nPos), GetDoc(), detailsXL );
313 if ( nRes & ScRefFlags::VALID )
314 {
315 nRes = aRange.Parse( rSymbol.copy(nPos+1), GetDoc(), detailsXL );
316 if ( nRes & ScRefFlags::VALID )
317 {
318 return rSymbol.replaceFirst(";", ",");
319 }
320 }
321 }
322 return rSymbol;
323}
324
325void XclExpName::SaveXml( XclExpXmlStream& rStrm )
326{
327 sax_fastparser::FSHelperPtr& rWorkbook = rStrm.GetCurrentStream();
328 rWorkbook->startElement( XML_definedName,
329 // OOXTODO: XML_comment, "",
330 // OOXTODO: XML_customMenu, "",
331 // OOXTODO: XML_description, "",
332 XML_function, ToPsz( ::get_flag( mnFlags, EXC_NAME_VB ) ),
333 // OOXTODO: XML_functionGroupId, "",
334 // OOXTODO: XML_help, "",
335 XML_hidden, ToPsz( ::get_flag( mnFlags, EXC_NAME_HIDDEN ) ),
336 XML_localSheetId, mnScTab == SCTAB_GLOBAL ? nullptr : OString::number( mnScTab ).getStr(),
337 XML_name, maOrigName.toUtf8(),
338 // OOXTODO: XML_publishToServer, "",
339 // OOXTODO: XML_shortcutKey, "",
340 // OOXTODO: XML_statusBar, "",
341 XML_vbProcedure, ToPsz( ::get_flag( mnFlags, EXC_NAME_VB ) )
342 // OOXTODO: XML_workbookParameter, "",
343 // OOXTODO: XML_xlm, ""
344 );
345 rWorkbook->writeEscaped( GetWithDefaultRangeSeparator( msSymbol ) );
346 rWorkbook->endElement( XML_definedName );
347}
348
349void XclExpName::WriteBody( XclExpStream& rStrm )
350{
351 sal_uInt16 nFmlaSize = mxTokArr ? mxTokArr->GetSize() : 0;
352
353 rStrm << mnFlags // flags
354 << sal_uInt8( 0 ); // keyboard shortcut
355 mxName->WriteLenField( rStrm ); // length of name
356 rStrm << nFmlaSize // size of token array
357 << mnExtSheet // BIFF5/7: EXTSHEET index, BIFF8: not used
358 << mnXclTab // 1-based sheet index for local names
359 << sal_uInt32( 0 ); // length of menu/descr/help/status text
360 mxName->WriteFlagField( rStrm ); // BIFF8 flag field (no-op in <=BIFF7)
361 mxName->WriteBuffer( rStrm ); // character array of the name
362 if( mxTokArr )
363 mxTokArr->WriteArray( rStrm ); // token array without size
364}
365
368static bool lcl_EnsureAbs3DToken( const SCTAB nTab, formula::FormulaToken* pTok, const bool bFix = true )
369{
370 bool bFixRequired = false;
371 if ( !pTok || ( pTok->GetType() != formula::svSingleRef && pTok->GetType() != formula::svDoubleRef ) )
372 return bFixRequired;
373
374 ScSingleRefData* pRef1 = pTok->GetSingleRef();
375 if ( !pRef1 )
376 return bFixRequired;
377
378 ScSingleRefData* pRef2 = nullptr;
379 if ( pTok->GetType() == formula::svDoubleRef )
380 pRef2 = pTok->GetSingleRef2();
381
382 if ( pRef1->IsTabRel() || !pRef1->IsFlag3D() )
383 {
384 bFixRequired = true;
385 if ( bFix )
386 {
387 if ( pRef1->IsTabRel() && nTab != SCTAB_GLOBAL )
388 pRef1->SetAbsTab( nTab + pRef1->Tab() ); //XLS requirement
389 if ( !pRef1->IsTabRel() )
390 {
391 pRef1->SetFlag3D( true ); //XLSX requirement
392 if ( pRef2 && !pRef2->IsTabRel() )
393 pRef2->SetFlag3D( pRef2->Tab() != pRef1->Tab() );
394 }
395 }
396 }
397
398 if ( pRef2 && pRef2->IsTabRel() && !pRef1->IsTabRel() )
399 {
400 bFixRequired = true;
401 if ( bFix && nTab != SCTAB_GLOBAL )
402 {
403 pRef2->SetAbsTab( nTab + pRef2->Tab() );
404 pRef2->SetFlag3D( pRef2->Tab() != pRef1->Tab() );
405 }
406 }
407 return bFixRequired;
408}
409
411 XclExpRoot( rRoot ),
412 mnFirstUserIdx( 0 )
413{
414}
415
417{
421}
422
423sal_uInt16 XclExpNameManagerImpl::InsertName( SCTAB nTab, sal_uInt16 nScNameIdx, SCTAB nCurrTab )
424{
425 sal_uInt16 nNameIdx = 0;
426 const ScRangeData* pData = nullptr;
427 ScRangeName* pRN = (nTab == SCTAB_GLOBAL) ? GetDoc().GetRangeName() : GetDoc().GetRangeName(nTab);
428 if (pRN)
429 pData = pRN->findByIndex(nScNameIdx);
430
431 if (pData)
432 {
433 bool bEmulateGlobalRelativeTable = false;
434 const ScTokenArray* pCode = pData->GetCode();
435 if ( pCode
436 && nTab == SCTAB_GLOBAL
437 && (pData->HasType( ScRangeData::Type::AbsPos ) || pData->HasType( ScRangeData::Type::AbsArea )) )
438 {
439 bEmulateGlobalRelativeTable = lcl_EnsureAbs3DToken( nTab, pCode->FirstToken(), /*bFix=*/false );
440 }
441 nNameIdx = FindNamedExp( bEmulateGlobalRelativeTable ? nCurrTab : nTab, pData->GetName() );
442 if (!nNameIdx)
443 nNameIdx = CreateName(nTab, *pData);
444 }
445
446 return nNameIdx;
447}
448
449sal_uInt16 XclExpNameManagerImpl::InsertBuiltInName( sal_Unicode cBuiltIn, const XclTokenArrayRef& xTokArr, const ScRange& aRange )
450{
451 XclExpNameRef xName = new XclExpName( GetRoot(), cBuiltIn );
452 xName->SetTokenArray( xTokArr );
453 xName->SetLocalTab( aRange.aStart.Tab() );
455 xName->SetSymbol( sSymbol );
456 return Append( xName );
457}
458
459sal_uInt16 XclExpNameManagerImpl::InsertBuiltInName( sal_Unicode cBuiltIn, const XclTokenArrayRef& xTokArr, SCTAB nScTab, const ScRangeList& rRangeList )
460{
461 XclExpNameRef xName = new XclExpName( GetRoot(), cBuiltIn );
462 xName->SetTokenArray( xTokArr );
463 xName->SetLocalTab( nScTab );
464 OUString sSymbol;
466 xName->SetSymbol( sSymbol );
467 return Append( xName );
468}
469
471 const OUString& rName, const XclTokenArrayRef& xTokArr, SCTAB nScTab )
472{
473 OSL_ENSURE( !rName.isEmpty(), "XclExpNameManagerImpl::InsertUniqueName - empty name" );
474 XclExpNameRef xName = new XclExpName( GetRoot(), GetUnusedName( rName ) );
475 xName->SetTokenArray( xTokArr );
476 xName->SetLocalTab( nScTab );
477 return Append( xName );
478}
479
480sal_uInt16 XclExpNameManagerImpl::InsertRawName( const OUString& rName )
481{
482 // empty name? may occur in broken external Calc tokens
483 if( rName.isEmpty() )
484 return 0;
485
486 // try to find an existing NAME record, regardless of its type
487 for( size_t nListIdx = mnFirstUserIdx, nListSize = maNameList.GetSize(); nListIdx < nListSize; ++nListIdx )
488 {
489 XclExpNameRef xName = maNameList.GetRecord( nListIdx );
490 if( xName->IsGlobal() && (xName->GetOrigName() == rName) )
491 return static_cast< sal_uInt16 >( nListIdx + 1 );
492 }
493
494 // create a new NAME record
495 XclExpNameRef xName = new XclExpName( GetRoot(), rName );
496 return Append( xName );
497}
498
499sal_uInt16 XclExpNameManagerImpl::InsertMacroCall( const OUString& rMacroName, bool bVBasic, bool bFunc, bool bHidden )
500{
501 // empty name? may occur in broken external Calc tokens
502 if( rMacroName.isEmpty() )
503 return 0;
504
505 // try to find an existing NAME record
506 for( size_t nListIdx = mnFirstUserIdx, nListSize = maNameList.GetSize(); nListIdx < nListSize; ++nListIdx )
507 {
508 XclExpNameRef xName = maNameList.GetRecord( nListIdx );
509 if( xName->IsMacroCall( bVBasic, bFunc ) && (xName->GetOrigName() == rMacroName) )
510 return static_cast< sal_uInt16 >( nListIdx + 1 );
511 }
512
513 // create a new NAME record
514 XclExpNameRef xName = new XclExpName( GetRoot(), rMacroName );
515 xName->SetMacroCall( bVBasic, bFunc );
516 xName->SetHidden( bHidden );
517
518 // for sheet macros, add a #NAME! error
519 if( !bVBasic )
520 xName->SetTokenArray( GetFormulaCompiler().CreateErrorFormula( EXC_ERR_NAME ) );
521
522 return Append( xName );
523}
524
525const XclExpName* XclExpNameManagerImpl::GetName( sal_uInt16 nNameIdx ) const
526{
527 OSL_ENSURE( maNameList.HasRecord( nNameIdx - 1 ), "XclExpNameManagerImpl::GetName - wrong record index" );
528 return maNameList.GetRecord( nNameIdx - 1 );
529}
530
532{
534}
535
537{
538 if( maNameList.IsEmpty() )
539 return;
540 sax_fastparser::FSHelperPtr& rWorkbook = rStrm.GetCurrentStream();
541 rWorkbook->startElement(XML_definedNames);
543 rWorkbook->endElement( XML_definedNames );
544}
545
546// private --------------------------------------------------------------------
547
548sal_uInt16 XclExpNameManagerImpl::FindNamedExp( SCTAB nTab, OUString sName )
549{
550 NamedExpMap::key_type key(nTab, sName);
551 NamedExpMap::const_iterator itr = maNamedExpMap.find(key);
552 return (itr == maNamedExpMap.end()) ? 0 : itr->second;
553}
554
556 const OUString& rName, const OUString& sSymbol ) const
557{
558 /* Get built-in index from the name. Special case: the database range
559 'unnamed' will be mapped to Excel's built-in '_FilterDatabase' name. */
561
562 if( cBuiltIn < EXC_BUILTIN_UNKNOWN )
563 {
564 // try to find the record in existing built-in NAME record list
565 for( size_t nPos = 0; nPos < mnFirstUserIdx; ++nPos )
566 {
568 if( xName->GetBuiltInName() == cBuiltIn && xName->GetSymbol().replace(';', ',') == sSymbol.replace(';', ',') )
569 {
570 // tdf#112567 restore the original built-in names with non-localized separators
571 // TODO: support more localizations, if needed
572 if ( xName->GetSymbol() != sSymbol )
573 {
574 xName->SetSymbol(xName->GetSymbol().replace(';', ','));
575 }
576 return static_cast< sal_uInt16 >( nPos + 1 );
577 }
578 }
579 }
580 return 0;
581}
582
583OUString XclExpNameManagerImpl::GetUnusedName( const OUString& rName ) const
584{
585 OUString aNewName( rName );
586 sal_Int32 nAppIdx = 0;
587 bool bExist = true;
588 while( bExist )
589 {
590 // search the list of user-defined names
591 bExist = false;
592 for( size_t nPos = mnFirstUserIdx, nSize = maNameList.GetSize(); !bExist && (nPos < nSize); ++nPos )
593 {
595 bExist = xName->GetOrigName() == aNewName;
596 // name exists -> create a new name "<originalname>_<counter>"
597 if( bExist )
598 aNewName = rName + "_" + OUString::number( ++nAppIdx );
599 }
600 }
601 return aNewName;
602}
603
604sal_uInt16 XclExpNameManagerImpl::Append( XclExpName* pName )
605{
606 if( maNameList.GetSize() == 0xFFFF )
607 return 0;
609 return static_cast< sal_uInt16 >( maNameList.GetSize() ); // 1-based
610}
611
612sal_uInt16 XclExpNameManagerImpl::CreateName( SCTAB nTab, const ScRangeData& rRangeData )
613{
614 const OUString& rName = rRangeData.GetName();
615
616 /* #i38821# recursive names: first insert the (empty) name object,
617 otherwise a recursive call of this function from the formula compiler
618 with the same defined name will not find it and will create it again. */
619 size_t nOldListSize = maNameList.GetSize();
620 XclExpNameRef xName = new XclExpName( GetRoot(), rName );
621 if (nTab != SCTAB_GLOBAL)
622 xName->SetLocalTab(nTab);
623 sal_uInt16 nNameIdx = Append( xName );
624 // store the index of the NAME record in the lookup map
625 NamedExpMap::key_type key(nTab, rRangeData.GetName());
626 maNamedExpMap[key] = nNameIdx;
627
628 /* Create the definition formula.
629 This may cause recursive creation of other defined names. */
630 if( const ScTokenArray* pScTokArr = const_cast< ScRangeData& >( rRangeData ).GetCode() )
631 {
632 XclTokenArrayRef xTokArr;
633 OUString sSymbol;
634 // MSO requires named ranges to have absolute sheet references
635 if ( rRangeData.HasType( ScRangeData::Type::AbsPos ) || rRangeData.HasType( ScRangeData::Type::AbsArea ) )
636 {
637 // Don't modify the actual document; use a temporary copy to create the export formulas.
638 ScTokenArray aTokenCopy( pScTokArr->CloneValue() );
639 lcl_EnsureAbs3DToken(nTab, aTokenCopy.FirstToken());
640
641 xTokArr = GetFormulaCompiler().CreateFormula(EXC_FMLATYPE_NAME, aTokenCopy);
642 if ( GetOutput() != EXC_OUTPUT_BINARY )
643 {
644 ScCompiler aComp(GetDoc(), rRangeData.GetPos(), aTokenCopy,
646 aComp.CreateStringFromTokenArray( sSymbol );
647 }
648 }
649 else
650 {
651 bool bOOXML = GetOutput() == EXC_OUTPUT_XML_2007;
652 xTokArr = GetFormulaCompiler().CreateFormula( EXC_FMLATYPE_NAME, *pScTokArr, bOOXML ?
653 &rRangeData.GetPos() : nullptr );
654 sSymbol = rRangeData.GetSymbol( (GetOutput() == EXC_OUTPUT_BINARY) ?
656 }
657 xName->SetTokenArray( xTokArr );
658 xName->SetSymbol( sSymbol );
659
660 /* Try to replace by existing built-in name - complete token array is
661 needed for comparison, and due to the recursion problem above this
662 cannot be done earlier. If a built-in name is found, the created NAME
663 record for this name and all following records in the list must be
664 deleted, otherwise they may contain wrong name list indexes. */
665 sal_uInt16 nBuiltInIdx = FindBuiltInNameIdx( rName, sSymbol );
666 if( nBuiltInIdx != 0 )
667 {
668 // delete the new NAME records
669 while( maNameList.GetSize() > nOldListSize )
671 // use index of the found built-in NAME record
672 key = NamedExpMap::key_type(nTab, rRangeData.GetName());
673 maNamedExpMap[key] = nNameIdx = nBuiltInIdx;
674 }
675 }
676
677 return nNameIdx;
678}
679
681{
682 ScDocument& rDoc = GetDoc();
683 XclExpTabInfo& rTabInfo = GetTabInfo();
684
685 /* #i2394# built-in defined names must be sorted by the name of the
686 containing sheet. Example: SheetA!Print_Range must be stored *before*
687 SheetB!Print_Range, regardless of the position of SheetA in the document! */
688 for( SCTAB nScTabIdx = 0, nScTabCount = rTabInfo.GetScTabCount(); nScTabIdx < nScTabCount; ++nScTabIdx )
689 {
690 // find real sheet index from the nScTabIdx counter
691 SCTAB nScTab = rTabInfo.GetRealScTab( nScTabIdx );
692 // create NAME records for all built-in names of this sheet
693 if( rTabInfo.IsExportTab( nScTab ) )
694 {
695 // *** 1) print ranges *** ----------------------------------------
696
697 if( rDoc.HasPrintRange() )
698 {
699 ScRangeList aRangeList;
700 for( sal_uInt16 nIdx = 0, nCount = rDoc.GetPrintRangeCount( nScTab ); nIdx < nCount; ++nIdx )
701 {
702 const ScRange* pPrintRange = rDoc.GetPrintRange( nScTab, nIdx );
703 if (!pPrintRange)
704 continue;
705 ScRange aRange( *pPrintRange );
706 // Calc document does not care about sheet index in print ranges
707 aRange.aStart.SetTab( nScTab );
708 aRange.aEnd.SetTab( nScTab );
709 aRange.PutInOrder();
710 aRangeList.push_back( aRange );
711 }
712 // create the NAME record (do not warn if ranges are shrunken)
713 GetAddressConverter().ValidateRangeList( aRangeList, false );
714 if( !aRangeList.empty() )
716 }
717
718 // *** 2) print titles *** ----------------------------------------
719
720 ScRangeList aTitleList;
721 // repeated columns
722 if( std::optional<ScRange> oColRange = rDoc.GetRepeatColRange( nScTab ) )
723 aTitleList.push_back( ScRange(
724 oColRange->aStart.Col(), 0, nScTab,
725 oColRange->aEnd.Col(), GetXclMaxPos().Row(), nScTab ) );
726 // repeated rows
727 if( std::optional<ScRange> oRowRange = rDoc.GetRepeatRowRange( nScTab ) )
728 aTitleList.push_back( ScRange(
729 0, oRowRange->aStart.Row(), nScTab,
730 GetXclMaxPos().Col(), oRowRange->aEnd.Row(), nScTab ) );
731 // create the NAME record
732 GetAddressConverter().ValidateRangeList( aTitleList, false );
733 if( !aTitleList.empty() )
735
736 // *** 3) filter ranges *** ---------------------------------------
737
738 if( GetBiff() == EXC_BIFF8 )
740 }
741 }
742}
743
745{
746 std::vector<ScRangeData*> vEmulateAsLocalRange;
747 const ScRangeName& rNamedRanges = GetNamedRanges();
748 for (const auto& rEntry : rNamedRanges)
749 {
750 // skip definitions of shared formulas
751 if (!FindNamedExp(SCTAB_GLOBAL, rEntry.second->GetName()))
752 {
753 const ScTokenArray* pCode = rEntry.second->GetCode();
754 if ( pCode
755 && (rEntry.second->HasType( ScRangeData::Type::AbsPos ) || rEntry.second->HasType( ScRangeData::Type::AbsArea ))
756 && lcl_EnsureAbs3DToken( SCTAB_GLOBAL, pCode->FirstToken(), /*bFix=*/false ) )
757 {
758 vEmulateAsLocalRange.emplace_back(rEntry.second.get());
759 }
760 else
761 CreateName(SCTAB_GLOBAL, *rEntry.second);
762 }
763 }
764 //look at sheets containing local range names
765 ScRangeName::TabNameCopyMap rLocalNames;
766 GetDoc().GetAllTabRangeNames(rLocalNames);
767 for (const auto& [rTab, pRangeName] : rLocalNames)
768 {
769 for (const auto& rEntry : *pRangeName)
770 {
771 // skip definitions of shared formulas
772 if (!FindNamedExp(rTab, rEntry.second->GetName()))
773 CreateName(rTab, *rEntry.second);
774 }
775 }
776
777 // Emulate relative global variables by creating a copy in each local range.
778 // Creating AFTER true local range names so that conflicting global names will be ignored.
779 for ( SCTAB nTab = 0; nTab < GetDoc().GetTableCount(); ++nTab )
780 {
781 for ( auto rangeDataItr : vEmulateAsLocalRange )
782 {
783 if ( !FindNamedExp(nTab, rangeDataItr->GetName()) )
784 CreateName(nTab, *rangeDataItr );
785 }
786 }
787}
788
790 XclExpRoot( rRoot ),
791 mxImpl( std::make_shared<XclExpNameManagerImpl>( rRoot ) )
792{
793}
794
796{
797}
798
800{
801 mxImpl->Initialize();
802}
803
804sal_uInt16 XclExpNameManager::InsertName( SCTAB nTab, sal_uInt16 nScNameIdx, SCTAB nCurrTab )
805{
806 return mxImpl->InsertName( nTab, nScNameIdx, nCurrTab );
807}
808
809sal_uInt16 XclExpNameManager::InsertBuiltInName( sal_Unicode cBuiltIn, const ScRange& rRange )
810{
812 return mxImpl->InsertBuiltInName( cBuiltIn, xTokArr, rRange );
813}
814
815sal_uInt16 XclExpNameManager::InsertBuiltInName( sal_Unicode cBuiltIn, const ScRangeList& rRangeList )
816{
817 sal_uInt16 nNameIdx = 0;
818 if( !rRangeList.empty() )
819 {
821 nNameIdx = mxImpl->InsertBuiltInName( cBuiltIn, xTokArr, rRangeList.front().aStart.Tab(), rRangeList );
822 }
823 return nNameIdx;
824}
825
827 const OUString& rName, const XclTokenArrayRef& xTokArr, SCTAB nScTab )
828{
829 return mxImpl->InsertUniqueName( rName, xTokArr, nScTab );
830}
831
832sal_uInt16 XclExpNameManager::InsertRawName( const OUString& rName )
833{
834 return mxImpl->InsertRawName( rName );
835}
836
837sal_uInt16 XclExpNameManager::InsertMacroCall( const OUString& rMacroName, bool bVBasic, bool bFunc, bool bHidden )
838{
839 return mxImpl->InsertMacroCall( rMacroName, bVBasic, bFunc, bHidden );
840}
841
842OUString XclExpNameManager::GetOrigName( sal_uInt16 nNameIdx ) const
843{
844 const XclExpName* pName = mxImpl->GetName( nNameIdx );
845 return pName ? pName->GetOrigName() : OUString();
846}
847
848SCTAB XclExpNameManager::GetScTab( sal_uInt16 nNameIdx ) const
849{
850 const XclExpName* pName = mxImpl->GetName( nNameIdx );
851 return pName ? pName->GetScTab() : SCTAB_GLOBAL;
852}
853
854bool XclExpNameManager::IsVolatile( sal_uInt16 nNameIdx ) const
855{
856 const XclExpName* pName = mxImpl->GetName( nNameIdx );
857 return pName && pName->IsVolatile();
858}
859
861{
862 mxImpl->Save( rStrm );
863}
864
866{
867 mxImpl->SaveXml( rStrm );
868}
869
870/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char * pName
ScRefFlags
Definition: address.hxx:158
SCTAB Tab() const
Definition: address.hxx:283
void SetTab(SCTAB nTabP)
Definition: address.hxx:295
SC_DLLPUBLIC const ScRange * GetPrintRange(SCTAB nTab, sal_uInt16 nPos)
Definition: document.cxx:6394
SC_DLLPUBLIC void GetAllTabRangeNames(ScRangeName::TabNameCopyMap &rRangeNames) const
Get all range names that are local to each table.
Definition: documen3.cxx:110
SC_DLLPUBLIC bool HasPrintRange()
Definition: document.cxx:6365
SC_DLLPUBLIC std::optional< ScRange > GetRepeatColRange(SCTAB nTab)
Definition: document.cxx:6402
SC_DLLPUBLIC std::optional< ScRange > GetRepeatRowRange(SCTAB nTab)
Definition: document.cxx:6410
SC_DLLPUBLIC ScRangeName * GetRangeName(SCTAB nTab) const
Definition: documen3.cxx:174
SC_DLLPUBLIC sal_uInt16 GetPrintRangeCount(SCTAB nTab)
Definition: document.cxx:6386
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:316
bool HasType(Type nType) const
Definition: rangenam.hxx:178
void GetName(OUString &rName) const
Definition: rangenam.hxx:110
const ScAddress & GetPos() const
Definition: rangenam.hxx:113
SC_DLLPUBLIC OUString GetSymbol(const formula::FormulaGrammar::Grammar eGrammar=formula::FormulaGrammar::GRAM_DEFAULT) const
Definition: rangenam.cxx:244
ScRange & front()
Definition: rangelst.hxx:92
void Format(OUString &, ScRefFlags nFlags, const ScDocument &, formula::FormulaGrammar::AddressConvention eConv=formula::FormulaGrammar::CONV_OOO, sal_Unicode cDelimiter=0, bool bFullAddressNotation=false) const
Definition: rangelst.cxx:132
bool empty() const
Definition: rangelst.hxx:88
void push_back(const ScRange &rRange)
Definition: rangelst.cxx:1137
SC_DLLPUBLIC ScRangeData * findByIndex(sal_uInt16 i) const
Definition: rangenam.cxx:716
::std::map< SCTAB, const ScRangeName * > TabNameCopyMap
Map that stores non-managed pointers to ScRangeName instances.
Definition: rangenam.hxx:201
OUString Format(const ScDocument &rDocument, ScRefFlags nFlags=ScRefFlags::ZERO, const ScAddress::Details &rDetails=ScAddress::detailsOOOa1, bool bFullAddressNotation=false) const
Returns string with formatted cell range from aStart to aEnd, according to provided address conventio...
Definition: address.cxx:2170
void PutInOrder()
Definition: address.hxx:622
ScAddress aEnd
Definition: address.hxx:498
ScRefFlags Parse(const OUString &, const ScDocument &, const ScAddress::Details &rDetails=ScAddress::detailsOOOa1, ScAddress::ExternalInfo *pExtInfo=nullptr, const css::uno::Sequence< css::sheet::ExternalLinkInfo > *pExternalLinks=nullptr, const OUString *pErrRef=nullptr)
Definition: address.cxx:1700
ScAddress aStart
Definition: address.hxx:497
void ValidateRangeList(ScRangeList &rScRanges, bool bWarn)
Checks and eventually crops the cell ranges to valid dimensions.
Definition: xehelper.cxx:261
void InitTabFilter(SCTAB nScTab)
Creates the filter records for the specified sheet.
Definition: excrecds.cxx:1150
XclTokenArrayRef CreateFormula(XclFormulaType eType, const ScTokenArray &rScTokArr, const ScAddress *pScBasePos=nullptr, XclExpRefLog *pRefLog=nullptr)
Creates and returns the token array of a formula.
Definition: xeformula.cxx:2644
Implementation class of the name manager.
Definition: xename.cxx:116
XclExpNameList maNameList
Definition: xename.cxx:190
sal_uInt16 InsertBuiltInName(sal_Unicode cBuiltIn, const XclTokenArrayRef &xTokArr, SCTAB nScTab, const ScRangeList &aRangeList)
Inserts a new built-in defined name.
Definition: xename.cxx:459
XclExpNameManagerImpl(const XclExpRoot &rRoot)
Definition: xename.cxx:410
sal_uInt16 CreateName(SCTAB nTab, const ScRangeData &rRangeData)
Creates a new NAME record for the passed user-defined name.
Definition: xename.cxx:612
NamedExpMap maNamedExpMap
Maps Calc's named range to Excel's NAME records.
Definition: xename.cxx:189
size_t mnFirstUserIdx
List of NAME records.
Definition: xename.cxx:191
void Initialize()
Creates NAME records for built-in and user defined names.
Definition: xename.cxx:416
XclExpNameList::RecordRefType XclExpNameRef
Definition: xename.cxx:151
sal_uInt16 FindBuiltInNameIdx(const OUString &rName, const OUString &sSymbol) const
Returns the index of an existing built-in NAME record with the passed definition, otherwise 0.
Definition: xename.cxx:555
void CreateUserNames()
Creates NAME records for all user-defined names in the document.
Definition: xename.cxx:744
sal_uInt16 FindNamedExp(SCTAB nTab, OUString sName)
Definition: xename.cxx:548
sal_uInt16 Append(XclExpName *pName)
Appends a new NAME record to the record list.
Definition: xename.cxx:604
const XclExpName * GetName(sal_uInt16 nNameIdx) const
Returns the NAME record at the specified position or 0 on error.
Definition: xename.cxx:525
void SaveXml(XclExpXmlStream &rStrm)
Definition: xename.cxx:536
sal_uInt16 InsertUniqueName(const OUString &rName, const XclTokenArrayRef &xTokArr, SCTAB nScTab)
Inserts a new defined name.
Definition: xename.cxx:470
sal_uInt16 InsertRawName(const OUString &rName)
Returns index of an existing name, or creates a name without definition.
Definition: xename.cxx:480
void Save(XclExpStream &rStrm)
Writes the entire list of NAME records.
Definition: xename.cxx:531
void CreateBuiltInNames()
Creates NAME records for all built-in names in the document.
Definition: xename.cxx:680
sal_uInt16 InsertName(SCTAB nTab, sal_uInt16 nScNameIdx, SCTAB nCurrTab)
Inserts the Calc name with the passed index and returns the Excel NAME index.
Definition: xename.cxx:423
XclExpRecordList< XclExpName > XclExpNameList
Definition: xename.cxx:150
sal_uInt16 Append(XclExpNameRef const &rxName)
Definition: xename.cxx:173
::std::map< ::std::pair< SCTAB, OUString >, sal_uInt16 > NamedExpMap
Definition: xename.cxx:153
sal_uInt16 InsertMacroCall(const OUString &rMacroName, bool bVBasic, bool bFunc, bool bHidden)
Searches or inserts a defined name describing a macro name.
Definition: xename.cxx:499
OUString GetUnusedName(const OUString &rName) const
Returns an unused name for the passed name.
Definition: xename.cxx:583
sal_uInt16 InsertRawName(const OUString &rName)
Returns index of an existing name, or creates a name without definition.
Definition: xename.cxx:832
SCTAB GetScTab(sal_uInt16 nNameIdx) const
Returns the Calc sheet of a local defined name, or SCTAB_GLOBAL for global defined names.
Definition: xename.cxx:848
sal_uInt16 InsertUniqueName(const OUString &rName, const XclTokenArrayRef &xTokArr, SCTAB nScTab)
Inserts a new defined name.
Definition: xename.cxx:826
sal_uInt16 InsertName(SCTAB nTab, sal_uInt16 nScNameIdx, SCTAB nCurrTab)
Inserts the Calc name with the passed index and returns the Excel NAME index.
Definition: xename.cxx:804
XclExpNameManager(const XclExpRoot &rRoot)
Definition: xename.cxx:789
bool IsVolatile(sal_uInt16 nNameIdx) const
Returns true, if the specified defined name is volatile.
Definition: xename.cxx:854
void Initialize()
Creates NAME records for built-in and user defined names.
Definition: xename.cxx:799
virtual ~XclExpNameManager() override
Definition: xename.cxx:795
virtual void Save(XclExpStream &rStrm) override
Writes the entire list of NAME records.
Definition: xename.cxx:860
sal_uInt16 InsertBuiltInName(sal_Unicode cBuiltIn, const ScRange &rRange)
Inserts a new built-in defined name, referring to the passed sheet range.
Definition: xename.cxx:809
sal_uInt16 InsertMacroCall(const OUString &rMacroName, bool bVBasic, bool bFunc, bool bHidden=false)
Searches or inserts a defined name describing a macro name.
Definition: xename.cxx:837
OUString GetOrigName(sal_uInt16 nNameIdx) const
Returns the Calc sheet of a local defined name, or SCTAB_GLOBAL for global defined names.
Definition: xename.cxx:842
virtual void SaveXml(XclExpXmlStream &rStrm) override
Definition: xename.cxx:865
XclExpNameMgrImplRef mxImpl
Definition: xename.hxx:70
virtual void SaveXml(XclExpXmlStream &rStrm)
Definition: xerecord.cxx:40
virtual void SaveXml(XclExpXmlStream &rStrm) override
Definition: xerecord.hxx:382
bool HasRecord(size_t nPos) const
Returns true, if the passed index points to an exiting record.
Definition: xerecord.hxx:330
size_t GetSize() const
Definition: xerecord.hxx:327
void RemoveRecord(size_t nPos)
Removes the record at the specified position from the list.
Definition: xerecord.hxx:369
bool IsEmpty() const
Definition: xerecord.hxx:326
virtual void Save(XclExpStream &rStrm) override
Writes the complete record list.
Definition: xerecord.hxx:375
void AppendRecord(RecType *pRec)
Appends a record to the list.
Definition: xerecord.hxx:348
RecType * GetRecord(size_t nPos) const
Returns reference to an existing record or empty reference on error.
Definition: xerecord.hxx:333
Base class for single records with any content.
Definition: xerecord.hxx:143
virtual void Save(XclExpStream &rStrm) override
Writes the record header and calls WriteBody().
Definition: xerecord.cxx:150
virtual void WriteBody(XclExpStream &rStrm)
Writes the body of the record (without record header).
Definition: xerecord.cxx:146
Access to global data from other classes.
Definition: xeroot.hxx:113
XclExpNameManager & GetNameManager() const
Returns the buffer that contains internal defined names.
Definition: xeroot.cxx:142
XclExpFormulaCompiler & GetFormulaCompiler() const
Returns the formula compiler to produce formula token arrays.
Definition: xeroot.cxx:88
XclExpAddressConverter & GetAddressConverter() const
Returns the address converter.
Definition: xeroot.cxx:82
XclExpTabInfo & GetTabInfo() const
Returns the buffer for Calc->Excel sheet index conversion.
Definition: xeroot.cxx:76
const XclExpRoot & GetRoot() const
Returns this root instance - for code readability in derived classes.
Definition: xeroot.hxx:118
XclExpFilterManager & GetFilterManager() const
Returns the filter manager.
Definition: xeroot.cxx:154
This class is used to export Excel record streams.
Definition: xestream.hxx:73
This class provides methods to create an XclExpString.
Definition: xehelper.hxx:215
static XclExpStringRef CreateString(const XclExpRoot &rRoot, const OUString &rString, XclStrFlags nFlags=XclStrFlags::NONE, sal_uInt16 nMaxLen=EXC_STR_MAXLEN)
Creates a new unformatted string from the passed string.
Definition: xehelper.cxx:531
Stores the correct Excel sheet index for each Calc sheet.
Definition: xelink.hxx:62
SCTAB GetScTabCount() const
Returns the number of Calc sheets.
Definition: xelink.hxx:89
SCTAB GetRealScTab(SCTAB nSortedScTab) const
Returns the Calc sheet index of the nSortedTab-th entry in the sorted sheet names list.
Definition: xelink.cxx:819
bool IsExportTab(SCTAB nScTab) const
Returns true, if the specified Calc sheet will be exported.
Definition: xelink.cxx:774
XclOutput GetOutput() const
Returns the current output format of the importer/exporter.
Definition: xlroot.hxx:143
XclBiff GetBiff() const
Returns the current BIFF version of the importer/exporter.
Definition: xlroot.hxx:141
ScRangeName & GetNamedRanges() const
Returns the defined names container of the Calc document.
Definition: xlroot.cxx:312
const ScAddress & GetXclMaxPos() const
Returns the highest possible cell address in an Excel document (using current BIFF version).
Definition: xlroot.hxx:246
ScDocument & GetDoc() const
Returns reference to the destination document (import) or source document (export).
Definition: xlroot.cxx:286
static OUString GetBuiltInDefNameXml(sal_Unicode cBuiltIn)
Returns the Excel built-in name with OOXML prefix @descr Adds the "_xlnm." prefix to the representati...
Definition: xltools.cxx:500
static OUString GetXclBuiltInDefName(sal_Unicode cBuiltIn)
Returns the raw English UI representation of a built-in defined name used in NAME records.
Definition: xltools.cxx:484
static sal_Unicode GetBuiltInDefNameIndex(const OUString &rDefName)
Returns the Excel built-in name index of the passed defined name from Calc.
Definition: xltools.cxx:505
void CreateStringFromTokenArray(OUString &rFormula)
FormulaToken * FirstToken() const
FormulaToken ** GetCode() const
virtual const ScSingleRefData * GetSingleRef() const
virtual const ScSingleRefData * GetSingleRef2() const
StackVar GetType() const
int nCount
FuncFlags mnFlags
Information about all parameters.
bool get_flag(Type nBitField, Type nMask)
Returns true, if at least one of the bits set in nMask is set in nBitField.
Definition: ftools.hxx:75
void set_flag(Type &rnBitField, Type nMask, bool bSet=true)
Sets or clears (according to bSet) all set bits of nMask in rnBitField.
Definition: ftools.hxx:95
OUString aName
sal_uInt16 nPos
std::unique_ptr< sal_Int32[]> pData
const char * sName
void SvStream & rStrm
std::shared_ptr< T > make_shared(Args &&... args)
std::shared_ptr< FastSerializerHelper > FSHelperPtr
Single reference (one address) into the sheet.
Definition: refdata.hxx:30
void SetAbsTab(SCTAB nVal)
Definition: refdata.cxx:93
SCTAB Tab() const
Definition: refdata.cxx:254
bool IsTabRel() const
Definition: refdata.hxx:69
void SetFlag3D(bool bVal)
Definition: refdata.hxx:89
bool IsFlag3D() const
Definition: refdata.hxx:90
unsigned char sal_uInt8
sal_uInt16 sal_Unicode
sal_Int16 SCTAB
Definition: types.hxx:22
static bool lcl_EnsureAbs3DToken(const SCTAB nTab, formula::FormulaToken *pTok, const bool bFix=true)
Returns true (needed fixing) if FormulaToken was not absolute and 3D.
Definition: xename.cxx:368
std::shared_ptr< XclExpString > XclExpStringRef
Definition: xeroot.hxx:35
const SCTAB SCTAB_GLOBAL
An invalid Calc sheet index, for common use.
Definition: xlconst.hxx:74
const sal_uInt8 EXC_ERR_NAME
Definition: xlconst.hxx:108
@ EXC_BIFF5
MS Excel 4.0.
Definition: xlconst.hxx:34
@ EXC_BIFF8
MS Excel 5.0, MS Excel 7.0 (95)
Definition: xlconst.hxx:35
@ EXC_OUTPUT_BINARY
Definition: xlconst.hxx:42
@ EXC_OUTPUT_XML_2007
MS Excel binary .xls.
Definition: xlconst.hxx:43
@ EXC_FMLATYPE_NAME
Data validation.
Definition: xlformula.hxx:166
std::shared_ptr< XclTokenArray > XclTokenArrayRef
Definition: xlformula.hxx:416
const sal_Unicode EXC_BUILTIN_UNKNOWN
Definition: xlname.hxx:61
const sal_uInt16 EXC_NAME_DEFAULT
Definition: xlname.hxx:32
const sal_uInt16 EXC_ID_NAME
Definition: xlname.hxx:28
const sal_uInt16 EXC_NAME_VB
Definition: xlname.hxx:35
const sal_Unicode EXC_BUILTIN_FILTERDATABASE
Definition: xlname.hxx:60
const sal_uInt16 EXC_NAME_HIDDEN
Definition: xlname.hxx:33
const sal_uInt16 EXC_NAME_BUILTIN
Definition: xlname.hxx:38
const sal_Unicode EXC_BUILTIN_PRINTTITLES
Definition: xlname.hxx:54
const sal_uInt16 EXC_NAME_GLOBAL
BIFF2 function/command flag.
Definition: xlname.hxx:44
const sal_uInt16 EXC_NAME_PROC
Definition: xlname.hxx:36
const sal_Unicode EXC_BUILTIN_PRINTAREA
Definition: xlname.hxx:53
const sal_uInt16 EXC_NAME_FUNC
Definition: xlname.hxx:34
XclStrFlags
Flags used to specify import/export mode of strings.
Definition: xlstring.hxx:29
@ EightBitLength
Always use UCS-2 characters (default: try to compress). BIFF8 only.
#define DBG_ERROR_BIFF()
Definition: xltools.hxx:33