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