13#include <document.hxx>
17#include <tokenarray.hxx>
22#include <oox/token/tokens.hxx>
45 CachedTokenArray(
const CachedTokenArray&) =
delete;
46 const CachedTokenArray& operator=(
const CachedTokenArray&) =
delete;
53 Item(
const Item&) =
delete;
54 const Item& operator=(
const Item&) =
delete;
59 explicit CachedTokenArray(
const ScDocument& rDoc ) :
62 Item*
get(
const ScAddress& rPos, std::u16string_view rFormula )
65 ColCacheType::iterator it =
maCache.find(rPos.
Col());
69 Item& rCached = *it->second;
72 if (rFormula == aPredicted)
80 ColCacheType::iterator it =
maCache.find(rPos.
Col());
84 std::pair<ColCacheType::iterator,bool> r =
85 maCache.emplace(rPos.
Col(), std::make_unique<Item>());
93 Item& rItem = *it->second;
94 rItem.mnRow = rPos.
Row();
99 typedef std::unordered_map<SCCOL, std::unique_ptr<Item>> ColCacheType;
104void applySharedFormulas(
107 std::vector<FormulaBuffer::SharedFormulaEntry>& rSharedFormulas,
108 std::vector<FormulaBuffer::SharedFormulaDesc>& rCells,
109 WorkbookHelper& rWorkbookHelper)
114 for (
const FormulaBuffer::SharedFormulaEntry& rEntry : rSharedFormulas)
116 const ScAddress& aPos = rEntry.maAddress;
117 sal_Int32
nId = rEntry.mnSharedId;
118 const OUString& rTokenStr = rEntry.maTokenStr;
122 std::unique_ptr<ScTokenArray> pArray = aComp.
CompileString(rTokenStr);
126 aGroups.
set(
nId, std::move(pArray), aPos);
133 const bool bGeneratorKnownGood = rWorkbookHelper.isGeneratorKnownGood();
134 bool bHasCalculatedFormulaCells = rWorkbookHelper.hasCalculatedFormulaCells();
136 for (
const FormulaBuffer::SharedFormulaDesc& rDesc : rCells)
154 if (rOrigin.
Col() != aPos.
Col())
162 if (bNeedNumberFormat)
165 if (rDesc.maCellValue.isEmpty())
178 switch (rDesc.mnValueType)
182 if (bNeedNumberFormat)
187 if (rDesc.maCellValue ==
"1" || rDesc.maCellValue ==
"0")
198 const double fVal = rDesc.maCellValue.toDouble();
199 if (!bHasCalculatedFormulaCells && fVal != 0.0)
201 rWorkbookHelper.setCalculatedFormulaCells();
202 bHasCalculatedFormulaCells =
true;
213 if (bGeneratorKnownGood)
236void applyCellFormulas(
238 const Sequence<ExternalLinkInfo>& rExternalLinks,
239 const std::vector<FormulaBuffer::TokenAddressItem>& rCells )
241 for (
const FormulaBuffer::TokenAddressItem& rItem : rCells)
244 CachedTokenArray::Item*
p = rCache.get(aPos, rItem.maTokenStr);
250 if (
p->mnRow + 1 == aPos.
Row())
258 assert(rPrev.
aPos.
Row() ==
p->mnRow);
273 p->mnRow = aPos.
Row();
279 aCompiler.SetNumberFormatter(&rFormatter);
280 aCompiler.SetExternalLinks(rExternalLinks);
281 std::unique_ptr<ScTokenArray> pCode = aCompiler.CompileString(rItem.maTokenStr);
285 aCompiler.CompileTokenArray();
291 rCache.store(aPos, pCell);
295void applyArrayFormulas(
297 const Sequence<ExternalLinkInfo>& rExternalLinks,
298 const std::vector<FormulaBuffer::TokenRangeAddressItem>& rArrays )
300 for (
const FormulaBuffer::TokenRangeAddressItem& rAddressItem : rArrays)
302 const ScAddress& aPos = rAddressItem.maTokenAndAddress.maAddress;
305 aComp.SetNumberFormatter(&rFormatter);
306 aComp.SetExternalLinks(rExternalLinks);
307 std::unique_ptr<ScTokenArray> pArray(aComp.CompileString(rAddressItem.maTokenAndAddress.maTokenStr));
313void applyCellFormulaValues(
314 ScDocumentImport& rDoc,
const std::vector<FormulaBuffer::FormulaValue>& rVector, WorkbookHelper& rWorkbookHelper )
317 const bool bGeneratorKnownGood = rWorkbookHelper.isGeneratorKnownGood();
318 bool bHasCalculatedFormulaCells = rWorkbookHelper.hasCalculatedFormulaCells();
320 for (
const FormulaBuffer::FormulaValue& rValue : rVector)
322 const ScAddress& aCellPos = rValue.maAddress;
324 const OUString& rValueStr = rValue.maValueStr;
328 switch (rValue.mnCellType)
332 const double fVal = rValueStr.toDouble();
333 if (!bHasCalculatedFormulaCells && fVal != 0.0)
335 rWorkbookHelper.setCalculatedFormulaCells();
336 bHasCalculatedFormulaCells =
true;
354 if (bGeneratorKnownGood)
368void processSheetFormulaCells(
370 const Sequence<ExternalLinkInfo>& rExternalLinks, WorkbookHelper& rWorkbookHelper )
372 if (rItem.mpSharedFormulaEntries && rItem.mpSharedFormulaIDs)
373 applySharedFormulas(rDoc, rFormatter, *rItem.mpSharedFormulaEntries,
374 *rItem.mpSharedFormulaIDs, rWorkbookHelper);
376 if (rItem.mpCellFormulas)
378 CachedTokenArray aCache(rDoc.
getDoc());
379 applyCellFormulas(rDoc, aCache, rFormatter, rExternalLinks, *rItem.mpCellFormulas);
382 if (rItem.mpArrayFormulas)
383 applyArrayFormulas(rDoc, rFormatter, rExternalLinks, *rItem.mpArrayFormulas);
385 if (rItem.mpCellFormulaValues)
386 applyCellFormulaValues(rDoc, *rItem.mpCellFormulaValues, rWorkbookHelper);
393 OUString aTokenStr, sal_Int32 nSharedId ) :
394 maAddress(rAddr), maTokenStr(
std::move(aTokenStr)), mnSharedId(nSharedId) {}
397 const ScAddress& rAddr, sal_Int32 nSharedId,
398 OUString aCellValue, sal_Int32 nValueType ) :
399 maAddress(rAddr), maCellValue(
std::move(aCellValue)), mnSharedId(nSharedId),
mnValueType(nValueType) {}
402 mpCellFormulas(nullptr),
403 mpArrayFormulas(nullptr),
404 mpCellFormulaValues(nullptr),
405 mpSharedFormulaEntries(nullptr),
406 mpSharedFormulaIDs(nullptr) {}
432 std::vector<SheetItem> aSheetItems;
433 aSheetItems.reserve(nTabCount);
434 for (
SCTAB nTab = 0; nTab < nTabCount; ++nTab)
447 xFormulaBar->setPosition( 1.0 );
478 sal_Int32 nSharedId,
const OUString& rTokens )
483 rSharedFormulas.push_back( aEntry );
493 const ScAddress& rAddress, sal_Int32 nSharedId,
const OUString& rCellValue, sal_Int32 nValueType )
508 const ScAddress& rAddress,
const OUString& rValueStr, sal_Int32 nCellType )
void SetNumberFormatter(SvNumberFormatter *pFormatter)
std::unique_ptr< ScTokenArray > CompileString(const OUString &rFormula)
Tokenize formula expression string into an array of tokens.
Accessor class to ScDocument.
void setMatrixCells(const ScRange &rRange, const ScTokenArray &rArray, formula::FormulaGrammar::Grammar eGrammar)
void setFormulaCell(const ScAddress &rPos, const OUString &rFormula, formula::FormulaGrammar::Grammar eGrammar, const double *pResult=nullptr)
SC_DLLPUBLIC sal_uInt32 GetNumberFormat(SCCOL nCol, SCROW nRow, SCTAB nTab) const
SC_DLLPUBLIC ScExternalRefManager * GetExternalRefManager() const
SC_DLLPUBLIC void SetAutoNameCache(std::unique_ptr< ScAutoNameCache > pCache)
SC_DLLPUBLIC void SetNumberFormat(const ScAddress &rPos, sal_uInt32 nNumberFormat)
SC_DLLPUBLIC svl::SharedStringPool & GetSharedStringPool()
SC_DLLPUBLIC SvNumberFormatter * GetFormatTable() const
SC_DLLPUBLIC const ScFormulaCell * GetFormulaCell(const ScAddress &rPos) const
SC_DLLPUBLIC SCTAB GetTableCount() const
Use this guard when performing something from the API that might query values from external reference...
void addFilesToLinkManager()
Add all known external files to the LinkManager.
std::unique_ptr< ScTokenArray > Clone() const
OUString CreateString(sc::TokenStringContext &rCxt, const ScAddress &rPos) const
Create a string representation of formula token array without modifying the internal state of the tok...
virtual ISegmentProgressBarRef createSegment(double fLength) override
Helper class to provide access to global workbook data.
ScDocumentImport & getDocImport()
SegmentProgressBar & getProgressBar() const
Returns the filter progress bar.
ExternalLinkBuffer & getExternalLinks() const
Returns the external links read from the external links substream.
const ScAddress & getOrigin() const
const ScTokenArray * getTokenArray() const
SharedString intern(const OUString &rStr)
#define SAL_WARN(area, stream)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
std::shared_ptr< ISegmentProgressBar > ISegmentProgressBarRef
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
Represents a shared formula definition.
SharedFormulaEntry(const ScAddress &rAddress, OUString aTokenStr, sal_Int32 nSharedId)
Context for creating string from an array of formula tokens, used in ScTokenArray::CreateString().
::boost::intrusive_ptr< ScFormulaCellGroup > ScFormulaCellGroupRef
#define SV_COUNTRY_LANGUAGE_OFFSET