pyuno: call target only with internal python
[LibreOffice.git] / sc / source / core / tool / cellform.cxx
blobd2933814ce8d2ff71d403d1435722277b27d3044
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <cellform.hxx>
21 #include <cellformtmpl.hxx>
23 #include <svl/numformat.hxx>
24 #include <svl/sharedstring.hxx>
25 #include <svl/sharedstringpool.hxx>
27 #include <formulacell.hxx>
28 #include <document.hxx>
29 #include <cellvalue.hxx>
30 #include <formula/errorcodes.hxx>
31 #include <editutil.hxx>
33 OUString ScCellFormat::GetString( const ScRefCellValue& rCell, sal_uInt32 nFormat,
34 const Color** ppColor, ScInterpreterContext* pContext, const ScDocument& rDoc,
35 bool bNullVals, bool bFormula, bool bUseStarFormat )
37 *ppColor = nullptr;
39 ScInterpreterContext& rContext = pContext ? *pContext : rDoc.GetNonThreadedContext();
41 switch (rCell.getType())
43 case CELLTYPE_STRING:
45 OUString str;
46 rContext.NFGetOutputString(rCell.getSharedString()->getString(), nFormat, str, ppColor, bUseStarFormat);
47 return str;
49 case CELLTYPE_EDIT:
51 OUString str;
52 rContext.NFGetOutputString(rCell.getString(&rDoc), nFormat, str, ppColor );
53 return str;
55 case CELLTYPE_VALUE:
57 const double nValue = rCell.getDouble();
58 if (!bNullVals && nValue == 0.0)
59 return OUString();
60 else
62 OUString str;
63 rContext.NFGetOutputString( nValue, nFormat, str, ppColor, bUseStarFormat );
64 return str;
67 case CELLTYPE_FORMULA:
69 ScFormulaCell* pFCell = rCell.getFormula();
70 if ( bFormula )
72 return pFCell->GetFormula();
74 else
76 // A macro started from the interpreter, which has
77 // access to Formula Cells, becomes a CellText, even if
78 // that triggers further interpretation, except if those
79 // cells are already being interpreted.
80 // IdleCalc generally doesn't trigger further interpretation,
81 // as not to get Err522 (circular).
82 if ( pFCell->GetDocument().IsInInterpreter() &&
83 (!pFCell->GetDocument().GetMacroInterpretLevel()
84 || pFCell->IsRunning()) )
86 return u"..."_ustr;
88 else
90 const FormulaError nErrCode = pFCell->GetErrCode();
92 if (nErrCode != FormulaError::NONE)
93 return ScGlobal::GetErrorString(nErrCode);
94 else if ( pFCell->IsEmptyDisplayedAsString() )
95 return OUString();
96 else if ( pFCell->IsValue() )
98 double fValue = pFCell->GetValue();
99 if ( !bNullVals && fValue == 0.0 )
100 return OUString();
101 else
103 OUString str;
104 rContext.NFGetOutputString( fValue, nFormat, str, ppColor, bUseStarFormat );
105 return str;
108 else
110 OUString str;
111 rContext.NFGetOutputString( pFCell->GetString().getString(),
112 nFormat, str, ppColor, bUseStarFormat );
113 return str;
118 default:
119 return OUString();
123 OUString ScCellFormat::GetString(
124 ScDocument& rDoc, const ScAddress& rPos, sal_uInt32 nFormat, const Color** ppColor,
125 ScInterpreterContext* pContext, bool bNullVals, bool bFormula )
127 *ppColor = nullptr;
129 ScRefCellValue aCell(rDoc, rPos);
130 return GetString(aCell, nFormat, ppColor, pContext, rDoc, bNullVals, bFormula);
133 OUString ScCellFormat::GetInputString(
134 const ScRefCellValue& rCell, sal_uInt32 nFormat, ScInterpreterContext* pContext, const ScDocument& rDoc,
135 bool bFiltering, bool bForceSystemLocale )
137 ScInterpreterContext& rContext = pContext ? *pContext : rDoc.GetNonThreadedContext();
139 switch (rCell.getType())
141 case CELLTYPE_STRING:
142 case CELLTYPE_EDIT:
143 return rCell.getString(&rDoc);
144 case CELLTYPE_VALUE:
145 return rContext.NFGetInputLineString(rCell.getDouble(), nFormat, bFiltering, bForceSystemLocale);
146 break;
147 case CELLTYPE_FORMULA:
149 std::optional<OUString> str;
150 ScFormulaCell* pFC = rCell.getFormula();
151 if (pFC->IsEmptyDisplayedAsString())
152 ; // empty
153 else if (pFC->IsValue())
154 str = rContext.NFGetInputLineString(pFC->GetValue(), nFormat, bFiltering, bForceSystemLocale);
155 else
156 str = pFC->GetString().getString();
158 const FormulaError nErrCode = pFC->GetErrCode();
159 if (nErrCode != FormulaError::NONE)
160 str.reset();
162 return str ? std::move(*str) : svl::SharedString::EMPTY_STRING;
164 case CELLTYPE_NONE:
165 return svl::SharedString::EMPTY_STRING;
166 default:
167 return svl::SharedString::EMPTY_STRING;
171 OUString ScCellFormat::GetOutputString( ScDocument& rDoc, const ScAddress& rPos, const ScRefCellValue& rCell )
173 if (rCell.isEmpty())
174 return OUString();
176 if (rCell.getType() == CELLTYPE_EDIT)
178 // GetString converts line breaks into spaces in EditCell,
179 // but here we need the line breaks
180 const EditTextObject* pData = rCell.getEditText();
181 if (pData)
183 ScFieldEditEngine& rEngine = rDoc.GetEditEngine();
184 rEngine.SetTextCurrentDefaults(*pData);
185 return rEngine.GetText();
187 // also do not format EditCells as numbers
188 // (fitting to output)
189 return OUString();
191 else
193 // like in GetString for document (column)
194 const Color* pColor;
195 sal_uInt32 nNumFmt = rDoc.GetNumberFormat(ScRange(rPos));
196 return GetString(rCell, nNumFmt, &pColor, nullptr, rDoc);
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */