1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <memory>
#include <simpleformulacalc.hxx>
#include <document.hxx>
#include <tokenarray.hxx>
#include <interpre.hxx>
#include <compiler.hxx>
#include <sfx2/linkmgr.hxx>
#define DISPLAY_LEN 66
ScSimpleFormulaCalculator::ScSimpleFormulaCalculator( ScDocument& rDoc, const ScAddress& rAddr,
const OUString& rFormula, bool bMatrixFormula, formula::FormulaGrammar::Grammar eGram )
: mnFormatType(SvNumFormatType::ALL)
, mbCalculated(false)
, maAddr(rAddr)
, mrDoc(rDoc)
, maGram(eGram)
, mbMatrixResult(false)
, mbLimitString(false)
, mbMatrixFormula(bMatrixFormula)
{
// compile already here
ScCompiler aComp(mrDoc, maAddr, eGram, true, bMatrixFormula);
mpCode = aComp.CompileString(rFormula);
if(mpCode->GetCodeError() == FormulaError::NONE && mpCode->GetLen())
aComp.CompileTokenArray();
}
ScSimpleFormulaCalculator::~ScSimpleFormulaCalculator()
{
}
void ScSimpleFormulaCalculator::Calculate()
{
if(mbCalculated)
return;
mbCalculated = true;
ScInterpreter aInt(mrDoc.GetFormulaCell( maAddr ), mrDoc, mrDoc.GetNonThreadedContext(), maAddr, *mpCode);
if (mbMatrixFormula)
aInt.AssertFormulaMatrix();
sfx2::LinkManager aNewLinkMgr( mrDoc.GetDocumentShell() );
aInt.SetLinkManager( &aNewLinkMgr );
formula::StackVar aIntType = aInt.Interpret();
if ( aIntType == formula::svMatrixCell )
{
ScCompiler aComp(mrDoc, maAddr, maGram);
OUStringBuffer aStr;
aComp.CreateStringFromToken(aStr, aInt.GetResultToken().get());
mbMatrixResult = true;
if (mbLimitString)
{
const sal_Unicode cCol = ScCompiler::GetNativeSymbol(ocArrayColSep)[0];
const sal_Unicode cRow = ScCompiler::GetNativeSymbol(ocArrayRowSep)[0];
const sal_Int32 n = aStr.getLength();
for (sal_Int32 i = DISPLAY_LEN; i < n; ++i)
{
const sal_Unicode c = aStr[i];
if (c == cCol || c == cRow)
{
aStr.truncate(i+1);
aStr.append("...");
break;
}
}
}
maMatrixFormulaResult = aStr.makeStringAndClear();
}
mnFormatType = aInt.GetRetFormatType();
maResult.SetToken(aInt.GetResultToken().get());
}
bool ScSimpleFormulaCalculator::IsValue()
{
Calculate();
if (mbMatrixResult)
return false;
return maResult.IsValue();
}
bool ScSimpleFormulaCalculator::IsMatrix()
{
Calculate();
return mbMatrixResult;
}
FormulaError ScSimpleFormulaCalculator::GetErrCode()
{
Calculate();
FormulaError nErr = mpCode->GetCodeError();
if (nErr != FormulaError::NONE)
return nErr;
return maResult.GetResultError();
}
double ScSimpleFormulaCalculator::GetValue()
{
Calculate();
if ((mpCode->GetCodeError() == FormulaError::NONE) &&
maResult.GetResultError() == FormulaError::NONE)
return maResult.GetDouble();
return 0.0;
}
svl::SharedString ScSimpleFormulaCalculator::GetString()
{
Calculate();
if (mbMatrixResult)
return svl::SharedString( maMatrixFormulaResult); // string not interned
if ((mpCode->GetCodeError() == FormulaError::NONE) &&
maResult.GetResultError() == FormulaError::NONE)
return maResult.GetString();
return svl::SharedString::getEmptyString();
}
bool ScSimpleFormulaCalculator::HasColRowName() const
{
return formula::FormulaTokenArrayPlainIterator(*mpCode).GetNextColRowName() != nullptr;
}
ScTokenArray* ScSimpleFormulaCalculator::GetCode()
{
return mpCode.get();
}
void ScSimpleFormulaCalculator::SetLimitString(bool bLimitString)
{
mbLimitString = bLimitString;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|