LibreOffice Module sc (master)  1
simpleformulacalc.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 <memory>
11 #include <simpleformulacalc.hxx>
12 #include <document.hxx>
13 #include <tokenarray.hxx>
14 #include <interpre.hxx>
15 #include <compiler.hxx>
16 #include <sfx2/linkmgr.hxx>
17 
18 #define DISPLAY_LEN 15
19 
21  const OUString& rFormula, bool bMatrixFormula, formula::FormulaGrammar::Grammar eGram )
22  : mnFormatType(SvNumFormatType::ALL)
23  , mbCalculated(false)
24  , maAddr(rAddr)
25  , mrDoc(rDoc)
26  , maGram(eGram)
27  , mbMatrixResult(false)
28  , mbLimitString(false)
29  , mbMatrixFormula(bMatrixFormula)
30 {
31  // compile already here
32  ScCompiler aComp(mrDoc, maAddr, eGram, true, bMatrixFormula);
33  mpCode = aComp.CompileString(rFormula);
34  if(mpCode->GetCodeError() == FormulaError::NONE && mpCode->GetLen())
35  aComp.CompileTokenArray();
36 }
37 
39 {
40 }
41 
43 {
44  if(mbCalculated)
45  return;
46 
47  mbCalculated = true;
48 
50  if (mbMatrixFormula)
51  aInt.AssertFormulaMatrix();
52 
53  std::unique_ptr<sfx2::LinkManager> pNewLinkMgr( new sfx2::LinkManager(mrDoc.GetDocumentShell()) );
54  aInt.SetLinkManager( pNewLinkMgr.get() );
55 
56  formula::StackVar aIntType = aInt.Interpret();
57  if ( aIntType == formula::svMatrixCell )
58  {
59  ScCompiler aComp(mrDoc, maAddr, maGram);
60  OUStringBuffer aStr;
61  aComp.CreateStringFromToken(aStr, aInt.GetResultToken().get());
62 
63  mbMatrixResult = true;
64 
65  if (mbLimitString)
66  {
67  size_t n = aStr.getLength();
68  for (size_t i = DISPLAY_LEN; i < n; ++i)
69  {
70  if (aStr[i] == ',' || aStr[i] == ';')
71  {
72  aStr.truncate(i);
73  aStr.append("...");
74  break;
75  }
76  }
77  }
78 
79  maMatrixFormulaResult = aStr.makeStringAndClear();
80  }
81  mnFormatType = aInt.GetRetFormatType();
82  maResult.SetToken(aInt.GetResultToken().get());
83 }
84 
86 {
87  Calculate();
88 
89  if (mbMatrixResult)
90  return false;
91 
92  return maResult.IsValue();
93 }
94 
96 {
97  Calculate();
98 
99  return mbMatrixResult;
100 }
101 
103 {
104  Calculate();
105 
106  FormulaError nErr = mpCode->GetCodeError();
107  if (nErr != FormulaError::NONE)
108  return nErr;
109  return maResult.GetResultError();
110 }
111 
113 {
114  Calculate();
115 
116  if ((mpCode->GetCodeError() == FormulaError::NONE) &&
117  maResult.GetResultError() == FormulaError::NONE)
118  return maResult.GetDouble();
119 
120  return 0.0;
121 }
122 
124 {
125  Calculate();
126 
127  if (mbMatrixResult)
128  return svl::SharedString( maMatrixFormulaResult); // string not interned
129 
130  if ((mpCode->GetCodeError() == FormulaError::NONE) &&
131  maResult.GetResultError() == FormulaError::NONE)
132  return maResult.GetString();
133 
135 }
136 
138 {
139  return formula::FormulaTokenArrayPlainIterator(*mpCode).GetNextColRowName() != nullptr;
140 }
141 
143 {
144  return mpCode.get();
145 }
146 
148 {
149  mbLimitString = bLimitString;
150 }
151 
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static SharedString getEmptyString()
svl::SharedString GetString() const
Return string if type formula::svString or formula::svHybridCell or formula::svMatrixCell and upper l...
SC_DLLPUBLIC const ScFormulaCell * GetFormulaCell(const ScAddress &rPos) const
Definition: document.cxx:3723
#define DISPLAY_LEN
void AssertFormulaMatrix()
Definition: interpr4.cxx:4753
sal_Int64 n
std::unique_ptr< ScTokenArray > CompileString(const OUString &rFormula)
Tokenize formula expression string into an array of tokens.
Definition: compiler.cxx:4535
const FormulaToken * CreateStringFromToken(OUString &rFormula, const FormulaToken *pToken)
void SetToken(const formula::FormulaToken *p)
Sets a direct double if token type is formula::svDouble, or mbEmpty if formula::svEmptyCell, else token.
bool IsValue() const
Test for cell result type formula::svDouble, including upper left if formula::svMatrixCell.
int i
SvNumFormatType
FormulaError
formula::FormulaGrammar::Grammar maGram
std::unique_ptr< ScTokenArray > mpCode
ScInterpreterContext & GetNonThreadedContext() const
Definition: document.hxx:612
double GetDouble() const
Return value if type formula::svDouble or formula::svHybridCell or formula::svMatrixCell and upper le...
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1058
ScSimpleFormulaCalculator(ScDocument &rDoc, const ScAddress &rAddr, const OUString &rFormula, bool bMatrixFormula, formula::FormulaGrammar::Grammar eGram=formula::FormulaGrammar::GRAM_DEFAULT)
aStr
svl::SharedString GetString()
void SetLimitString(bool bLimitString)
FormulaError GetResultError() const
Get error code if set or GetCellResultType() is formula::svError or svUnknown, else 0...