xref: /AOO41X/main/sc/source/core/tool/cellform.cxx (revision ff0525f24f03981d56b7579b645949f111420994)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 // INCLUDE ---------------------------------------------------------------
27 
28 #include <sfx2/objsh.hxx>
29 #include <svl/smplhint.hxx>
30 #include <svl/zforlist.hxx>
31 
32 #include "cellform.hxx"
33 #include "cell.hxx"
34 #include "document.hxx"
35 #include "formula/errorcodes.hxx"
36 #include "sc.hrc"
37 
38 // STATIC DATA -----------------------------------------------------------
39 
40 // Err527 Workaround
41 const ScFormulaCell* pLastFormulaTreeTop = 0;
42 
43 // -----------------------------------------------------------------------
44 
45 void ScCellFormat::GetString( ScBaseCell* pCell, sal_uLong nFormat, String& rString,
46                               Color** ppColor, SvNumberFormatter& rFormatter,
47                               sal_Bool bNullVals,
48                               sal_Bool bFormula,
49                               ScForceTextFmt eForceTextFmt )
50 {
51     *ppColor = NULL;
52     if (&rFormatter==NULL)
53     {
54         rString.Erase();
55         return;
56     }
57 
58     CellType eType = pCell->GetCellType();
59     switch(eType)
60     {
61         case CELLTYPE_STRING:
62             {
63                 String aCellString;
64                 ((ScStringCell*)pCell)->GetString( aCellString );
65                 rFormatter.GetOutputString( aCellString, nFormat, rString, ppColor );
66             }
67             break;
68         case CELLTYPE_EDIT:
69             {
70                 String aCellString;
71                 ((ScEditCell*)pCell)->GetString( aCellString );
72                 rFormatter.GetOutputString( aCellString, nFormat, rString, ppColor );
73             }
74             break;
75         case CELLTYPE_VALUE:
76             {
77                 double nValue = ((ScValueCell*)pCell)->GetValue();
78                 if ( !bNullVals && nValue == 0.0 )
79                     rString.Erase();
80                 else
81                 {
82                     if( eForceTextFmt == ftCheck )
83                     {
84                         if( nFormat && rFormatter.IsTextFormat( nFormat ) )
85                             eForceTextFmt = ftForce;
86                     }
87                     if( eForceTextFmt == ftForce )
88                     {
89                         String aTemp;
90                         rFormatter.GetOutputString( nValue, 0, aTemp, ppColor );
91                         rFormatter.GetOutputString( aTemp, nFormat, rString, ppColor );
92                     }
93                     else
94                         rFormatter.GetOutputString( nValue, nFormat, rString, ppColor );
95                 }
96             }
97             break;
98         case CELLTYPE_FORMULA:
99             {
100                 ScFormulaCell*  pFCell = (ScFormulaCell*)pCell;
101                 if ( bFormula )
102                     pFCell->GetFormula( rString );
103                 else
104                 {
105                     // #62160# Ein via Interpreter gestartetes Makro, das hart
106                     // auf Formelzellen zugreift, bekommt einen CellText, auch
107                     // wenn dadurch ein weiterer Interpreter gestartet wird,
108                     // aber nicht wenn diese Zelle gerade interpretiert wird.
109                     // IdleCalc startet generell keine weiteren Interpreter,
110                     // um keine Err522 (zirkulaer) zu bekommen.
111                     if ( pFCell->GetDocument()->IsInInterpreter() &&
112                             (!pFCell->GetDocument()->GetMacroInterpretLevel()
113                             || pFCell->IsRunning()) )
114                     {
115                         rString.AssignAscii( RTL_CONSTASCII_STRINGPARAM("...") );
116                     }
117                     else
118                     {
119                         sal_uInt16 nErrCode = pFCell->GetErrCode();
120 
121                         // erst nach dem Interpretieren (GetErrCode) das Zahlformat holen:
122                         if ( (nFormat % SV_COUNTRY_LANGUAGE_OFFSET) == 0 )
123                             nFormat = pFCell->GetStandardFormat( rFormatter,
124                                 nFormat );
125 
126                         if (nErrCode != 0)
127                             rString = ScGlobal::GetErrorString(nErrCode);
128                         else if ( pFCell->IsEmptyDisplayedAsString() )
129                             rString.Erase();
130                         else if ( pFCell->IsValue() )
131                         {
132                             if(pFCell->GetFormatType() == NUMBERFORMAT_LOGICAL)
133                             {
134                                 String aCellString;
135                                 double fValue = pFCell->GetValue();
136                                 if(fValue)
137                                     aCellString = rFormatter.GetTrueString();
138                                 else
139                                     aCellString = rFormatter.GetFalseString();
140                                 rFormatter.GetOutputString( aCellString, nFormat, rString, ppColor );
141 
142                             }
143                             else
144                             {
145                                 double fValue = pFCell->GetValue();
146                                 if ( !bNullVals && fValue == 0.0 )
147                                     rString.Erase();
148                                 else
149                                     rFormatter.GetOutputString( fValue, nFormat, rString, ppColor );
150                             }
151                         }
152                         else
153                         {
154                             String aCellString;
155                             pFCell->GetString( aCellString );
156                             rFormatter.GetOutputString( aCellString, nFormat, rString, ppColor );
157                         }
158                     }
159                 }
160             }
161             break;
162         default:
163             rString.Erase();
164             break;
165     }
166 }
167 
168 void ScCellFormat::GetInputString( ScBaseCell* pCell, sal_uLong nFormat, String& rString,
169                                       SvNumberFormatter& rFormatter )
170 {
171     if (&rFormatter==NULL)
172     {
173         rString.Erase();
174         return;
175     }
176 
177     CellType eType = pCell->GetCellType();
178     switch(eType)
179     {
180         case CELLTYPE_STRING:
181             {
182                 ((ScStringCell*)pCell)->GetString( rString );
183             }
184             break;
185         case CELLTYPE_EDIT:
186             {
187                 ((ScEditCell*)pCell)->GetString( rString );
188             }
189             break;
190         case CELLTYPE_VALUE:
191             {
192                 double nValue = ((ScValueCell*)pCell)->GetValue();
193                 rFormatter.GetInputLineString( nValue, nFormat, rString );
194             }
195             break;
196         case CELLTYPE_FORMULA:
197             {
198                 if (((ScFormulaCell*)pCell)->IsEmptyDisplayedAsString())
199                 {
200                     rString.Erase();
201                 }
202                 else if (((ScFormulaCell*)pCell)->IsValue())
203                 {
204                     double nValue = ((ScFormulaCell*)pCell)->GetValue();
205                     rFormatter.GetInputLineString( nValue, nFormat, rString );
206                 }
207                 else
208                 {
209                     ((ScFormulaCell*)pCell)->GetString( rString );
210                 }
211 
212                 sal_uInt16 nErrCode = ((ScFormulaCell*)pCell)->GetErrCode();
213                 if (nErrCode != 0)
214                 {
215                     rString.Erase();
216                 }
217             }
218             break;
219         default:
220             rString.Erase();
221             break;
222     }
223 }
224 
225 
226 
227