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