1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
11 #include <simpleformulacalc.hxx>
12 #include <document.hxx>
14 #include <tokenarray.hxx>
15 #include <interpre.hxx>
16 #include <compiler.hxx>
17 #include <sfx2/linkmgr.hxx>
19 #define DISPLAY_LEN 66
21 ScSimpleFormulaCalculator::ScSimpleFormulaCalculator( ScDocument
& rDoc
, const ScAddress
& rAddr
,
22 const OUString
& rFormula
, bool bMatrixFormula
, formula::FormulaGrammar::Grammar eGram
)
23 : mnFormatType(SvNumFormatType::ALL
)
28 , mbMatrixResult(false)
29 , mbLimitString(false)
30 , mbMatrixFormula(bMatrixFormula
)
32 // compile already here
33 ScCompiler
aComp(mrDoc
, maAddr
, eGram
, true, bMatrixFormula
);
34 mpCode
= aComp
.CompileString(rFormula
);
35 if(mpCode
->GetCodeError() == FormulaError::NONE
&& mpCode
->GetLen())
36 aComp
.CompileTokenArray();
39 ScSimpleFormulaCalculator::~ScSimpleFormulaCalculator()
43 void ScSimpleFormulaCalculator::Calculate()
50 ScInterpreter
aInt(mrDoc
.GetFormulaCell( maAddr
), mrDoc
, mrDoc
.GetNonThreadedContext(), maAddr
, *mpCode
);
52 aInt
.AssertFormulaMatrix();
54 sfx2::LinkManager
aNewLinkMgr( mrDoc
.GetDocumentShell() );
55 aInt
.SetLinkManager( &aNewLinkMgr
);
57 formula::StackVar aIntType
= aInt
.Interpret();
58 if ( aIntType
== formula::svMatrixCell
)
60 ScCompiler
aComp(mrDoc
, maAddr
, maGram
);
62 aComp
.CreateStringFromToken(aStr
, aInt
.GetResultToken().get());
64 mbMatrixResult
= true;
68 const sal_Unicode cCol
= ScCompiler::GetNativeSymbol(ocArrayColSep
)[0];
69 const sal_Unicode cRow
= ScCompiler::GetNativeSymbol(ocArrayRowSep
)[0];
70 const sal_Int32 n
= aStr
.getLength();
71 for (sal_Int32 i
= DISPLAY_LEN
; i
< n
; ++i
)
73 const sal_Unicode c
= aStr
[i
];
74 if (c
== cCol
|| c
== cRow
)
83 maMatrixFormulaResult
= aStr
.makeStringAndClear();
85 mnFormatType
= aInt
.GetRetFormatType();
86 maResult
.SetToken(aInt
.GetResultToken().get());
89 bool ScSimpleFormulaCalculator::IsValue()
96 return maResult
.IsValue();
99 bool ScSimpleFormulaCalculator::IsMatrix()
103 return mbMatrixResult
;
106 FormulaError
ScSimpleFormulaCalculator::GetErrCode()
110 FormulaError nErr
= mpCode
->GetCodeError();
111 if (nErr
!= FormulaError::NONE
)
113 return maResult
.GetResultError();
116 double ScSimpleFormulaCalculator::GetValue()
120 if ((mpCode
->GetCodeError() == FormulaError::NONE
) &&
121 maResult
.GetResultError() == FormulaError::NONE
)
122 return maResult
.GetDouble();
127 svl::SharedString
ScSimpleFormulaCalculator::GetString()
132 return svl::SharedString( maMatrixFormulaResult
); // string not interned
134 if ((mpCode
->GetCodeError() == FormulaError::NONE
) &&
135 maResult
.GetResultError() == FormulaError::NONE
)
136 return maResult
.GetString();
138 return svl::SharedString::getEmptyString();
141 bool ScSimpleFormulaCalculator::HasColRowName() const
143 return formula::FormulaTokenArrayPlainIterator(*mpCode
).GetNextColRowName() != nullptr;
146 ScTokenArray
* ScSimpleFormulaCalculator::GetCode()
151 void ScSimpleFormulaCalculator::SetLimitString(bool bLimitString
)
153 mbLimitString
= bLimitString
;
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */