LibreOffice Module sc (master)  1
rangenam.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 <string.h>
21 #include <memory>
23 #include <unotools/charclass.hxx>
24 #include <com/sun/star/sheet/NamedRangeFlag.hpp>
25 #include <osl/diagnose.h>
26 
27 #include <token.hxx>
28 #include <tokenarray.hxx>
29 #include <rangenam.hxx>
30 #include <global.hxx>
31 #include <compiler.hxx>
32 #include <refupdat.hxx>
33 #include <document.hxx>
34 #include <refupdatecontext.hxx>
35 #include <tokenstringcontext.hxx>
36 
37 #include <formula/errorcodes.hxx>
38 
39 using namespace formula;
40 using ::std::pair;
41 
42 // ScRangeData
43 
45  const OUString& rName,
46  const OUString& rSymbol,
47  const ScAddress& rAddress,
48  Type nType,
49  const FormulaGrammar::Grammar eGrammar ) :
50  aName ( rName ),
51  aUpperName ( ScGlobal::getCharClassPtr()->uppercase( rName ) ),
52  aPos ( rAddress ),
53  eType ( nType ),
54  rDoc ( rDok ),
55  eTempGrammar( eGrammar ),
56  nIndex ( 0 ),
57  bModified ( false )
58 {
59  if (!rSymbol.isEmpty())
60  {
61  // Let the compiler set an error on unknown names for a subsequent
62  // CompileUnresolvedXML().
63  const bool bImporting = rDoc.IsImportingXML();
64  CompileRangeData( rSymbol, bImporting);
65  if (bImporting)
67  }
68  else
69  {
70  // #i63513#/#i65690# don't leave pCode as NULL.
71  // Copy ctor default-constructs pCode if it was NULL, so it's initialized here, too,
72  // to ensure same behavior if unnecessary copying is left out.
73 
74  pCode.reset( new ScTokenArray(rDoc) );
75  pCode->SetFromRangeName(true);
76  }
77 }
78 
80  const OUString& rName,
81  const ScTokenArray& rArr,
82  const ScAddress& rAddress,
83  Type nType ) :
84  aName ( rName ),
85  aUpperName ( ScGlobal::getCharClassPtr()->uppercase( rName ) ),
86  pCode ( new ScTokenArray( rArr ) ),
87  aPos ( rAddress ),
88  eType ( nType ),
89  rDoc ( rDok ),
90  eTempGrammar( FormulaGrammar::GRAM_UNSPECIFIED ),
91  nIndex ( 0 ),
92  bModified ( false )
93 {
94  pCode->SetFromRangeName(true);
95  InitCode();
96 }
97 
99  const OUString& rName,
100  const ScAddress& rTarget ) :
101  aName ( rName ),
102  aUpperName ( ScGlobal::getCharClassPtr()->uppercase( rName ) ),
103  pCode ( new ScTokenArray(rDok) ),
104  aPos ( rTarget ),
105  eType ( Type::Name ),
106  rDoc ( rDok ),
107  eTempGrammar( FormulaGrammar::GRAM_UNSPECIFIED ),
108  nIndex ( 0 ),
109  bModified ( false )
110 {
111  ScSingleRefData aRefData;
112  aRefData.InitAddress( rTarget );
113  aRefData.SetFlag3D( true );
114  pCode->AddSingleReference( aRefData );
115  pCode->SetFromRangeName(true);
116  ScCompiler aComp( rDoc, aPos, *pCode, rDoc.GetGrammar() );
117  aComp.CompileTokenArray();
118  if ( pCode->GetCodeError() == FormulaError::NONE )
119  eType |= Type::AbsPos;
120 }
121 
122 ScRangeData::ScRangeData(const ScRangeData& rScRangeData, ScDocument* pDocument, const ScAddress* pPos) :
123  aName (rScRangeData.aName),
124  aUpperName (rScRangeData.aUpperName),
125  pCode (rScRangeData.pCode ? rScRangeData.pCode->Clone().release() : new ScTokenArray(*pDocument)), // make real copy (not copy-ctor)
126  aPos (pPos ? *pPos : rScRangeData.aPos),
127  eType (rScRangeData.eType),
128  rDoc (pDocument ? *pDocument : rScRangeData.rDoc),
129  eTempGrammar(rScRangeData.eTempGrammar),
130  nIndex (rScRangeData.nIndex),
131  bModified (rScRangeData.bModified)
132 {
133  pCode->SetFromRangeName(true);
134 }
135 
137 {
138 }
139 
140 void ScRangeData::CompileRangeData( const OUString& rSymbol, bool bSetError )
141 {
142  if (eTempGrammar == FormulaGrammar::GRAM_UNSPECIFIED)
143  {
144  OSL_FAIL( "ScRangeData::CompileRangeData: unspecified grammar");
145  // Anything is almost as bad as this, but we might have the best choice
146  // if not loading documents.
147  eTempGrammar = FormulaGrammar::GRAM_NATIVE;
148  }
149 
150  ScCompiler aComp( rDoc, aPos, eTempGrammar );
151  if (bSetError)
153  pCode = aComp.CompileString( rSymbol );
154  pCode->SetFromRangeName(true);
155  if( pCode->GetCodeError() != FormulaError::NONE )
156  return;
157 
159  FormulaToken* p = aIter.GetNextReference();
160  if( p )
161  {
162  // first token is a reference
163  /* FIXME: wouldn't that need a check if it's exactly one reference? */
164  if( p->GetType() == svSingleRef )
166  else
168  }
169  // For manual input set an error for an incomplete formula.
170  if (!rDoc.IsImportingXML())
171  {
172  aComp.CompileTokenArray();
173  pCode->DelRPN();
174  }
175 }
176 
178 {
179  if (pCode->GetCodeError() == FormulaError::NoName)
180  {
181  // Reconstruct the symbol/formula and then recompile.
182  OUString aSymbol;
183  rCxt.setGrammar(eTempGrammar);
184  ScCompiler aComp(rCxt, aPos, *pCode);
185  aComp.CreateStringFromTokenArray( aSymbol);
186  // Don't let the compiler set an error for unknown names on final
187  // compile, errors are handled by the interpreter thereafter.
188  CompileRangeData( aSymbol, false);
190  }
191 }
192 
193 #if DEBUG_FORMULA_COMPILER
194 void ScRangeData::Dump() const
195 {
196  cout << "-- ScRangeData" << endl;
197  cout << " name: " << aName << endl;
198  cout << " ref position: (col=" << aPos.Col() << ", row=" << aPos.Row() << ", sheet=" << aPos.Tab() << ")" << endl;
199 
200  if (pCode)
201  pCode->Dump();
202 }
203 #endif
204 
206 {
207  // set a position that allows "absoluting" of all relative references
208  // in CalcAbsIfRel without errors
209 
210  OSL_ENSURE(aPos == ScAddress(), "position will go lost now");
211 
212  SCCOL nMinCol = 0;
213  SCROW nMinRow = 0;
214  SCTAB nMinTab = 0;
215 
218  while ( ( t = aIter.GetNextReference() ) != nullptr )
219  {
220  ScSingleRefData& rRef1 = *t->GetSingleRef();
221  if ( rRef1.IsColRel() && rRef1.Col() < nMinCol )
222  nMinCol = rRef1.Col();
223  if ( rRef1.IsRowRel() && rRef1.Row() < nMinRow )
224  nMinRow = rRef1.Row();
225  if ( rRef1.IsTabRel() && rRef1.Tab() < nMinTab )
226  nMinTab = rRef1.Tab();
227 
228  if ( t->GetType() == svDoubleRef )
229  {
230  ScSingleRefData& rRef2 = t->GetDoubleRef()->Ref2;
231  if ( rRef2.IsColRel() && rRef2.Col() < nMinCol )
232  nMinCol = rRef2.Col();
233  if ( rRef2.IsRowRel() && rRef2.Row() < nMinRow )
234  nMinRow = rRef2.Row();
235  if ( rRef2.IsTabRel() && rRef2.Tab() < nMinTab )
236  nMinTab = rRef2.Tab();
237  }
238  }
239 
240  aPos = ScAddress( static_cast<SCCOL>(-nMinCol), static_cast<SCROW>(-nMinRow), static_cast<SCTAB>(-nMinTab) );
241 }
242 
243 void ScRangeData::GetSymbol( OUString& rSymbol, const FormulaGrammar::Grammar eGrammar ) const
244 {
245  ScCompiler aComp(rDoc, aPos, *pCode, eGrammar);
246  aComp.CreateStringFromTokenArray( rSymbol );
247 }
248 
249 void ScRangeData::GetSymbol( OUString& rSymbol, const ScAddress& rPos, const FormulaGrammar::Grammar eGrammar ) const
250 {
251  OUString aStr;
252  ScCompiler aComp(rDoc, rPos, *pCode, eGrammar);
253  aComp.CreateStringFromTokenArray( aStr );
254  rSymbol = aStr;
255 }
256 
257 void ScRangeData::UpdateSymbol( OUStringBuffer& rBuffer, const ScAddress& rPos )
258 {
259  std::unique_ptr<ScTokenArray> pTemp( pCode->Clone() );
261  aComp.MoveRelWrap();
262  aComp.CreateStringFromTokenArray( rBuffer );
263 }
264 
266 {
267  sc::RefUpdateResult aRes = pCode->AdjustReferenceInName(rCxt, aPos);
269  if (aRes.mbReferenceModified)
270  rCxt.maUpdatedNames.setUpdatedName(nLocalTab, nIndex);
271 }
272 
273 void ScRangeData::UpdateTranspose( const ScRange& rSource, const ScAddress& rDest )
274 {
275  bool bChanged = false;
276 
279 
280  while ( ( t = aIter.GetNextReference() ) != nullptr )
281  {
282  if( t->GetType() != svIndex )
283  {
284  SingleDoubleRefModifier aMod( *t );
285  ScComplexRefData& rRef = aMod.Ref();
286  // Update only absolute references
287  if (!rRef.Ref1.IsColRel() && !rRef.Ref1.IsRowRel() &&
288  (!rRef.Ref1.IsFlag3D() || !rRef.Ref1.IsTabRel()) &&
289  ( t->GetType() == svSingleRef ||
290  (!rRef.Ref2.IsColRel() && !rRef.Ref2.IsRowRel() &&
291  (!rRef.Ref2.IsFlag3D() || !rRef.Ref2.IsTabRel()))))
292  {
293  ScRange aAbs = rRef.toAbs(rDoc, aPos);
294  // Check if the absolute reference of this range is pointing to the transposed source
295  if (ScRefUpdate::UpdateTranspose(rDoc, rSource, rDest, aAbs) != UR_NOTHING)
296  {
297  rRef.SetRange(rDoc.GetSheetLimits(), aAbs, aPos);
298  bChanged = true;
299  }
300  }
301  }
302  }
303 
304  bModified = bChanged;
305 }
306 
307 void ScRangeData::UpdateGrow( const ScRange& rArea, SCCOL nGrowX, SCROW nGrowY )
308 {
309  bool bChanged = false;
310 
313 
314  while ( ( t = aIter.GetNextReference() ) != nullptr )
315  {
316  if( t->GetType() != svIndex )
317  {
318  SingleDoubleRefModifier aMod( *t );
319  ScComplexRefData& rRef = aMod.Ref();
320  if (!rRef.Ref1.IsColRel() && !rRef.Ref1.IsRowRel() &&
321  (!rRef.Ref1.IsFlag3D() || !rRef.Ref1.IsTabRel()) &&
322  ( t->GetType() == svSingleRef ||
323  (!rRef.Ref2.IsColRel() && !rRef.Ref2.IsRowRel() &&
324  (!rRef.Ref2.IsFlag3D() || !rRef.Ref2.IsTabRel()))))
325  {
326  ScRange aAbs = rRef.toAbs(rDoc, aPos);
327  if (ScRefUpdate::UpdateGrow(rArea, nGrowX, nGrowY, aAbs) != UR_NOTHING)
328  {
329  rRef.SetRange(rDoc.GetSheetLimits(), aAbs, aPos);
330  bChanged = true;
331  }
332  }
333  }
334  }
335 
336  bModified = bChanged; // has to be evaluated immediately afterwards
337 }
338 
339 bool ScRangeData::operator== (const ScRangeData& rData) const // for Undo
340 {
341  if ( nIndex != rData.nIndex ||
342  aName != rData.aName ||
343  aPos != rData.aPos ||
344  eType != rData.eType ) return false;
345 
346  sal_uInt16 nLen = pCode->GetLen();
347  if ( nLen != rData.pCode->GetLen() ) return false;
348 
349  FormulaToken** ppThis = pCode->GetArray();
350  FormulaToken** ppOther = rData.pCode->GetArray();
351 
352  for ( sal_uInt16 i=0; i<nLen; i++ )
353  if ( ppThis[i] != ppOther[i] && !(*ppThis[i] == *ppOther[i]) )
354  return false;
355 
356  return true;
357 }
358 
359 bool ScRangeData::IsRangeAtBlock( const ScRange& rBlock ) const
360 {
361  bool bRet = false;
362  ScRange aRange;
363  if ( IsReference(aRange) )
364  bRet = ( rBlock == aRange );
365  return bRet;
366 }
367 
368 bool ScRangeData::IsReference( ScRange& rRange ) const
369 {
370  if ( (eType & ( Type::AbsArea | Type::RefArea | Type::AbsPos )) && pCode )
371  return pCode->IsReference(rRange, aPos);
372 
373  return false;
374 }
375 
376 bool ScRangeData::IsReference( ScRange& rRange, const ScAddress& rPos ) const
377 {
378  if ( (eType & ( Type::AbsArea | Type::RefArea | Type::AbsPos ) ) && pCode )
379  return pCode->IsReference(rRange, rPos);
380 
381  return false;
382 }
383 
385 {
386  if ( (eType & ( Type::AbsArea | Type::RefArea | Type::AbsPos ) ) && pCode )
387  return pCode->IsValidReference(rRange, aPos);
388 
389  return false;
390 }
391 
393 {
394  sc::RefUpdateResult aRes = pCode->AdjustReferenceOnInsertedTab(rCxt, aPos);
395  if (aRes.mbReferenceModified)
396  rCxt.maUpdatedNames.setUpdatedName(nLocalTab, nIndex);
397 
398  if (rCxt.mnInsertPos <= aPos.Tab())
399  aPos.IncTab(rCxt.mnSheets);
400 }
401 
403 {
404  sc::RefUpdateResult aRes = pCode->AdjustReferenceOnDeletedTab(rCxt, aPos);
405  if (aRes.mbReferenceModified)
406  rCxt.maUpdatedNames.setUpdatedName(nLocalTab, nIndex);
407 
408  if (rCxt.mnDeletePos <= aPos.Tab())
409  aPos.IncTab(-rCxt.mnSheets);
410 }
411 
413 {
414  sc::RefUpdateResult aRes = pCode->AdjustReferenceOnMovedTab(rCxt, aPos);
415  if (aRes.mbReferenceModified)
416  rCxt.maUpdatedNames.setUpdatedName(nLocalTab, nIndex);
417 
418  aPos.SetTab(rCxt.getNewTab(aPos.Tab()));
419 }
420 
421 void ScRangeData::MakeValidName( const ScDocument& rDoc, OUString& rName )
422 {
423 
424  // strip leading invalid characters
425  sal_Int32 nPos = 0;
426  sal_Int32 nLen = rName.getLength();
427  while ( nPos < nLen && !ScCompiler::IsCharFlagAllConventions( rName, nPos, ScCharFlags::Name) )
428  ++nPos;
429  if ( nPos>0 )
430  rName = rName.copy(nPos);
431 
432  // if the first character is an invalid start character, precede with '_'
433  if ( !rName.isEmpty() && !ScCompiler::IsCharFlagAllConventions( rName, 0, ScCharFlags::CharName ) )
434  rName = "_" + rName;
435 
436  // replace invalid with '_'
437  nLen = rName.getLength();
438  for (nPos=0; nPos<nLen; nPos++)
439  {
441  rName = rName.replaceAt( nPos, 1, "_" );
442  }
443 
444  // Ensure that the proposed name is not a reference under any convention,
445  // same as in IsNameValid()
446  ScAddress aAddr;
447  ScRange aRange;
448  for (int nConv = FormulaGrammar::CONV_UNSPECIFIED; ++nConv < FormulaGrammar::CONV_LAST; )
449  {
450  ScAddress::Details details( static_cast<FormulaGrammar::AddressConvention>( nConv ) );
451  // Don't check Parse on VALID, any partial only VALID may result in
452  // #REF! during compile later!
453  while (aRange.Parse(rName, rDoc, details) != ScRefFlags::ZERO ||
454  aAddr.Parse(rName, rDoc, details) != ScRefFlags::ZERO)
455  {
456  // Range Parse is partially valid also with invalid sheet name,
457  // Address Parse ditto, during compile name would generate a #REF!
458  if ( rName.indexOf( '.' ) != -1 )
459  rName = rName.replaceFirst( ".", "_" );
460  else
461  rName = "_" + rName;
462  }
463  }
464 }
465 
467 {
468  /* XXX If changed, sc/source/filter/ftools/ftools.cxx
469  * ScfTools::ConvertToScDefinedName needs to be changed too. */
470  char const a('.');
471  if (rName.indexOf(a) != -1)
473  sal_Int32 nPos = 0;
474  sal_Int32 nLen = rName.getLength();
475  if ( !nLen || !ScCompiler::IsCharFlagAllConventions( rName, nPos++, ScCharFlags::CharName ) )
477  while ( nPos < nLen )
478  {
481  }
482  ScAddress aAddr;
483  ScRange aRange;
484  for (int nConv = FormulaGrammar::CONV_UNSPECIFIED; ++nConv < FormulaGrammar::CONV_LAST; )
485  {
486  ScAddress::Details details( static_cast<FormulaGrammar::AddressConvention>( nConv ) );
487  // Don't check Parse on VALID, any partial only VALID may result in
488  // #REF! during compile later!
489  if (aRange.Parse(rName, rDoc, details) != ScRefFlags::ZERO ||
490  aAddr.Parse(rName, rDoc, details) != ScRefFlags::ZERO )
491  {
493  }
494  }
496 }
497 
499 {
500  return pCode ? pCode->GetCodeError() : FormulaError::NONE;
501 }
502 
504 {
505  return pCode->HasReferences();
506 }
507 
508 sal_uInt32 ScRangeData::GetUnoType() const
509 {
510  sal_uInt32 nUnoType = 0;
511  if ( HasType(Type::Criteria) ) nUnoType |= css::sheet::NamedRangeFlag::FILTER_CRITERIA;
512  if ( HasType(Type::PrintArea) ) nUnoType |= css::sheet::NamedRangeFlag::PRINT_AREA;
515  return nUnoType;
516 }
517 
519 {
520  // try to make sure all relative references and the reference position
521  // are within existing tables, so they can be represented as text
522  // (if the range of used tables is more than the existing tables,
523  // the result may still contain invalid tables, because the relative
524  // references aren't changed so formulas stay the same)
525 
526  // find range of used tables
527 
528  SCTAB nMinTab = aPos.Tab();
529  SCTAB nMaxTab = nMinTab;
532  while ( ( t = aIter.GetNextReference() ) != nullptr )
533  {
534  ScSingleRefData& rRef1 = *t->GetSingleRef();
535  ScAddress aAbs = rRef1.toAbs(rDoc, aPos);
536  if ( rRef1.IsTabRel() && !rRef1.IsTabDeleted() )
537  {
538  if (aAbs.Tab() < nMinTab)
539  nMinTab = aAbs.Tab();
540  if (aAbs.Tab() > nMaxTab)
541  nMaxTab = aAbs.Tab();
542  }
543  if ( t->GetType() == svDoubleRef )
544  {
545  ScSingleRefData& rRef2 = t->GetDoubleRef()->Ref2;
546  aAbs = rRef2.toAbs(rDoc, aPos);
547  if ( rRef2.IsTabRel() && !rRef2.IsTabDeleted() )
548  {
549  if (aAbs.Tab() < nMinTab)
550  nMinTab = aAbs.Tab();
551  if (aAbs.Tab() > nMaxTab)
552  nMaxTab = aAbs.Tab();
553  }
554  }
555  }
556 
557  SCTAB nTabCount = rDoc.GetTableCount();
558  if ( nMaxTab < nTabCount || nMinTab <= 0 )
559  return;
560 
561  // move position and relative tab refs
562  // The formulas that use the name are not changed by this
563 
564  SCTAB nMove = nMinTab;
565  ScAddress aOldPos = aPos;
566  aPos.SetTab( aPos.Tab() - nMove );
567 
568  aIter.Reset();
569  while ( ( t = aIter.GetNextReference() ) != nullptr )
570  {
571  switch (t->GetType())
572  {
573  case svSingleRef:
574  {
575  ScSingleRefData& rRef = *t->GetSingleRef();
576  if (!rRef.IsTabDeleted())
577  {
578  ScAddress aAbs = rRef.toAbs(rDoc, aOldPos);
579  rRef.SetAddress(rDoc.GetSheetLimits(), aAbs, aPos);
580  }
581  }
582  break;
583  case svDoubleRef:
584  {
585  ScComplexRefData& rRef = *t->GetDoubleRef();
586  if (!rRef.Ref1.IsTabDeleted())
587  {
588  ScAddress aAbs = rRef.Ref1.toAbs(rDoc, aOldPos);
589  rRef.Ref1.SetAddress(rDoc.GetSheetLimits(), aAbs, aPos);
590  }
591  if (!rRef.Ref2.IsTabDeleted())
592  {
593  ScAddress aAbs = rRef.Ref2.toAbs(rDoc, aOldPos);
594  rRef.Ref2.SetAddress(rDoc.GetSheetLimits(), aAbs, aPos);
595  }
596  }
597  break;
598  default:
599  ;
600  }
601  }
602 }
603 
605 {
606  pCode.reset(new ScTokenArray( rArr ));
607  pCode->SetFromRangeName(true);
608  InitCode();
609 }
610 
612 {
613  if( pCode->GetCodeError() == FormulaError::NONE )
614  {
616  if( p ) // exact one reference at first
617  {
618  if( p->GetType() == svSingleRef )
620  else
622  }
623  }
624 }
625 
626 extern "C"
627 int ScRangeData_QsortNameCompare( const void* p1, const void* p2 )
628 {
629  return static_cast<int>(ScGlobal::GetCollator()->compareString(
630  (*static_cast<const ScRangeData* const *>(p1))->GetName(),
631  (*static_cast<const ScRangeData* const *>(p2))->GetName() ));
632 }
633 
634 namespace {
635 
639 class MatchByRange
640 {
641  const ScRange& mrRange;
642 public:
643  explicit MatchByRange(const ScRange& rRange) : mrRange(rRange) {}
644  bool operator() (std::pair<OUString const, std::unique_ptr<ScRangeData>> const& r) const
645  {
646  return r.second->IsRangeAtBlock(mrRange);
647  }
648 };
649 
650 }
651 
653 
655 {
656  for (auto const& it : r.m_Data)
657  {
658  m_Data.insert(std::make_pair(it.first, std::make_unique<ScRangeData>(*it.second)));
659  }
660  // std::map was cloned, so each collection needs its own index to data.
661  maIndexToData.resize( r.maIndexToData.size(), nullptr);
662  for (auto const& itr : m_Data)
663  {
664  size_t nPos = itr.second->GetIndex() - 1;
665  if (nPos >= maIndexToData.size())
666  {
667  OSL_FAIL( "ScRangeName copy-ctor: maIndexToData size doesn't fit");
668  maIndexToData.resize(nPos+1, nullptr);
669  }
670  maIndexToData[nPos] = itr.second.get();
671  }
672 }
673 
674 const ScRangeData* ScRangeName::findByRange(const ScRange& rRange) const
675 {
676  DataType::const_iterator itr = std::find_if(
677  m_Data.begin(), m_Data.end(), MatchByRange(rRange));
678  return itr == m_Data.end() ? nullptr : itr->second.get();
679 }
680 
682 {
683  DataType::iterator itr = m_Data.find(rName);
684  return itr == m_Data.end() ? nullptr : itr->second.get();
685 }
686 
687 const ScRangeData* ScRangeName::findByUpperName(const OUString& rName) const
688 {
689  DataType::const_iterator itr = m_Data.find(rName);
690  return itr == m_Data.end() ? nullptr : itr->second.get();
691 }
692 
694 {
695  if (!i)
696  // index should never be zero.
697  return nullptr;
698 
699  size_t nPos = i - 1;
700  return nPos < maIndexToData.size() ? maIndexToData[nPos] : nullptr;
701 }
702 
704 {
705  if (rCxt.meMode == URM_COPY)
706  // Copying cells does not modify named expressions.
707  return;
708 
709  for (auto const& itr : m_Data)
710  {
711  itr.second->UpdateReference(rCxt, nLocalTab);
712  }
713 }
714 
716 {
717  for (auto const& itr : m_Data)
718  {
719  itr.second->UpdateInsertTab(rCxt, nLocalTab);
720  }
721 }
722 
724 {
725  for (auto const& itr : m_Data)
726  {
727  itr.second->UpdateDeleteTab(rCxt, nLocalTab);
728  }
729 }
730 
732 {
733  for (auto const& itr : m_Data)
734  {
735  itr.second->UpdateMoveTab(rCxt, nLocalTab);
736  }
737 }
738 
739 void ScRangeName::UpdateTranspose(const ScRange& rSource, const ScAddress& rDest)
740 {
741  for (auto const& itr : m_Data)
742  {
743  itr.second->UpdateTranspose(rSource, rDest);
744  }
745 }
746 
747 void ScRangeName::UpdateGrow(const ScRange& rArea, SCCOL nGrowX, SCROW nGrowY)
748 {
749  for (auto const& itr : m_Data)
750  {
751  itr.second->UpdateGrow(rArea, nGrowX, nGrowY);
752  }
753 }
754 
756 {
757  for (auto const& itr : m_Data)
758  {
759  itr.second->CompileUnresolvedXML(rCxt);
760  }
761 }
762 
763 void ScRangeName::CopyUsedNames( const SCTAB nLocalTab, const SCTAB nOldTab, const SCTAB nNewTab,
764  const ScDocument& rOldDoc, ScDocument& rNewDoc, const bool bGlobalNamesToLocal ) const
765 {
766  for (auto const& itr : m_Data)
767  {
768  SCTAB nSheet = (nLocalTab < 0) ? nLocalTab : nOldTab;
769  sal_uInt16 nIndex = itr.second->GetIndex();
770  ScAddress aOldPos( itr.second->GetPos());
771  aOldPos.SetTab( nOldTab);
772  ScAddress aNewPos( aOldPos);
773  aNewPos.SetTab( nNewTab);
774  ScRangeData* pRangeData = nullptr;
775  rOldDoc.CopyAdjustRangeName( nSheet, nIndex, pRangeData, rNewDoc, aNewPos, aOldPos, bGlobalNamesToLocal, false);
776  }
777 }
778 
780 {
781  return m_Data.begin();
782 }
783 
785 {
786  return m_Data.end();
787 }
788 
790 {
791  return m_Data.begin();
792 }
793 
795 {
796  return m_Data.end();
797 }
798 
799 size_t ScRangeName::size() const
800 {
801  return m_Data.size();
802 }
803 
804 bool ScRangeName::empty() const
805 {
806  return m_Data.empty();
807 }
808 
809 bool ScRangeName::insert( ScRangeData* p, bool bReuseFreeIndex )
810 {
811  if (!p)
812  return false;
813 
814  if (!p->GetIndex())
815  {
816  // Assign a new index. An index must be unique and is never 0.
817  if (bReuseFreeIndex)
818  {
819  IndexDataType::iterator itr = std::find(
820  maIndexToData.begin(), maIndexToData.end(), static_cast<ScRangeData*>(nullptr));
821  if (itr != maIndexToData.end())
822  {
823  // Empty slot exists. Re-use it.
824  size_t nPos = std::distance(maIndexToData.begin(), itr);
825  p->SetIndex(nPos + 1);
826  }
827  else
828  // No empty slot. Append it to the end.
829  p->SetIndex(maIndexToData.size() + 1);
830  }
831  else
832  {
833  p->SetIndex(maIndexToData.size() + 1);
834  }
835  }
836 
837  OUString aName(p->GetUpperName());
838  erase(aName); // ptr_map won't insert it if a duplicate name exists.
839  pair<DataType::iterator, bool> r =
840  m_Data.insert(std::make_pair(aName, std::unique_ptr<ScRangeData>(p)));
841  if (r.second)
842  {
843  // Data inserted. Store its index for mapping.
844  size_t nPos = p->GetIndex() - 1;
845  if (nPos >= maIndexToData.size())
846  maIndexToData.resize(nPos+1, nullptr);
847  maIndexToData[nPos] = p;
848  }
849  return r.second;
850 }
851 
853 {
854  erase(r.GetUpperName());
855 }
856 
857 void ScRangeName::erase(const OUString& rName)
858 {
859  DataType::iterator itr = m_Data.find(rName);
860  if (itr != m_Data.end())
861  erase(itr);
862 }
863 
864 void ScRangeName::erase(const iterator& itr)
865 {
866  sal_uInt16 nIndex = itr->second->GetIndex();
867  m_Data.erase(itr);
868  OSL_ENSURE( 0 < nIndex && nIndex <= maIndexToData.size(), "ScRangeName::erase: bad index");
869  if (0 < nIndex && nIndex <= maIndexToData.size())
870  maIndexToData[nIndex-1] = nullptr;
871 }
872 
874 {
875  m_Data.clear();
876  maIndexToData.clear();
877 }
878 
880 {
881  return std::equal(m_Data.begin(), m_Data.end(), r.m_Data.begin(), r.m_Data.end(),
882  [](const DataType::value_type& lhs, const DataType::value_type& rhs) {
883  return (lhs.first == rhs.first) && (*lhs.second == *rhs.second);
884  });
885 }
886 
887 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsTabRel() const
Definition: refdata.hxx:69
SCCOL Col() const
Definition: refdata.cxx:247
sal_uInt32 GetUnoType() const
Definition: rangenam.cxx:508
void UpdateDeleteTab(sc::RefUpdateDeleteTabContext &rCxt, SCTAB nLocalTab=-1)
Definition: rangenam.cxx:723
sal_Int32 nIndex
bool IsTabDeleted() const
Definition: refdata.hxx:86
void CompileUnresolvedXML(sc::CompileFormulaContext &rCxt)
Compile those names that couldn't be resolved during loading and inserting because they may have refe...
Definition: rangenam.cxx:755
sal_Int32 compareString(const OUString &s1, const OUString &s2) const
void IncTab(SCTAB nDelta=1)
Definition: address.hxx:307
SCROW Row() const
Definition: address.hxx:261
SC_DLLPUBLIC ScRange toAbs(const ScSheetLimits &rLimits, const ScAddress &rPos) const
Definition: refdata.cxx:493
SC_DLLPUBLIC void SetCode(const ScTokenArray &)
Definition: rangenam.cxx:604
Single reference (one address) into the sheet.
Definition: refdata.hxx:29
const OUString & GetUpperName() const
Definition: rangenam.hxx:112
ScAddress toAbs(const ScSheetLimits &rLimits, const ScAddress &rPos) const
Definition: refdata.cxx:193
sal_uInt16 GetIndex() const
Definition: rangenam.hxx:116
Context for reference update during shifting, moving or copying of cell ranges.
static ScRefUpdateRes UpdateGrow(const ScRange &rArea, SCCOL nGrowX, SCROW nGrowY, ScRange &rRef)
Definition: refupdat.cxx:556
void UpdateTranspose(const ScRange &rSource, const ScAddress &rDest)
Definition: rangenam.cxx:273
std::unique_ptr< ScTokenArray > CompileString(const OUString &rFormula)
Tokenize formula expression string into an array of tokens.
Definition: compiler.cxx:4542
static SC_DLLPUBLIC CollatorWrapper * GetCollator()
Definition: global.cxx:1028
SCTAB getNewTab(SCTAB nOldTab) const
const ContentProperties & rData
bool operator==(const ScRangeName &r) const
Definition: rangenam.cxx:879
static void MakeValidName(const ScDocument &rDoc, OUString &rName)
Definition: rangenam.cxx:421
DataType::iterator iterator
Definition: rangenam.hxx:196
void clear()
Definition: rangenam.cxx:873
void CompileUnresolvedXML(sc::CompileFormulaContext &rCxt)
Definition: rangenam.cxx:177
OUString Name
SC_DLLPUBLIC formula::FormulaGrammar::Grammar GetGrammar() const
Definition: document.hxx:983
void CreateStringFromTokenArray(OUString &rFormula)
IndexDataType maIndexToData
Definition: rangenam.hxx:189
void setUpdatedName(SCTAB nTab, sal_uInt16 nIndex)
SC_DLLPUBLIC void CheckLinkFormulaNeedingCheck(const ScTokenArray &rCode)
Check token array and set link check if ocDde/ocWebservice is contained.
Definition: documen8.cxx:1154
DataType::const_iterator const_iterator
Definition: rangenam.hxx:195
formula::FormulaGrammar::Grammar eTempGrammar
Definition: rangenam.hxx:77
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:313
bool empty() const
Definition: rangenam.cxx:804
DataType m_Data
Definition: rangenam.hxx:188
virtual const ScSingleRefData * GetSingleRef() const
void UpdateGrow(const ScRange &rArea, SCCOL nGrowX, SCROW nGrowY)
Definition: rangenam.cxx:307
void CopyUsedNames(const SCTAB nLocalTab, const SCTAB nOldTab, const SCTAB nNewTab, const ScDocument &rOldDoc, ScDocument &rNewDoc, const bool bGlobalNamesToLocal) const
Copy names while copying a sheet if they reference the sheet to be copied.
Definition: rangenam.cxx:763
void UpdateGrow(const ScRange &rArea, SCCOL nGrowX, SCROW nGrowY)
Definition: rangenam.cxx:747
SCTAB Tab() const
Definition: address.hxx:270
SC_DLLPUBLIC void GuessPosition()
Definition: rangenam.cxx:205
bool IsColRel() const
Definition: refdata.hxx:65
SC_DLLPUBLIC const_iterator begin() const
Definition: rangenam.cxx:779
OUString aName
Definition: rangenam.hxx:69
void UpdateInsertTab(sc::RefUpdateInsertTabContext &rCxt, SCTAB nLocalTab)
Definition: rangenam.cxx:392
void UpdateReference(sc::RefUpdateContext &rCxt, SCTAB nLocalTab=-1)
Definition: rangenam.cxx:703
bool HasReferences() const
Definition: rangenam.cxx:503
std::unique_ptr< ScTokenArray > pCode
Definition: rangenam.hxx:73
void UpdateInsertTab(sc::RefUpdateInsertTabContext &rCxt, SCTAB nLocalTab=-1)
Definition: rangenam.cxx:715
DocumentType eType
bool IsRangeAtBlock(const ScRange &) const
Definition: rangenam.cxx:359
void SetTab(SCTAB nTabP)
Definition: address.hxx:282
SC_DLLPUBLIC ScRangeData * findByUpperName(const OUString &rName)
Definition: rangenam.cxx:681
SC_DLLPUBLIC const ScRangeData * findByRange(const ScRange &rRange) const
Definition: rangenam.cxx:674
SC_DLLPUBLIC const_iterator end() const
Definition: rangenam.cxx:784
bool IsImportingXML() const
Definition: document.hxx:2148
virtual const ScComplexRefData * GetDoubleRef() const
SC_DLLPUBLIC void GetSymbol(OUString &rSymbol, const formula::FormulaGrammar::Grammar eGrammar=formula::FormulaGrammar::GRAM_DEFAULT) const
Definition: rangenam.cxx:243
int i
Reference< XAnimationNode > Clone(const Reference< XAnimationNode > &xSourceNode, const SdPage *pSource, const SdPage *pTarget)
uno_Any a
UpdateRefMode meMode
update mode - insert/delete, copy, or move.
void UpdateTranspose(const ScRange &rSource, const ScAddress &rDest)
Definition: rangenam.cxx:739
void SetFlag3D(bool bVal)
Definition: refdata.hxx:89
int ScRangeData_QsortNameCompare(const void *p1, const void *p2)
Definition: rangenam.cxx:627
void InitAddress(const ScAddress &rAdr)
InitAddress: InitFlags and set address.
Definition: refdata.cxx:27
sal_Int16 SCCOL
Definition: types.hxx:21
ScSheetLimits & GetSheetLimits() const
Definition: document.hxx:872
ScSingleRefData Ref1
Definition: refdata.hxx:124
svIndex
static SC_DLLPUBLIC IsNameValidType IsNameValid(const OUString &rName, const ScDocument &rDoc)
Definition: rangenam.cxx:466
static bool IsCharFlagAllConventions(OUString const &rStr, sal_Int32 nPos, ScCharFlags nFlags)
If the character is allowed as tested by nFlags (SC_COMPILER_C_...
Definition: compiler.cxx:5065
ScSingleRefData Ref2
Definition: refdata.hxx:125
bool IsRowRel() const
Definition: refdata.hxx:67
void UpdateMoveTab(sc::RefUpdateMoveTabContext &rCxt, SCTAB nLocalTab)
Definition: rangenam.cxx:412
ScAddress aPos
Definition: rangenam.hxx:74
SC_DLLPUBLIC ScRangeData * findByIndex(sal_uInt16 i) const
Definition: rangenam.cxx:693
void erase(const ScRangeData &r)
Definition: rangenam.cxx:852
bool operator==(const ScRangeData &rData) const
Definition: rangenam.cxx:339
bool HasType(Type nType) const
Definition: rangenam.hxx:176
void CompileRangeData(const OUString &rSymbol, bool bSetError)
Definition: rangenam.cxx:140
SCTAB Tab() const
Definition: refdata.cxx:254
void setGrammar(formula::FormulaGrammar::Grammar eGram)
ScComplexRefData & Ref()
Definition: token.hxx:437
Reference not affected, no change at all.
Definition: refupdat.hxx:31
svSingleRef
bool IsFlag3D() const
Definition: refdata.hxx:90
FormulaError
void UpdateMoveTab(sc::RefUpdateMoveTabContext &rCxt, SCTAB nLocalTab=-1)
Definition: rangenam.cxx:731
SCCOL Col() const
Definition: address.hxx:266
XPropertyListType t
ScDocument & rDoc
Definition: rangenam.hxx:76
svDoubleRef
sal_Int32 SCROW
Definition: types.hxx:17
void SetIndex(sal_uInt16 nInd)
Definition: rangenam.hxx:115
static ScRefUpdateRes UpdateTranspose(const ScDocument &rDoc, const ScRange &rSource, const ScAddress &rDest, ScRange &rRef)
Definition: refupdat.cxx:532
void UpdateDeleteTab(sc::RefUpdateDeleteTabContext &rCxt, SCTAB nLocalTab)
Definition: rangenam.cxx:402
sal_uInt16 nIndex
Definition: rangenam.hxx:78
SvStream & endl(SvStream &rStr)
OUString aName
SC_DLLPUBLIC 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:1742
void SetRange(const ScSheetLimits &rLimits, const ScRange &rRange, const ScAddress &rPos)
Set a new range, assuming that the ordering of the range matches the ordering of the reference data f...
Definition: refdata.cxx:498
SCROW Row() const
Definition: refdata.cxx:240
void * p
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:400
void SetExtendedErrorDetection(ExtendedErrorDetection eVal)
Definition: compiler.hxx:448
void SetAddress(const ScSheetLimits &rLimits, const ScAddress &rAddr, const ScAddress &rPos)
Definition: refdata.cxx:213
SC_DLLPUBLIC FormulaError GetErrCode() const
Definition: rangenam.cxx:498
bool mbReferenceModified
This flag indicates whether any reference in the token array has been modified.
SC_DLLPUBLIC bool IsValidReference(ScRange &rRef) const
Definition: rangenam.cxx:384
SC_DLLPUBLIC bool IsReference(ScRange &rRef) const
Definition: rangenam.cxx:368
Complex reference (a range) into the sheet.
Definition: refdata.hxx:122
Type eType
Definition: rangenam.hxx:75
void InitCode()
Definition: rangenam.cxx:611
SC_DLLPUBLIC size_t size() const
Definition: rangenam.cxx:799
bool bModified
Definition: rangenam.hxx:79
void UpdateSymbol(OUStringBuffer &rBuffer, const ScAddress &)
Definition: rangenam.cxx:257
SC_DLLPUBLIC ScRefFlags Parse(const OUString &, const ScDocument &, const Details &rDetails=detailsOOOa1, ExternalInfo *pExtInfo=nullptr, const css::uno::Sequence< css::sheet::ExternalLinkInfo > *pExternalLinks=nullptr, sal_Int32 *pSheetEndPos=nullptr, const OUString *pErrRef=nullptr)
Definition: address.cxx:1548
bool CopyAdjustRangeName(SCTAB &rSheet, sal_uInt16 &rIndex, ScRangeData *&rpRangeData, ScDocument &rNewDoc, const ScAddress &rNewPos, const ScAddress &rOldPos, const bool bGlobalNamesToLocal, const bool bUsedByFormula) const
If necessary (name references sheet rOldPos.Tab()) copy and adjust named expression/range from sheet-...
Definition: document10.cxx:784
UpdatedRangeNames maUpdatedNames
StackVar GetType() const
aStr
void ValidateTabRefs()
Definition: rangenam.cxx:518
SC_DLLPUBLIC ScRangeData(ScDocument &rDoc, const OUString &rName, const OUString &rSymbol, const ScAddress &rAdr=ScAddress(), Type nType=Type::Name, const formula::FormulaGrammar::Grammar eGrammar=formula::FormulaGrammar::GRAM_DEFAULT)
Definition: rangenam.cxx:44
SC_DLLPUBLIC bool insert(ScRangeData *p, bool bReuseFreeIndex=true)
Insert object into set.
Definition: rangenam.cxx:809
SC_DLLPUBLIC ~ScRangeData()
Definition: rangenam.cxx:136
sal_uInt16 nPos
sal_Int16 SCTAB
Definition: types.hxx:22
void UpdateReference(sc::RefUpdateContext &rCxt, SCTAB nLocalTab)
Definition: rangenam.cxx:265