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>
28 #include <formula/errorcodes.hxx>
29 #include <editutil.hxx>
30 
31 OUString 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.meType)
38  {
39  case CELLTYPE_STRING:
40  {
41  OUString str;
42  rFormatter.GetOutputString(rCell.mpString->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.mfValue;
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  }
63  case CELLTYPE_FORMULA:
64  {
65  ScFormulaCell* pFCell = rCell.mpFormula;
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.meType)
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.mfValue, nFormat, str, bFiltering, bForceSystemLocale);
144  return str;
145  }
146  break;
147  case CELLTYPE_FORMULA:
148  {
149  OUString str;
150  ScFormulaCell* pFC = rCell.mpFormula;
151  if (pFC->IsEmptyDisplayedAsString())
152  ; // empty
153  else if (pFC->IsValue())
154  rFormatter.GetInputLineString(pFC->GetValue(), nFormat, str, bFiltering, bForceSystemLocale);
155  else
156  {
157  const svl::SharedString& shared = pFC->GetString();
158  // Allow callers to optimize by avoiding converting later back to OUString.
159  // To avoid refcounting that won't be needed, do not even return the OUString.
160  if( pShared != nullptr )
161  *pShared = &shared;
162  else
163  str = shared.getString();
164  }
165 
166  const FormulaError nErrCode = pFC->GetErrCode();
167  if (nErrCode != FormulaError::NONE)
168  {
169  str.clear();
170  if( pShared != nullptr )
171  *pShared = nullptr;
172  }
173 
174  return str;
175  }
176  case CELLTYPE_NONE:
177  if( pShared != nullptr )
178  *pShared = &svl::SharedString::getEmptyString();
179  return OUString();
180  default:
181  return OUString();
182  }
183 }
184 
185 OUString ScCellFormat::GetOutputString( ScDocument& rDoc, const ScAddress& rPos, const ScRefCellValue& rCell )
186 {
187  if (rCell.isEmpty())
188  return OUString();
189 
190  if (rCell.meType == CELLTYPE_EDIT)
191  {
192  // GetString converts line breaks into spaces in EditCell,
193  // but here we need the line breaks
194  const EditTextObject* pData = rCell.mpEditText;
195  if (pData)
196  {
197  ScFieldEditEngine& rEngine = rDoc.GetEditEngine();
198  rEngine.SetTextCurrentDefaults(*pData);
199  return rEngine.GetText();
200  }
201  // also do not format EditCells as numbers
202  // (fitting to output)
203  return OUString();
204  }
205  else
206  {
207  // like in GetString for document (column)
208  const Color* pColor;
209  sal_uInt32 nNumFmt = rDoc.GetNumberFormat(rPos);
210  return GetString(rCell, nNumFmt, &pColor, *rDoc.GetFormatTable(), rDoc);
211  }
212 }
213 
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString GetText(LineEnd eEnd=LINEEND_LF) const
OUString getString() const
bool IsInInterpreter() const
Definition: document.hxx:2403
OUString getString(const ScDocument *pDoc) const
Retrieve string value.
Definition: cellvalue.cxx:660
OUString GetFormula(const formula::FormulaGrammar::Grammar=formula::FormulaGrammar::GRAM_DEFAULT, const ScInterpreterContext *pContext=nullptr) const
bool isEmpty() const
Definition: cellvalue.cxx:670
std::unique_ptr< sal_Int32[]> pData
static const SharedString & getEmptyString()
This is very similar to ScCellValue, except that it references the original value instead of copying ...
Definition: cellvalue.hxx:103
ScDocument & GetDocument() const
const EditTextObject * mpEditText
Definition: cellvalue.hxx:109
static OUString GetOutputString(ScDocument &rDoc, const ScAddress &rPos, const ScRefCellValue &rCell)
Definition: cellform.cxx:185
double GetValue()
FormulaError GetErrCode()
ScFormulaCell * mpFormula
Definition: cellvalue.hxx:110
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 SvNumberFormatter * GetFormatTable() const
Definition: documen2.cxx:459
void SetTextCurrentDefaults(const EditTextObject &rTextObject)
SetText and apply defaults already set.
Definition: editutil.cxx:616
const svl::SharedString * mpString
Definition: cellvalue.hxx:108
sal_uInt16 GetMacroInterpretLevel() const
Definition: document.hxx:2417
bool IsRunning() const
bool IsEmptyDisplayedAsString()
const svl::SharedString & GetString()
FormulaError
CellType meType
Definition: cellvalue.hxx:105
void GetInputLineString(const double &fOutNumber, sal_uInt32 nFIndex, OUString &rOutString, bool bFiltering=false, bool bForceSystemLocale=false)
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
void GetOutputString(const double &fOutNumber, sal_uInt32 nFIndex, OUString &sOutString, const Color **ppColor, bool bUseStarFormat=false)
static OUString GetErrorString(FormulaError nErrNumber)
Definition: global.cxx:311
SC_DLLPUBLIC ScFieldEditEngine & GetEditEngine()
Definition: documen2.cxx:475
sal_Int16 nValue
SC_DLLPUBLIC sal_uInt32 GetNumberFormat(SCCOL nCol, SCROW nRow, SCTAB nTab) const
Definition: document.cxx:3677