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  if (!rRef.Ref1.IsColRel() && !rRef.Ref1.IsRowRel() &&
287  (!rRef.Ref1.IsFlag3D() || !rRef.Ref1.IsTabRel()) &&
288  ( t->GetType() == svSingleRef ||
289  (!rRef.Ref2.IsColRel() && !rRef.Ref2.IsRowRel() &&
290  (!rRef.Ref2.IsFlag3D() || !rRef.Ref2.IsTabRel()))))
291  {
292  ScRange aAbs = rRef.toAbs(rDoc, aPos);
293  if (ScRefUpdate::UpdateTranspose(rDoc, rSource, rDest, aAbs) != UR_NOTHING)
294  {
295  rRef.SetRange(rDoc.GetSheetLimits(), aAbs, aPos);
296  bChanged = true;
297  }
298  }
299  }
300  }
301 
302  bModified = bChanged;
303 }
304 
305 void ScRangeData::UpdateGrow( const ScRange& rArea, SCCOL nGrowX, SCROW nGrowY )
306 {
307  bool bChanged = false;
308 
311 
312  while ( ( t = aIter.GetNextReference() ) != nullptr )
313  {
314  if( t->GetType() != svIndex )
315  {
316  SingleDoubleRefModifier aMod( *t );
317  ScComplexRefData& rRef = aMod.Ref();
318  if (!rRef.Ref1.IsColRel() && !rRef.Ref1.IsRowRel() &&
319  (!rRef.Ref1.IsFlag3D() || !rRef.Ref1.IsTabRel()) &&
320  ( t->GetType() == svSingleRef ||
321  (!rRef.Ref2.IsColRel() && !rRef.Ref2.IsRowRel() &&
322  (!rRef.Ref2.IsFlag3D() || !rRef.Ref2.IsTabRel()))))
323  {
324  ScRange aAbs = rRef.toAbs(rDoc, aPos);
325  if (ScRefUpdate::UpdateGrow(rArea, nGrowX, nGrowY, aAbs) != UR_NOTHING)
326  {
327  rRef.SetRange(rDoc.GetSheetLimits(), aAbs, aPos);
328  bChanged = true;
329  }
330  }
331  }
332  }
333 
334  bModified = bChanged; // has to be evaluated immediately afterwards
335 }
336 
337 bool ScRangeData::operator== (const ScRangeData& rData) const // for Undo
338 {
339  if ( nIndex != rData.nIndex ||
340  aName != rData.aName ||
341  aPos != rData.aPos ||
342  eType != rData.eType ) return false;
343 
344  sal_uInt16 nLen = pCode->GetLen();
345  if ( nLen != rData.pCode->GetLen() ) return false;
346 
347  FormulaToken** ppThis = pCode->GetArray();
348  FormulaToken** ppOther = rData.pCode->GetArray();
349 
350  for ( sal_uInt16 i=0; i<nLen; i++ )
351  if ( ppThis[i] != ppOther[i] && !(*ppThis[i] == *ppOther[i]) )
352  return false;
353 
354  return true;
355 }
356 
357 bool ScRangeData::IsRangeAtBlock( const ScRange& rBlock ) const
358 {
359  bool bRet = false;
360  ScRange aRange;
361  if ( IsReference(aRange) )
362  bRet = ( rBlock == aRange );
363  return bRet;
364 }
365 
366 bool ScRangeData::IsReference( ScRange& rRange ) const
367 {
368  if ( (eType & ( Type::AbsArea | Type::RefArea | Type::AbsPos )) && pCode )
369  return pCode->IsReference(rRange, aPos);
370 
371  return false;
372 }
373 
374 bool ScRangeData::IsReference( ScRange& rRange, const ScAddress& rPos ) const
375 {
376  if ( (eType & ( Type::AbsArea | Type::RefArea | Type::AbsPos ) ) && pCode )
377  return pCode->IsReference(rRange, rPos);
378 
379  return false;
380 }
381 
383 {
384  if ( (eType & ( Type::AbsArea | Type::RefArea | Type::AbsPos ) ) && pCode )
385  return pCode->IsValidReference(rRange, aPos);
386 
387  return false;
388 }
389 
391 {
392  sc::RefUpdateResult aRes = pCode->AdjustReferenceOnInsertedTab(rCxt, aPos);
393  if (aRes.mbReferenceModified)
394  rCxt.maUpdatedNames.setUpdatedName(nLocalTab, nIndex);
395 
396  if (rCxt.mnInsertPos <= aPos.Tab())
397  aPos.IncTab(rCxt.mnSheets);
398 }
399 
401 {
402  sc::RefUpdateResult aRes = pCode->AdjustReferenceOnDeletedTab(rCxt, aPos);
403  if (aRes.mbReferenceModified)
404  rCxt.maUpdatedNames.setUpdatedName(nLocalTab, nIndex);
405 
406  if (rCxt.mnDeletePos <= aPos.Tab())
407  aPos.IncTab(-rCxt.mnSheets);
408 }
409 
411 {
412  sc::RefUpdateResult aRes = pCode->AdjustReferenceOnMovedTab(rCxt, aPos);
413  if (aRes.mbReferenceModified)
414  rCxt.maUpdatedNames.setUpdatedName(nLocalTab, nIndex);
415 
416  aPos.SetTab(rCxt.getNewTab(aPos.Tab()));
417 }
418 
419 void ScRangeData::MakeValidName( const ScDocument& rDoc, OUString& rName )
420 {
421 
422  // strip leading invalid characters
423  sal_Int32 nPos = 0;
424  sal_Int32 nLen = rName.getLength();
425  while ( nPos < nLen && !ScCompiler::IsCharFlagAllConventions( rName, nPos, ScCharFlags::Name) )
426  ++nPos;
427  if ( nPos>0 )
428  rName = rName.copy(nPos);
429 
430  // if the first character is an invalid start character, precede with '_'
431  if ( !rName.isEmpty() && !ScCompiler::IsCharFlagAllConventions( rName, 0, ScCharFlags::CharName ) )
432  rName = "_" + rName;
433 
434  // replace invalid with '_'
435  nLen = rName.getLength();
436  for (nPos=0; nPos<nLen; nPos++)
437  {
439  rName = rName.replaceAt( nPos, 1, "_" );
440  }
441 
442  // Ensure that the proposed name is not a reference under any convention,
443  // same as in IsNameValid()
444  ScAddress aAddr;
445  ScRange aRange;
446  for (int nConv = FormulaGrammar::CONV_UNSPECIFIED; ++nConv < FormulaGrammar::CONV_LAST; )
447  {
448  ScAddress::Details details( static_cast<FormulaGrammar::AddressConvention>( nConv ) );
449  // Don't check Parse on VALID, any partial only VALID may result in
450  // #REF! during compile later!
451  while (aRange.Parse(rName, rDoc, details) != ScRefFlags::ZERO ||
452  aAddr.Parse(rName, rDoc, details) != ScRefFlags::ZERO)
453  {
454  // Range Parse is partially valid also with invalid sheet name,
455  // Address Parse ditto, during compile name would generate a #REF!
456  if ( rName.indexOf( '.' ) != -1 )
457  rName = rName.replaceFirst( ".", "_" );
458  else
459  rName = "_" + rName;
460  }
461  }
462 }
463 
465 {
466  /* XXX If changed, sc/source/filter/ftools/ftools.cxx
467  * ScfTools::ConvertToScDefinedName needs to be changed too. */
468  char const a('.');
469  if (rName.indexOf(a) != -1)
471  sal_Int32 nPos = 0;
472  sal_Int32 nLen = rName.getLength();
473  if ( !nLen || !ScCompiler::IsCharFlagAllConventions( rName, nPos++, ScCharFlags::CharName ) )
475  while ( nPos < nLen )
476  {
479  }
480  ScAddress aAddr;
481  ScRange aRange;
482  for (int nConv = FormulaGrammar::CONV_UNSPECIFIED; ++nConv < FormulaGrammar::CONV_LAST; )
483  {
484  ScAddress::Details details( static_cast<FormulaGrammar::AddressConvention>( nConv ) );
485  // Don't check Parse on VALID, any partial only VALID may result in
486  // #REF! during compile later!
487  if (aRange.Parse(rName, rDoc, details) != ScRefFlags::ZERO ||
488  aAddr.Parse(rName, rDoc, details) != ScRefFlags::ZERO )
489  {
490  return NAME_INVALID_CELL_REF;
491  }
492  }
493  return NAME_VALID;
494 }
495 
497 {
498  return pCode ? pCode->GetCodeError() : FormulaError::NONE;
499 }
500 
502 {
503  return pCode->HasReferences();
504 }
505 
506 sal_uInt32 ScRangeData::GetUnoType() const
507 {
508  sal_uInt32 nUnoType = 0;
509  if ( HasType(Type::Criteria) ) nUnoType |= css::sheet::NamedRangeFlag::FILTER_CRITERIA;
510  if ( HasType(Type::PrintArea) ) nUnoType |= css::sheet::NamedRangeFlag::PRINT_AREA;
513  return nUnoType;
514 }
515 
517 {
518  // try to make sure all relative references and the reference position
519  // are within existing tables, so they can be represented as text
520  // (if the range of used tables is more than the existing tables,
521  // the result may still contain invalid tables, because the relative
522  // references aren't changed so formulas stay the same)
523 
524  // find range of used tables
525 
526  SCTAB nMinTab = aPos.Tab();
527  SCTAB nMaxTab = nMinTab;
530  while ( ( t = aIter.GetNextReference() ) != nullptr )
531  {
532  ScSingleRefData& rRef1 = *t->GetSingleRef();
533  ScAddress aAbs = rRef1.toAbs(rDoc, aPos);
534  if ( rRef1.IsTabRel() && !rRef1.IsTabDeleted() )
535  {
536  if (aAbs.Tab() < nMinTab)
537  nMinTab = aAbs.Tab();
538  if (aAbs.Tab() > nMaxTab)
539  nMaxTab = aAbs.Tab();
540  }
541  if ( t->GetType() == svDoubleRef )
542  {
543  ScSingleRefData& rRef2 = t->GetDoubleRef()->Ref2;
544  aAbs = rRef2.toAbs(rDoc, aPos);
545  if ( rRef2.IsTabRel() && !rRef2.IsTabDeleted() )
546  {
547  if (aAbs.Tab() < nMinTab)
548  nMinTab = aAbs.Tab();
549  if (aAbs.Tab() > nMaxTab)
550  nMaxTab = aAbs.Tab();
551  }
552  }
553  }
554 
555  SCTAB nTabCount = rDoc.GetTableCount();
556  if ( nMaxTab < nTabCount || nMinTab <= 0 )
557  return;
558 
559  // move position and relative tab refs
560  // The formulas that use the name are not changed by this
561 
562  SCTAB nMove = nMinTab;
563  ScAddress aOldPos = aPos;
564  aPos.SetTab( aPos.Tab() - nMove );
565 
566  aIter.Reset();
567  while ( ( t = aIter.GetNextReference() ) != nullptr )
568  {
569  switch (t->GetType())
570  {
571  case svSingleRef:
572  {
573  ScSingleRefData& rRef = *t->GetSingleRef();
574  if (!rRef.IsTabDeleted())
575  {
576  ScAddress aAbs = rRef.toAbs(rDoc, aOldPos);
577  rRef.SetAddress(rDoc.GetSheetLimits(), aAbs, aPos);
578  }
579  }
580  break;
581  case svDoubleRef:
582  {
583  ScComplexRefData& rRef = *t->GetDoubleRef();
584  if (!rRef.Ref1.IsTabDeleted())
585  {
586  ScAddress aAbs = rRef.Ref1.toAbs(rDoc, aOldPos);
587  rRef.Ref1.SetAddress(rDoc.GetSheetLimits(), aAbs, aPos);
588  }
589  if (!rRef.Ref2.IsTabDeleted())
590  {
591  ScAddress aAbs = rRef.Ref2.toAbs(rDoc, aOldPos);
592  rRef.Ref2.SetAddress(rDoc.GetSheetLimits(), aAbs, aPos);
593  }
594  }
595  break;
596  default:
597  ;
598  }
599  }
600 }
601 
603 {
604  pCode.reset(new ScTokenArray( rArr ));
605  pCode->SetFromRangeName(true);
606  InitCode();
607 }
608 
610 {
611  if( pCode->GetCodeError() == FormulaError::NONE )
612  {
614  if( p ) // exact one reference at first
615  {
616  if( p->GetType() == svSingleRef )
618  else
620  }
621  }
622 }
623 
624 extern "C"
625 int ScRangeData_QsortNameCompare( const void* p1, const void* p2 )
626 {
627  return static_cast<int>(ScGlobal::GetCollator()->compareString(
628  (*static_cast<const ScRangeData* const *>(p1))->GetName(),
629  (*static_cast<const ScRangeData* const *>(p2))->GetName() ));
630 }
631 
632 namespace {
633 
637 class MatchByRange
638 {
639  const ScRange& mrRange;
640 public:
641  explicit MatchByRange(const ScRange& rRange) : mrRange(rRange) {}
642  bool operator() (std::pair<OUString const, std::unique_ptr<ScRangeData>> const& r) const
643  {
644  return r.second->IsRangeAtBlock(mrRange);
645  }
646 };
647 
648 }
649 
651 
653 {
654  for (auto const& it : r.m_Data)
655  {
656  m_Data.insert(std::make_pair(it.first, std::make_unique<ScRangeData>(*it.second)));
657  }
658  // std::map was cloned, so each collection needs its own index to data.
659  maIndexToData.resize( r.maIndexToData.size(), nullptr);
660  for (auto const& itr : m_Data)
661  {
662  size_t nPos = itr.second->GetIndex() - 1;
663  if (nPos >= maIndexToData.size())
664  {
665  OSL_FAIL( "ScRangeName copy-ctor: maIndexToData size doesn't fit");
666  maIndexToData.resize(nPos+1, nullptr);
667  }
668  maIndexToData[nPos] = itr.second.get();
669  }
670 }
671 
672 const ScRangeData* ScRangeName::findByRange(const ScRange& rRange) const
673 {
674  DataType::const_iterator itr = std::find_if(
675  m_Data.begin(), m_Data.end(), MatchByRange(rRange));
676  return itr == m_Data.end() ? nullptr : itr->second.get();
677 }
678 
680 {
681  DataType::iterator itr = m_Data.find(rName);
682  return itr == m_Data.end() ? nullptr : itr->second.get();
683 }
684 
685 const ScRangeData* ScRangeName::findByUpperName(const OUString& rName) const
686 {
687  DataType::const_iterator itr = m_Data.find(rName);
688  return itr == m_Data.end() ? nullptr : itr->second.get();
689 }
690 
692 {
693  if (!i)
694  // index should never be zero.
695  return nullptr;
696 
697  size_t nPos = i - 1;
698  return nPos < maIndexToData.size() ? maIndexToData[nPos] : nullptr;
699 }
700 
702 {
703  if (rCxt.meMode == URM_COPY)
704  // Copying cells does not modify named expressions.
705  return;
706 
707  for (auto const& itr : m_Data)
708  {
709  itr.second->UpdateReference(rCxt, nLocalTab);
710  }
711 }
712 
714 {
715  for (auto const& itr : m_Data)
716  {
717  itr.second->UpdateInsertTab(rCxt, nLocalTab);
718  }
719 }
720 
722 {
723  for (auto const& itr : m_Data)
724  {
725  itr.second->UpdateDeleteTab(rCxt, nLocalTab);
726  }
727 }
728 
730 {
731  for (auto const& itr : m_Data)
732  {
733  itr.second->UpdateMoveTab(rCxt, nLocalTab);
734  }
735 }
736 
737 void ScRangeName::UpdateTranspose(const ScRange& rSource, const ScAddress& rDest)
738 {
739  for (auto const& itr : m_Data)
740  {
741  itr.second->UpdateTranspose(rSource, rDest);
742  }
743 }
744 
745 void ScRangeName::UpdateGrow(const ScRange& rArea, SCCOL nGrowX, SCROW nGrowY)
746 {
747  for (auto const& itr : m_Data)
748  {
749  itr.second->UpdateGrow(rArea, nGrowX, nGrowY);
750  }
751 }
752 
754 {
755  for (auto const& itr : m_Data)
756  {
757  itr.second->CompileUnresolvedXML(rCxt);
758  }
759 }
760 
761 void ScRangeName::CopyUsedNames( const SCTAB nLocalTab, const SCTAB nOldTab, const SCTAB nNewTab,
762  const ScDocument& rOldDoc, ScDocument& rNewDoc, const bool bGlobalNamesToLocal ) const
763 {
764  for (auto const& itr : m_Data)
765  {
766  SCTAB nSheet = (nLocalTab < 0) ? nLocalTab : nOldTab;
767  sal_uInt16 nIndex = itr.second->GetIndex();
768  ScAddress aOldPos( itr.second->GetPos());
769  aOldPos.SetTab( nOldTab);
770  ScAddress aNewPos( aOldPos);
771  aNewPos.SetTab( nNewTab);
772  ScRangeData* pRangeData = nullptr;
773  rOldDoc.CopyAdjustRangeName( nSheet, nIndex, pRangeData, rNewDoc, aNewPos, aOldPos, bGlobalNamesToLocal, false);
774  }
775 }
776 
778 {
779  return m_Data.begin();
780 }
781 
783 {
784  return m_Data.end();
785 }
786 
788 {
789  return m_Data.begin();
790 }
791 
793 {
794  return m_Data.end();
795 }
796 
797 size_t ScRangeName::size() const
798 {
799  return m_Data.size();
800 }
801 
802 bool ScRangeName::empty() const
803 {
804  return m_Data.empty();
805 }
806 
807 bool ScRangeName::insert( ScRangeData* p, bool bReuseFreeIndex )
808 {
809  if (!p)
810  return false;
811 
812  if (!p->GetIndex())
813  {
814  // Assign a new index. An index must be unique and is never 0.
815  if (bReuseFreeIndex)
816  {
817  IndexDataType::iterator itr = std::find(
818  maIndexToData.begin(), maIndexToData.end(), static_cast<ScRangeData*>(nullptr));
819  if (itr != maIndexToData.end())
820  {
821  // Empty slot exists. Re-use it.
822  size_t nPos = std::distance(maIndexToData.begin(), itr);
823  p->SetIndex(nPos + 1);
824  }
825  else
826  // No empty slot. Append it to the end.
827  p->SetIndex(maIndexToData.size() + 1);
828  }
829  else
830  {
831  p->SetIndex(maIndexToData.size() + 1);
832  }
833  }
834 
835  OUString aName(p->GetUpperName());
836  erase(aName); // ptr_map won't insert it if a duplicate name exists.
837  pair<DataType::iterator, bool> r =
838  m_Data.insert(std::make_pair(aName, std::unique_ptr<ScRangeData>(p)));
839  if (r.second)
840  {
841  // Data inserted. Store its index for mapping.
842  size_t nPos = p->GetIndex() - 1;
843  if (nPos >= maIndexToData.size())
844  maIndexToData.resize(nPos+1, nullptr);
845  maIndexToData[nPos] = p;
846  }
847  return r.second;
848 }
849 
851 {
852  erase(r.GetUpperName());
853 }
854 
855 void ScRangeName::erase(const OUString& rName)
856 {
857  DataType::iterator itr = m_Data.find(rName);
858  if (itr != m_Data.end())
859  erase(itr);
860 }
861 
862 void ScRangeName::erase(const iterator& itr)
863 {
864  sal_uInt16 nIndex = itr->second->GetIndex();
865  m_Data.erase(itr);
866  OSL_ENSURE( 0 < nIndex && nIndex <= maIndexToData.size(), "ScRangeName::erase: bad index");
867  if (0 < nIndex && nIndex <= maIndexToData.size())
868  maIndexToData[nIndex-1] = nullptr;
869 }
870 
872 {
873  m_Data.clear();
874  maIndexToData.clear();
875 }
876 
878 {
879  return std::equal(m_Data.begin(), m_Data.end(), r.m_Data.begin(), r.m_Data.end(),
880  [](const DataType::value_type& lhs, const DataType::value_type& rhs) {
881  return (lhs.first == rhs.first) && (*lhs.second == *rhs.second);
882  });
883 }
884 
885 /* 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:506
void UpdateDeleteTab(sc::RefUpdateDeleteTabContext &rCxt, SCTAB nLocalTab=-1)
Definition: rangenam.cxx:721
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:753
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:602
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:4536
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:877
static void MakeValidName(const ScDocument &rDoc, OUString &rName)
Definition: rangenam.cxx:419
DataType::iterator iterator
Definition: rangenam.hxx:196
void clear()
Definition: rangenam.cxx:871
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:1153
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:312
bool empty() const
Definition: rangenam.cxx:802
DataType m_Data
Definition: rangenam.hxx:188
virtual const ScSingleRefData * GetSingleRef() const
void UpdateGrow(const ScRange &rArea, SCCOL nGrowX, SCROW nGrowY)
Definition: rangenam.cxx:305
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:761
void UpdateGrow(const ScRange &rArea, SCCOL nGrowX, SCROW nGrowY)
Definition: rangenam.cxx:745
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:777
OUString aName
Definition: rangenam.hxx:69
void UpdateInsertTab(sc::RefUpdateInsertTabContext &rCxt, SCTAB nLocalTab)
Definition: rangenam.cxx:390
void UpdateReference(sc::RefUpdateContext &rCxt, SCTAB nLocalTab=-1)
Definition: rangenam.cxx:701
bool HasReferences() const
Definition: rangenam.cxx:501
std::unique_ptr< ScTokenArray > pCode
Definition: rangenam.hxx:73
void UpdateInsertTab(sc::RefUpdateInsertTabContext &rCxt, SCTAB nLocalTab=-1)
Definition: rangenam.cxx:713
DocumentType eType
bool IsRangeAtBlock(const ScRange &) const
Definition: rangenam.cxx:357
void SetTab(SCTAB nTabP)
Definition: address.hxx:282
SC_DLLPUBLIC ScRangeData * findByUpperName(const OUString &rName)
Definition: rangenam.cxx:679
SC_DLLPUBLIC const ScRangeData * findByRange(const ScRange &rRange) const
Definition: rangenam.cxx:672
SC_DLLPUBLIC const_iterator end() const
Definition: rangenam.cxx:782
bool IsImportingXML() const
Definition: document.hxx:2137
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:737
void SetFlag3D(bool bVal)
Definition: refdata.hxx:89
int ScRangeData_QsortNameCompare(const void *p1, const void *p2)
Definition: rangenam.cxx:625
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:464
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:5059
ScSingleRefData Ref2
Definition: refdata.hxx:125
bool IsRowRel() const
Definition: refdata.hxx:67
void UpdateMoveTab(sc::RefUpdateMoveTabContext &rCxt, SCTAB nLocalTab)
Definition: rangenam.cxx:410
ScAddress aPos
Definition: rangenam.hxx:74
SC_DLLPUBLIC ScRangeData * findByIndex(sal_uInt16 i) const
Definition: rangenam.cxx:691
void erase(const ScRangeData &r)
Definition: rangenam.cxx:850
bool operator==(const ScRangeData &rData) const
Definition: rangenam.cxx:337
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:729
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:400
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:496
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:382
SC_DLLPUBLIC bool IsReference(ScRange &rRef) const
Definition: rangenam.cxx:366
Complex reference (a range) into the sheet.
Definition: refdata.hxx:122
Type eType
Definition: rangenam.hxx:75
void InitCode()
Definition: rangenam.cxx:609
SC_DLLPUBLIC size_t size() const
Definition: rangenam.cxx:797
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:516
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:807
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