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 66
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
52
54 aInt.SetLinkManager( &aNewLinkMgr );
55
56 formula::StackVar aIntType = aInt.Interpret();
57 if ( aIntType == formula::svMatrixCell )
58 {
60 OUStringBuffer aStr;
61 aComp.CreateStringFromToken(aStr, aInt.GetResultToken().get());
62
63 mbMatrixResult = true;
64
65 if (mbLimitString)
66 {
69 const sal_Int32 n = aStr.getLength();
70 for (sal_Int32 i = DISPLAY_LEN; i < n; ++i)
71 {
72 const sal_Unicode c = aStr[i];
73 if (c == cCol || c == cRow)
74 {
75 aStr.truncate(i+1);
76 aStr.append("...");
77 break;
78 }
79 }
80 }
81
82 maMatrixFormulaResult = aStr.makeStringAndClear();
83 }
85 maResult.SetToken(aInt.GetResultToken().get());
86}
87
89{
90 Calculate();
91
93 return false;
94
95 return maResult.IsValue();
96}
97
99{
100 Calculate();
101
102 return mbMatrixResult;
103}
104
106{
107 Calculate();
108
109 FormulaError nErr = mpCode->GetCodeError();
110 if (nErr != FormulaError::NONE)
111 return nErr;
112 return maResult.GetResultError();
113}
114
116{
117 Calculate();
118
119 if ((mpCode->GetCodeError() == FormulaError::NONE) &&
120 maResult.GetResultError() == FormulaError::NONE)
121 return maResult.GetDouble();
122
123 return 0.0;
124}
125
127{
128 Calculate();
129
130 if (mbMatrixResult)
131 return svl::SharedString( maMatrixFormulaResult); // string not interned
132
133 if ((mpCode->GetCodeError() == FormulaError::NONE) &&
134 maResult.GetResultError() == FormulaError::NONE)
135 return maResult.GetString();
136
138}
139
141{
143}
144
146{
147 return mpCode.get();
148}
149
151{
152 mbLimitString = bLimitString;
153}
154
155/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< ScTokenArray > CompileString(const OUString &rFormula)
Tokenize formula expression string into an array of tokens.
Definition: compiler.cxx:4691
ScInterpreterContext & GetNonThreadedContext() const
Definition: document.hxx:617
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1083
SC_DLLPUBLIC const ScFormulaCell * GetFormulaCell(const ScAddress &rPos) const
Definition: document.cxx:3714
void SetToken(const formula::FormulaToken *p)
Sets a direct double if token type is formula::svDouble, or mbEmpty if formula::svEmptyCell,...
double GetDouble() const
Return value if type formula::svDouble or formula::svHybridCell or formula::svMatrixCell and upper le...
const svl::SharedString & GetString() const
Return string if type formula::svString or formula::svHybridCell or formula::svMatrixCell and upper l...
bool IsValue() const
Test for cell result type formula::svDouble, including upper left if formula::svMatrixCell.
FormulaError GetResultError() const
Get error code if set or GetCellResultType() is formula::svError or svUnknown, else 0.
void AssertFormulaMatrix()
Definition: interpr4.cxx:4809
formula::StackVar Interpret()
Definition: interpr4.cxx:3972
const formula::FormulaConstTokenRef & GetResultToken() const
Definition: interpre.hxx:1019
SvNumFormatType GetRetFormatType() const
Definition: interpre.hxx:1020
void SetLinkManager(sfx2::LinkManager *pLinkMgr)
Definition: interpre.hxx:1012
svl::SharedString GetString()
std::unique_ptr< ScTokenArray > mpCode
formula::FormulaGrammar::Grammar maGram
void SetLimitString(bool bLimitString)
ScSimpleFormulaCalculator(ScDocument &rDoc, const ScAddress &rAddr, const OUString &rFormula, bool bMatrixFormula, formula::FormulaGrammar::Grammar eGram=formula::FormulaGrammar::GRAM_DEFAULT)
static const OUString & GetNativeSymbol(OpCode eOp)
const FormulaToken * CreateStringFromToken(OUString &rFormula, const FormulaToken *pToken)
static const SharedString & getEmptyString()
FormulaError
sal_Int64 n
aStr
int i
ocArrayRowSep
ocArrayColSep
#define DISPLAY_LEN
sal_uInt16 sal_Unicode
SvNumFormatType