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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <cellform.hxx>
21 #include <cellformtmpl.hxx>
23 #include <svl/numformat.hxx>
24 #include <svl/sharedstring.hxx>
25 #include <svl/sharedstringpool.hxx>
27 #include <formulacell.hxx>
28 #include <document.hxx>
29 #include <cellvalue.hxx>
30 #include <formula/errorcodes.hxx>
31 #include <editutil.hxx>
33 OUString
ScCellFormat::GetString( const ScRefCellValue
& rCell
, sal_uInt32 nFormat
,
34 const Color
** ppColor
, ScInterpreterContext
* pContext
, const ScDocument
& rDoc
,
35 bool bNullVals
, bool bFormula
, bool bUseStarFormat
)
39 ScInterpreterContext
& rContext
= pContext
? *pContext
: rDoc
.GetNonThreadedContext();
41 switch (rCell
.getType())
46 rContext
.NFGetOutputString(rCell
.getSharedString()->getString(), nFormat
, str
, ppColor
, bUseStarFormat
);
52 rContext
.NFGetOutputString(rCell
.getString(&rDoc
), nFormat
, str
, ppColor
);
57 const double nValue
= rCell
.getDouble();
58 if (!bNullVals
&& nValue
== 0.0)
63 rContext
.NFGetOutputString( nValue
, nFormat
, str
, ppColor
, bUseStarFormat
);
67 case CELLTYPE_FORMULA
:
69 ScFormulaCell
* pFCell
= rCell
.getFormula();
72 return pFCell
->GetFormula();
76 // A macro started from the interpreter, which has
77 // access to Formula Cells, becomes a CellText, even if
78 // that triggers further interpretation, except if those
79 // cells are already being interpreted.
80 // IdleCalc generally doesn't trigger further interpretation,
81 // as not to get Err522 (circular).
82 if ( pFCell
->GetDocument().IsInInterpreter() &&
83 (!pFCell
->GetDocument().GetMacroInterpretLevel()
84 || pFCell
->IsRunning()) )
90 const FormulaError nErrCode
= pFCell
->GetErrCode();
92 if (nErrCode
!= FormulaError::NONE
)
93 return ScGlobal::GetErrorString(nErrCode
);
94 else if ( pFCell
->IsEmptyDisplayedAsString() )
96 else if ( pFCell
->IsValue() )
98 double fValue
= pFCell
->GetValue();
99 if ( !bNullVals
&& fValue
== 0.0 )
104 rContext
.NFGetOutputString( fValue
, nFormat
, str
, ppColor
, bUseStarFormat
);
111 rContext
.NFGetOutputString( pFCell
->GetString().getString(),
112 nFormat
, str
, ppColor
, bUseStarFormat
);
123 OUString
ScCellFormat::GetString(
124 ScDocument
& rDoc
, const ScAddress
& rPos
, sal_uInt32 nFormat
, const Color
** ppColor
,
125 ScInterpreterContext
* pContext
, bool bNullVals
, bool bFormula
)
129 ScRefCellValue
aCell(rDoc
, rPos
);
130 return GetString(aCell
, nFormat
, ppColor
, pContext
, rDoc
, bNullVals
, bFormula
);
133 OUString
ScCellFormat::GetInputString(
134 const ScRefCellValue
& rCell
, sal_uInt32 nFormat
, ScInterpreterContext
* pContext
, const ScDocument
& rDoc
,
135 bool bFiltering
, bool bForceSystemLocale
)
137 ScInterpreterContext
& rContext
= pContext
? *pContext
: rDoc
.GetNonThreadedContext();
139 switch (rCell
.getType())
141 case CELLTYPE_STRING
:
143 return rCell
.getString(&rDoc
);
145 return rContext
.NFGetInputLineString(rCell
.getDouble(), nFormat
, bFiltering
, bForceSystemLocale
);
147 case CELLTYPE_FORMULA
:
149 std::optional
<OUString
> str
;
150 ScFormulaCell
* pFC
= rCell
.getFormula();
151 if (pFC
->IsEmptyDisplayedAsString())
153 else if (pFC
->IsValue())
154 str
= rContext
.NFGetInputLineString(pFC
->GetValue(), nFormat
, bFiltering
, bForceSystemLocale
);
156 str
= pFC
->GetString().getString();
158 const FormulaError nErrCode
= pFC
->GetErrCode();
159 if (nErrCode
!= FormulaError::NONE
)
162 return str
? std::move(*str
) : svl::SharedString::EMPTY_STRING
;
165 return svl::SharedString::EMPTY_STRING
;
167 return svl::SharedString::EMPTY_STRING
;
171 OUString
ScCellFormat::GetOutputString( ScDocument
& rDoc
, const ScAddress
& rPos
, const ScRefCellValue
& rCell
)
176 if (rCell
.getType() == CELLTYPE_EDIT
)
178 // GetString converts line breaks into spaces in EditCell,
179 // but here we need the line breaks
180 const EditTextObject
* pData
= rCell
.getEditText();
183 ScFieldEditEngine
& rEngine
= rDoc
.GetEditEngine();
184 rEngine
.SetTextCurrentDefaults(*pData
);
185 return rEngine
.GetText();
187 // also do not format EditCells as numbers
188 // (fitting to output)
193 // like in GetString for document (column)
195 sal_uInt32 nNumFmt
= rDoc
.GetNumberFormat(ScRange(rPos
));
196 return GetString(rCell
, nNumFmt
, &pColor
, nullptr, rDoc
);
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */