LibreOffice Module sc (master)  1
tokenstringcontext.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 
10 #include <tokenstringcontext.hxx>
11 #include <compiler.hxx>
12 #include <document.hxx>
13 #include <dbdata.hxx>
14 #include <externalrefmgr.hxx>
15 #include <globstr.hrc>
16 #include <scresid.hxx>
17 
18 using namespace com::sun::star;
19 
20 namespace sc {
21 
22 namespace {
23 
24 void insertAllNames( TokenStringContext::IndexNameMapType& rMap, const ScRangeName& rNames )
25 {
26  for (auto const& it : rNames)
27  {
28  const ScRangeData *const pData = it.second.get();
29  rMap.emplace(pData->GetIndex(), pData->GetName());
30  }
31 }
32 
33 }
34 
35 TokenStringContext::TokenStringContext( const ScDocument* pDoc, formula::FormulaGrammar::Grammar eGram ) :
36  meGram(eGram),
37  mpRefConv(ScCompiler::GetRefConvention(formula::FormulaGrammar::extractRefConvention(eGram)))
38 {
39  ScCompiler aComp(nullptr, ScAddress());
41  if (mxOpCodeMap)
42  maErrRef = mxOpCodeMap->getSymbol(ocErrRef);
43  else
44  {
45  assert(!"TokenStringContext - no OpCodeMap?!?");
46  maErrRef = ScResId(STR_NO_REF_TABLE);
47  }
48 
49  if (!pDoc)
50  return;
51 
52  // Fetch all sheet names.
53  maTabNames = pDoc->GetAllTableNames();
54  {
55  for (auto& rTabName : maTabNames)
57  }
58 
59  // Fetch all named range names.
60  const ScRangeName* pNames = pDoc->GetRangeName();
61  if (pNames)
62  // global names
63  insertAllNames(maGlobalRangeNames, *pNames);
64 
65  {
66  ScRangeName::TabNameCopyMap aTabRangeNames;
67  pDoc->GetAllTabRangeNames(aTabRangeNames);
68  for (const auto& [nTab, pSheetNames] : aTabRangeNames)
69  {
70  if (!pSheetNames)
71  continue;
72 
73  IndexNameMapType aNames;
74  insertAllNames(aNames, *pSheetNames);
75  maSheetRangeNames.emplace(nTab, aNames);
76  }
77  }
78 
79  // Fetch all named database ranges names.
80  const ScDBCollection* pDBs = pDoc->GetDBCollection();
81  if (pDBs)
82  {
83  const ScDBCollection::NamedDBs& rNamedDBs = pDBs->getNamedDBs();
84  for (const auto& rxNamedDB : rNamedDBs)
85  {
86  const ScDBData& rData = *rxNamedDB;
87  maNamedDBs.emplace(rData.GetIndex(), rData.GetName());
88  }
89  }
90 
91  // Fetch all relevant bits for external references.
92  if (!pDoc->HasExternalRefManager())
93  return;
94 
95  const ScExternalRefManager* pRefMgr = pDoc->GetExternalRefManager();
97  for (size_t i = 0, n = maExternalFileNames.size(); i < n; ++i)
98  {
99  sal_uInt16 nFileId = static_cast<sal_uInt16>(i);
100  std::vector<OUString> aTabNames;
101  pRefMgr->getAllCachedTableNames(nFileId, aTabNames);
102  if (!aTabNames.empty())
103  maExternalCachedTabNames.emplace(nFileId, aTabNames);
104  }
105 }
106 
108  mpDoc(pDoc), meGram(pDoc->GetGrammar())
109 {
110  updateTabNames();
111 }
112 
114  mpDoc(pDoc), meGram(eGram)
115 {
116  updateTabNames();
117 }
118 
120 {
121  // Fetch all sheet names.
123  {
124  for (auto& rTabName : maTabNames)
126  }
127 }
128 
130 {
131  bool bUpdate = (meGram != eGram);
132  meGram = eGram;
133  if (bUpdate)
134  updateTabNames();
135 }
136 
137 }
138 
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool HasExternalRefManager() const
Definition: document.hxx:1006
CompileFormulaContext(ScDocument *pDoc)
SC_DLLPUBLIC ScDBCollection * GetDBCollection() const
Definition: document.hxx:819
std::unique_ptr< ContentProperties > pData
formula::FormulaGrammar::Grammar meGram
sal_uInt16 GetIndex() const
Definition: rangenam.hxx:117
SC_DLLPUBLIC std::vector< OUString > GetAllTableNames() const
Definition: document.cxx:286
SC_DLLPUBLIC ScRangeName * GetRangeName(SCTAB nTab) const
Definition: documen3.cxx:168
std::vector< OUString > getAllCachedExternalFileNames() const
Get all cached external file names as an array.
sal_Int64 n
const ContentProperties & rData
::std::map< SCTAB, const ScRangeName * > TabNameCopyMap
Map that stores non-managed pointers to ScRangeName instances.
Definition: rangenam.hxx:194
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
std::vector< OUString > maTabNames
formula::FormulaCompiler::OpCodeMapPtr mxOpCodeMap
SC_DLLPUBLIC ScExternalRefManager * GetExternalRefManager() const
Definition: documen3.cxx:604
static AddressConvention extractRefConvention(const Grammar eGrammar)
void GetName(OUString &rName) const
Definition: rangenam.hxx:111
sal_uInt16 GetIndex() const
Definition: dbdata.hxx:135
OpCodeMapPtr GetOpCodeMap(const sal_Int32 nLanguage) const
std::unordered_map< sal_uInt16, OUString > IndexNameMapType
std::vector< OUString > maTabNames
static sal_Int32 extractFormulaLanguage(const Grammar eGrammar)
int i
OUString ScResId(const char *pId)
Definition: scdll.cxx:95
const OUString & GetName() const
Definition: dbdata.hxx:122
std::vector< OUString > maExternalFileNames
void getAllCachedTableNames(sal_uInt16 nFileId,::std::vector< OUString > &rTabNames) const
Returns a vector containing all (real) table names and cache tables of the specified file...
IndexNameMapType maGlobalRangeNames
void setGrammar(formula::FormulaGrammar::Grammar eGram)
TabIndexMapType maSheetRangeNames
Stores global named database ranges.
Definition: dbdata.hxx:235
IndexNamesMapType maExternalCachedTabNames
NamedDBs & getNamedDBs()
Definition: dbdata.hxx:315
ocErrRef
static void CheckTabQuotes(OUString &aTabName, const formula::FormulaGrammar::AddressConvention eConv=formula::FormulaGrammar::CONV_OOO)
all
Definition: compiler.cxx:1883
SC_DLLPUBLIC void GetAllTabRangeNames(ScRangeName::TabNameCopyMap &rRangeNames) const
Get all range names that are local to each table.
Definition: documen3.cxx:102