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 void ScCellFormat::GetString( const ScRefCellValue& rCell, sal_uInt32 nFormat, OUString& rString,
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  rFormatter.GetOutputString(rCell.mpString->getString(), nFormat, rString, ppColor, bUseStarFormat);
42  break;
43  case CELLTYPE_EDIT:
44  rFormatter.GetOutputString(rCell.getString(&rDoc), nFormat, rString, ppColor );
45  break;
46  case CELLTYPE_VALUE:
47  {
48  const double & nValue = rCell.mfValue;
49  if (!bNullVals && nValue == 0.0)
50  rString.clear();
51  else
52  rFormatter.GetOutputString( nValue, nFormat, rString, ppColor, bUseStarFormat );
53  }
54  break;
55  case CELLTYPE_FORMULA:
56  {
57  ScFormulaCell* pFCell = rCell.mpFormula;
58  if ( bFormula )
59  {
60  pFCell->GetFormula( rString );
61  }
62  else
63  {
64  // A macro started from the interpreter, which has
65  // access to Formula Cells, becomes a CellText, even if
66  // that triggers further interpretation, except if those
67  // cells are already being interpreted.
68  // IdleCalc generally doesn't trigger further interpretation,
69  // as not to get Err522 (circular).
70  if ( pFCell->GetDocument().IsInInterpreter() &&
72  || pFCell->IsRunning()) )
73  {
74  rString = "...";
75  }
76  else
77  {
78  const FormulaError nErrCode = pFCell->GetErrCode();
79 
80  if (nErrCode != FormulaError::NONE)
81  rString = ScGlobal::GetErrorString(nErrCode);
82  else if ( pFCell->IsEmptyDisplayedAsString() )
83  rString.clear();
84  else if ( pFCell->IsValue() )
85  {
86  double fValue = pFCell->GetValue();
87  if ( !bNullVals && fValue == 0.0 )
88  rString.clear();
89  else
90  rFormatter.GetOutputString( fValue, nFormat, rString, ppColor, bUseStarFormat );
91  }
92  else
93  {
94  rFormatter.GetOutputString( pFCell->GetString().getString(),
95  nFormat, rString, ppColor, bUseStarFormat );
96  }
97  }
98  }
99  }
100  break;
101  default:
102  rString.clear();
103  break;
104  }
105 }
106 
108  ScDocument& rDoc, const ScAddress& rPos, sal_uInt32 nFormat, const Color** ppColor,
109  SvNumberFormatter& rFormatter, bool bNullVals, bool bFormula )
110 {
111  OUString aString;
112  *ppColor = nullptr;
113 
114  ScRefCellValue aCell(rDoc, rPos);
115  GetString(aCell, nFormat, aString, ppColor, rFormatter, rDoc, bNullVals, bFormula);
116  return aString;
117 }
118 
120  const ScRefCellValue& rCell, sal_uInt32 nFormat, OUString& rString, SvNumberFormatter& rFormatter, const ScDocument& rDoc, bool bFiltering )
121 {
122  switch (rCell.meType)
123  {
124  case CELLTYPE_STRING:
125  case CELLTYPE_EDIT:
126  rString = rCell.getString(&rDoc);
127  break;
128  case CELLTYPE_VALUE:
129  rFormatter.GetInputLineString(rCell.mfValue, nFormat, rString, bFiltering);
130  break;
131  case CELLTYPE_FORMULA:
132  {
133  ScFormulaCell* pFC = rCell.mpFormula;
134  if (pFC->IsEmptyDisplayedAsString())
135  rString = EMPTY_OUSTRING;
136  else if (pFC->IsValue())
137  rFormatter.GetInputLineString(pFC->GetValue(), nFormat, rString);
138  else
139  rString = pFC->GetString().getString();
140 
141  const FormulaError nErrCode = pFC->GetErrCode();
142  if (nErrCode != FormulaError::NONE)
143  rString = EMPTY_OUSTRING;
144  }
145  break;
146  default:
147  rString = EMPTY_OUSTRING;
148  break;
149  }
150 }
151 
152 OUString ScCellFormat::GetOutputString( ScDocument& rDoc, const ScAddress& rPos, const ScRefCellValue& rCell )
153 {
154  if (rCell.isEmpty())
155  return EMPTY_OUSTRING;
156 
157  OUString aVal;
158 
159  if (rCell.meType == CELLTYPE_EDIT)
160  {
161  // GetString converts line breaks into spaces in EditCell,
162  // but here we need the line breaks
163  const EditTextObject* pData = rCell.mpEditText;
164  if (pData)
165  {
166  ScFieldEditEngine& rEngine = rDoc.GetEditEngine();
167  rEngine.SetTextCurrentDefaults(*pData);
168  aVal = rEngine.GetText();
169  }
170  // also do not format EditCells as numbers
171  // (fitting to output)
172  }
173  else
174  {
175  // like in GetString for document (column)
176  const Color* pColor;
177  sal_uInt32 nNumFmt = rDoc.GetNumberFormat(rPos);
178  GetString(rCell, nNumFmt, aVal, &pColor, *rDoc.GetFormatTable(), rDoc);
179  }
180  return aVal;
181 }
182 
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString GetText(LineEnd eEnd=LINEEND_LF) const
OUString getString() const
static void GetInputString(const ScRefCellValue &rCell, sal_uInt32 nFormat, OUString &rString, SvNumberFormatter &rFormatter, const ScDocument &rDoc, bool bFiltering=false)
Definition: cellform.cxx:119
#define EMPTY_OUSTRING
Definition: global.hxx:213
bool IsInInterpreter() const
Definition: document.hxx:2322
OUString getString(const ScDocument *pDoc) const
Retrieve string value.
Definition: cellvalue.cxx:660
std::unique_ptr< ContentProperties > pData
bool isEmpty() const
Definition: cellvalue.cxx:670
void GetFormula(OUString &rFormula, const formula::FormulaGrammar::Grammar=formula::FormulaGrammar::GRAM_DEFAULT, const ScInterpreterContext *pContext=nullptr) const
SC_DLLPUBLIC void GetNumberFormat(SCCOL nCol, SCROW nRow, SCTAB nTab, sal_uInt32 &rFormat) const
Definition: document.cxx:3673
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:152
double GetValue()
FormulaError GetErrCode()
ScFormulaCell * mpFormula
Definition: cellvalue.hxx:110
SC_DLLPUBLIC SvNumberFormatter * GetFormatTable() const
Definition: documen2.cxx:441
void SetTextCurrentDefaults(const EditTextObject &rTextObject)
SetText and apply defaults already set.
Definition: editutil.cxx:596
const svl::SharedString * mpString
Definition: cellvalue.hxx:108
sal_uInt16 GetMacroInterpretLevel() const
Definition: document.hxx:2336
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 void GetString(const ScRefCellValue &rCell, sal_uInt32 nFormat, OUString &rString, const Color **ppColor, SvNumberFormatter &rFormatter, const ScDocument &rDoc, bool bNullVals=true, bool bFormula=false, bool bUseStarFormat=false)
Definition: cellform.cxx:32
void GetOutputString(const double &fOutNumber, sal_uInt32 nFIndex, OUString &sOutString, const Color **ppColor, bool bUseStarFormat=false)
static OUString GetErrorString(FormulaError nErrNumber)
Definition: global.cxx:303
SC_DLLPUBLIC ScFieldEditEngine & GetEditEngine()
Definition: documen2.cxx:457
sal_Int16 nValue