LibreOffice Module sc (master) 1
cellform.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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <cellform.hxx>
21
22#include <svl/numformat.hxx>
23#include <svl/sharedstring.hxx>
24
25#include <formulacell.hxx>
26#include <document.hxx>
27#include <cellvalue.hxx>
29#include <editutil.hxx>
30
31OUString ScCellFormat::GetString( const ScRefCellValue& rCell, sal_uInt32 nFormat,
32 const Color** ppColor, SvNumberFormatter& rFormatter, const ScDocument& rDoc,
33 bool bNullVals, bool bFormula, bool bUseStarFormat )
34{
35 *ppColor = nullptr;
36
37 switch (rCell.getType())
38 {
39 case CELLTYPE_STRING:
40 {
41 OUString str;
42 rFormatter.GetOutputString(rCell.getSharedString()->getString(), nFormat, str, ppColor, bUseStarFormat);
43 return str;
44 }
45 case CELLTYPE_EDIT:
46 {
47 OUString str;
48 rFormatter.GetOutputString(rCell.getString(&rDoc), nFormat, str, ppColor );
49 return str;
50 }
51 case CELLTYPE_VALUE:
52 {
53 const double nValue = rCell.getDouble();
54 if (!bNullVals && nValue == 0.0)
55 return OUString();
56 else
57 {
58 OUString str;
59 rFormatter.GetOutputString( nValue, nFormat, str, ppColor, bUseStarFormat );
60 return str;
61 }
62 }
64 {
65 ScFormulaCell* pFCell = rCell.getFormula();
66 if ( bFormula )
67 {
68 return pFCell->GetFormula();
69 }
70 else
71 {
72 // A macro started from the interpreter, which has
73 // access to Formula Cells, becomes a CellText, even if
74 // that triggers further interpretation, except if those
75 // cells are already being interpreted.
76 // IdleCalc generally doesn't trigger further interpretation,
77 // as not to get Err522 (circular).
78 if ( pFCell->GetDocument().IsInInterpreter() &&
80 || pFCell->IsRunning()) )
81 {
82 return "...";
83 }
84 else
85 {
86 const FormulaError nErrCode = pFCell->GetErrCode();
87
88 if (nErrCode != FormulaError::NONE)
89 return ScGlobal::GetErrorString(nErrCode);
90 else if ( pFCell->IsEmptyDisplayedAsString() )
91 return OUString();
92 else if ( pFCell->IsValue() )
93 {
94 double fValue = pFCell->GetValue();
95 if ( !bNullVals && fValue == 0.0 )
96 return OUString();
97 else
98 {
99 OUString str;
100 rFormatter.GetOutputString( fValue, nFormat, str, ppColor, bUseStarFormat );
101 return str;
102 }
103 }
104 else
105 {
106 OUString str;
107 rFormatter.GetOutputString( pFCell->GetString().getString(),
108 nFormat, str, ppColor, bUseStarFormat );
109 return str;
110 }
111 }
112 }
113 }
114 default:
115 return OUString();
116 }
117}
118
120 ScDocument& rDoc, const ScAddress& rPos, sal_uInt32 nFormat, const Color** ppColor,
121 SvNumberFormatter& rFormatter, bool bNullVals, bool bFormula )
122{
123 *ppColor = nullptr;
124
125 ScRefCellValue aCell(rDoc, rPos);
126 return GetString(aCell, nFormat, ppColor, rFormatter, rDoc, bNullVals, bFormula);
127}
128
130 const ScRefCellValue& rCell, sal_uInt32 nFormat, SvNumberFormatter& rFormatter, const ScDocument& rDoc,
131 const svl::SharedString** pShared, bool bFiltering, bool bForceSystemLocale )
132{
133 if(pShared != nullptr)
134 *pShared = nullptr;
135 switch (rCell.getType())
136 {
137 case CELLTYPE_STRING:
138 case CELLTYPE_EDIT:
139 return rCell.getString(&rDoc);
140 case CELLTYPE_VALUE:
141 {
142 OUString str;
143 rFormatter.GetInputLineString(rCell.getDouble(), nFormat, str, bFiltering, bForceSystemLocale);
144 return str;
145 }
146 break;
147 case CELLTYPE_FORMULA:
148 {
149 std::optional<OUString> str;
150 ScFormulaCell* pFC = rCell.getFormula();
151 if (pFC->IsEmptyDisplayedAsString())
152 ; // empty
153 else if (pFC->IsValue())
154 {
155 str.emplace();
156 rFormatter.GetInputLineString(pFC->GetValue(), nFormat, *str, bFiltering, bForceSystemLocale);
157 }
158 else
159 {
160 const svl::SharedString& shared = pFC->GetString();
161 // Allow callers to optimize by avoiding converting later back to OUString.
162 // To avoid refcounting that won't be needed, do not even return the OUString.
163 if( pShared != nullptr )
164 *pShared = &shared;
165 else
166 str = shared.getString();
167 }
168
169 const FormulaError nErrCode = pFC->GetErrCode();
170 if (nErrCode != FormulaError::NONE)
171 {
172 str.reset();
173 if( pShared != nullptr )
174 *pShared = nullptr;
175 }
176
177 return str ? std::move(*str) : svl::SharedString::EMPTY_STRING;
178 }
179 case CELLTYPE_NONE:
180 if( pShared != nullptr )
183 default:
185 }
186}
187
188OUString ScCellFormat::GetOutputString( ScDocument& rDoc, const ScAddress& rPos, const ScRefCellValue& rCell )
189{
190 if (rCell.isEmpty())
191 return OUString();
192
193 if (rCell.getType() == CELLTYPE_EDIT)
194 {
195 // GetString converts line breaks into spaces in EditCell,
196 // but here we need the line breaks
197 const EditTextObject* pData = rCell.getEditText();
198 if (pData)
199 {
200 ScFieldEditEngine& rEngine = rDoc.GetEditEngine();
202 return rEngine.GetText();
203 }
204 // also do not format EditCells as numbers
205 // (fitting to output)
206 return OUString();
207 }
208 else
209 {
210 // like in GetString for document (column)
211 const Color* pColor;
212 sal_uInt32 nNumFmt = rDoc.GetNumberFormat(rPos);
213 return GetString(rCell, nNumFmt, &pColor, *rDoc.GetFormatTable(), rDoc);
214 }
215}
216
217/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString GetText(LineEnd eEnd=LINEEND_LF) const
static OUString GetOutputString(ScDocument &rDoc, const ScAddress &rPos, const ScRefCellValue &rCell)
Definition: cellform.cxx:188
static OUString GetInputString(const ScRefCellValue &rCell, sal_uInt32 nFormat, SvNumberFormatter &rFormatter, const ScDocument &rDoc, const svl::SharedString **pShared=nullptr, bool bFiltering=false, bool bForceSystemLocale=false)
Definition: cellform.cxx:129
static OUString GetString(const ScRefCellValue &rCell, sal_uInt32 nFormat, const Color **ppColor, SvNumberFormatter &rFormatter, const ScDocument &rDoc, bool bNullVals=true, bool bFormula=false, bool bUseStarFormat=false)
Definition: cellform.cxx:31
SC_DLLPUBLIC sal_uInt32 GetNumberFormat(SCCOL nCol, SCROW nRow, SCTAB nTab) const
Definition: document.cxx:3685
SC_DLLPUBLIC ScFieldEditEngine & GetEditEngine()
Definition: documen2.cxx:477
bool IsInInterpreter() const
Definition: document.hxx:2405
SC_DLLPUBLIC SvNumberFormatter * GetFormatTable() const
Definition: documen2.cxx:461
sal_uInt16 GetMacroInterpretLevel() const
Definition: document.hxx:2419
void SetTextCurrentDefaults(const EditTextObject &rTextObject)
SetText and apply defaults already set.
Definition: editutil.cxx:616
bool IsEmptyDisplayedAsString()
bool IsRunning() const
double GetValue()
const svl::SharedString & GetString()
FormulaError GetErrCode()
ScDocument & GetDocument() const
OUString GetFormula(const formula::FormulaGrammar::Grammar=formula::FormulaGrammar::GRAM_DEFAULT, const ScInterpreterContext *pContext=nullptr) const
static OUString GetErrorString(FormulaError nErrNumber)
Definition: global.cxx:315
void GetOutputString(const double &fOutNumber, sal_uInt32 nFIndex, OUString &sOutString, const Color **ppColor, bool bUseStarFormat=false)
void GetInputLineString(const double &fOutNumber, sal_uInt32 nFIndex, OUString &rOutString, bool bFiltering=false, bool bForceSystemLocale=false)
static const OUString EMPTY_STRING
const OUString & getString() const
static const SharedString & getEmptyString()
FormulaError
sal_Int16 nValue
@ CELLTYPE_EDIT
Definition: global.hxx:276
@ CELLTYPE_STRING
Definition: global.hxx:274
@ CELLTYPE_FORMULA
Definition: global.hxx:275
@ CELLTYPE_NONE
Definition: global.hxx:272
@ CELLTYPE_VALUE
Definition: global.hxx:273
std::unique_ptr< sal_Int32[]> pData
This is very similar to ScCellValue, except that it references the original value instead of copying ...
Definition: cellvalue.hxx:108
ScFormulaCell * getFormula() const
Definition: cellvalue.hxx:137
const EditTextObject * getEditText() const
Definition: cellvalue.hxx:136
double getDouble() const
Definition: cellvalue.hxx:134
bool isEmpty() const
Definition: cellvalue.cxx:667
OUString getString(const ScDocument *pDoc) const
Retrieve string value.
Definition: cellvalue.cxx:657
const svl::SharedString * getSharedString() const
Definition: cellvalue.hxx:135
CellType getType() const
Definition: cellvalue.hxx:133