Line data Source code
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 <svl/zforlist.hxx>
21 : #include <rtl/math.hxx>
22 :
23 : #include <com/sun/star/uno/Any.hxx>
24 : #include <com/sun/star/uno/Sequence.hxx>
25 : #include <comphelper/string.hxx>
26 : #include "rangeseq.hxx"
27 : #include "document.hxx"
28 : #include "dociter.hxx"
29 : #include "scmatrix.hxx"
30 : #include "cell.hxx"
31 :
32 : using namespace com::sun::star;
33 :
34 : //------------------------------------------------------------------------
35 :
36 0 : static bool lcl_HasErrors( ScDocument* pDoc, const ScRange& rRange )
37 : {
38 : // no need to look at empty cells - just use ScCellIterator
39 0 : ScCellIterator aIter( pDoc, rRange );
40 0 : ScBaseCell* pCell = aIter.GetFirst();
41 0 : while (pCell)
42 : {
43 0 : if ( pCell->GetCellType() == CELLTYPE_FORMULA && static_cast<ScFormulaCell*>(pCell)->GetErrCode() != 0 )
44 0 : return true;
45 0 : pCell = aIter.GetNext();
46 : }
47 0 : return false; // no error found
48 : }
49 :
50 0 : static long lcl_DoubleToLong( double fVal )
51 : {
52 : double fInt = (fVal >= 0.0) ? ::rtl::math::approxFloor( fVal ) :
53 0 : ::rtl::math::approxCeil( fVal );
54 0 : if ( fInt >= LONG_MIN && fInt <= LONG_MAX )
55 0 : return (long)fInt;
56 : else
57 0 : return 0; // out of range
58 : }
59 :
60 0 : sal_Bool ScRangeToSequence::FillLongArray( uno::Any& rAny, ScDocument* pDoc, const ScRange& rRange )
61 : {
62 0 : SCTAB nTab = rRange.aStart.Tab();
63 0 : SCCOL nStartCol = rRange.aStart.Col();
64 0 : SCROW nStartRow = rRange.aStart.Row();
65 0 : long nColCount = rRange.aEnd.Col() + 1 - rRange.aStart.Col();
66 0 : long nRowCount = rRange.aEnd.Row() + 1 - rRange.aStart.Row();
67 :
68 0 : uno::Sequence< uno::Sequence<sal_Int32> > aRowSeq( nRowCount );
69 0 : uno::Sequence<sal_Int32>* pRowAry = aRowSeq.getArray();
70 0 : for (long nRow = 0; nRow < nRowCount; nRow++)
71 : {
72 0 : uno::Sequence<sal_Int32> aColSeq( nColCount );
73 0 : sal_Int32* pColAry = aColSeq.getArray();
74 0 : for (long nCol = 0; nCol < nColCount; nCol++)
75 0 : pColAry[nCol] = lcl_DoubleToLong( pDoc->GetValue(
76 0 : ScAddress( (SCCOL)(nStartCol+nCol), (SCROW)(nStartRow+nRow), nTab ) ) );
77 :
78 0 : pRowAry[nRow] = aColSeq;
79 0 : }
80 :
81 0 : rAny <<= aRowSeq;
82 0 : return !lcl_HasErrors( pDoc, rRange );
83 : }
84 :
85 :
86 0 : sal_Bool ScRangeToSequence::FillLongArray( uno::Any& rAny, const ScMatrix* pMatrix )
87 : {
88 0 : if (!pMatrix)
89 0 : return false;
90 :
91 : SCSIZE nColCount;
92 : SCSIZE nRowCount;
93 0 : pMatrix->GetDimensions( nColCount, nRowCount );
94 :
95 0 : uno::Sequence< uno::Sequence<sal_Int32> > aRowSeq( static_cast<sal_Int32>(nRowCount) );
96 0 : uno::Sequence<sal_Int32>* pRowAry = aRowSeq.getArray();
97 0 : for (SCSIZE nRow = 0; nRow < nRowCount; nRow++)
98 : {
99 0 : uno::Sequence<sal_Int32> aColSeq( static_cast<sal_Int32>(nColCount) );
100 0 : sal_Int32* pColAry = aColSeq.getArray();
101 0 : for (SCSIZE nCol = 0; nCol < nColCount; nCol++)
102 0 : if ( pMatrix->IsString( nCol, nRow ) )
103 0 : pColAry[nCol] = 0;
104 : else
105 0 : pColAry[nCol] = lcl_DoubleToLong( pMatrix->GetDouble( nCol, nRow ) );
106 :
107 0 : pRowAry[nRow] = aColSeq;
108 0 : }
109 :
110 0 : rAny <<= aRowSeq;
111 0 : return sal_True;
112 : }
113 :
114 : //------------------------------------------------------------------------
115 :
116 0 : sal_Bool ScRangeToSequence::FillDoubleArray( uno::Any& rAny, ScDocument* pDoc, const ScRange& rRange )
117 : {
118 0 : SCTAB nTab = rRange.aStart.Tab();
119 0 : SCCOL nStartCol = rRange.aStart.Col();
120 0 : SCROW nStartRow = rRange.aStart.Row();
121 0 : long nColCount = rRange.aEnd.Col() + 1 - rRange.aStart.Col();
122 0 : long nRowCount = rRange.aEnd.Row() + 1 - rRange.aStart.Row();
123 :
124 0 : uno::Sequence< uno::Sequence<double> > aRowSeq( nRowCount );
125 0 : uno::Sequence<double>* pRowAry = aRowSeq.getArray();
126 0 : for (long nRow = 0; nRow < nRowCount; nRow++)
127 : {
128 0 : uno::Sequence<double> aColSeq( nColCount );
129 0 : double* pColAry = aColSeq.getArray();
130 0 : for (long nCol = 0; nCol < nColCount; nCol++)
131 0 : pColAry[nCol] = pDoc->GetValue(
132 0 : ScAddress( (SCCOL)(nStartCol+nCol), (SCROW)(nStartRow+nRow), nTab ) );
133 :
134 0 : pRowAry[nRow] = aColSeq;
135 0 : }
136 :
137 0 : rAny <<= aRowSeq;
138 0 : return !lcl_HasErrors( pDoc, rRange );
139 : }
140 :
141 :
142 0 : sal_Bool ScRangeToSequence::FillDoubleArray( uno::Any& rAny, const ScMatrix* pMatrix )
143 : {
144 0 : if (!pMatrix)
145 0 : return false;
146 :
147 : SCSIZE nColCount;
148 : SCSIZE nRowCount;
149 0 : pMatrix->GetDimensions( nColCount, nRowCount );
150 :
151 0 : uno::Sequence< uno::Sequence<double> > aRowSeq( static_cast<sal_Int32>(nRowCount) );
152 0 : uno::Sequence<double>* pRowAry = aRowSeq.getArray();
153 0 : for (SCSIZE nRow = 0; nRow < nRowCount; nRow++)
154 : {
155 0 : uno::Sequence<double> aColSeq( static_cast<sal_Int32>(nColCount) );
156 0 : double* pColAry = aColSeq.getArray();
157 0 : for (SCSIZE nCol = 0; nCol < nColCount; nCol++)
158 0 : if ( pMatrix->IsString( nCol, nRow ) )
159 0 : pColAry[nCol] = 0.0;
160 : else
161 0 : pColAry[nCol] = pMatrix->GetDouble( nCol, nRow );
162 :
163 0 : pRowAry[nRow] = aColSeq;
164 0 : }
165 :
166 0 : rAny <<= aRowSeq;
167 0 : return sal_True;
168 : }
169 :
170 : //------------------------------------------------------------------------
171 :
172 0 : sal_Bool ScRangeToSequence::FillStringArray( uno::Any& rAny, ScDocument* pDoc, const ScRange& rRange )
173 : {
174 0 : SCTAB nTab = rRange.aStart.Tab();
175 0 : SCCOL nStartCol = rRange.aStart.Col();
176 0 : SCROW nStartRow = rRange.aStart.Row();
177 0 : long nColCount = rRange.aEnd.Col() + 1 - rRange.aStart.Col();
178 0 : long nRowCount = rRange.aEnd.Row() + 1 - rRange.aStart.Row();
179 :
180 0 : bool bHasErrors = false;
181 :
182 0 : uno::Sequence< uno::Sequence<rtl::OUString> > aRowSeq( nRowCount );
183 0 : uno::Sequence<rtl::OUString>* pRowAry = aRowSeq.getArray();
184 0 : for (long nRow = 0; nRow < nRowCount; nRow++)
185 : {
186 0 : uno::Sequence<rtl::OUString> aColSeq( nColCount );
187 0 : rtl::OUString* pColAry = aColSeq.getArray();
188 0 : for (long nCol = 0; nCol < nColCount; nCol++)
189 : {
190 : sal_uInt16 nErrCode = pDoc->GetStringForFormula(
191 : ScAddress((SCCOL)(nStartCol+nCol), (SCROW)(nStartRow+nRow), nTab),
192 0 : pColAry[nCol] );
193 0 : if ( nErrCode != 0 )
194 0 : bHasErrors = true;
195 : }
196 0 : pRowAry[nRow] = aColSeq;
197 0 : }
198 :
199 0 : rAny <<= aRowSeq;
200 0 : return !bHasErrors;
201 : }
202 :
203 :
204 0 : sal_Bool ScRangeToSequence::FillStringArray( uno::Any& rAny, const ScMatrix* pMatrix,
205 : SvNumberFormatter* pFormatter )
206 : {
207 0 : if (!pMatrix)
208 0 : return false;
209 :
210 : SCSIZE nColCount;
211 : SCSIZE nRowCount;
212 0 : pMatrix->GetDimensions( nColCount, nRowCount );
213 :
214 0 : uno::Sequence< uno::Sequence<rtl::OUString> > aRowSeq( static_cast<sal_Int32>(nRowCount) );
215 0 : uno::Sequence<rtl::OUString>* pRowAry = aRowSeq.getArray();
216 0 : for (SCSIZE nRow = 0; nRow < nRowCount; nRow++)
217 : {
218 0 : uno::Sequence<rtl::OUString> aColSeq( static_cast<sal_Int32>(nColCount) );
219 0 : rtl::OUString* pColAry = aColSeq.getArray();
220 0 : for (SCSIZE nCol = 0; nCol < nColCount; nCol++)
221 : {
222 0 : String aStr;
223 0 : if ( pMatrix->IsString( nCol, nRow ) )
224 : {
225 0 : if ( !pMatrix->IsEmpty( nCol, nRow ) )
226 0 : aStr = pMatrix->GetString( nCol, nRow );
227 : }
228 0 : else if ( pFormatter )
229 : {
230 0 : double fVal = pMatrix->GetDouble( nCol, nRow );
231 : Color* pColor;
232 0 : pFormatter->GetOutputString( fVal, 0, aStr, &pColor );
233 : }
234 0 : pColAry[nCol] = rtl::OUString( aStr );
235 0 : }
236 :
237 0 : pRowAry[nRow] = aColSeq;
238 0 : }
239 :
240 0 : rAny <<= aRowSeq;
241 0 : return sal_True;
242 : }
243 :
244 : //------------------------------------------------------------------------
245 :
246 0 : static double lcl_GetValueFromCell( ScBaseCell& rCell )
247 : {
248 : //! ScBaseCell member function?
249 :
250 0 : CellType eType = rCell.GetCellType();
251 0 : if ( eType == CELLTYPE_VALUE )
252 0 : return ((ScValueCell&)rCell).GetValue();
253 0 : else if ( eType == CELLTYPE_FORMULA )
254 0 : return ((ScFormulaCell&)rCell).GetValue(); // called only if result is value
255 :
256 : OSL_FAIL( "GetValueFromCell: wrong type" );
257 0 : return 0;
258 : }
259 :
260 0 : sal_Bool ScRangeToSequence::FillMixedArray( uno::Any& rAny, ScDocument* pDoc, const ScRange& rRange,
261 : sal_Bool bAllowNV )
262 : {
263 0 : SCTAB nTab = rRange.aStart.Tab();
264 0 : SCCOL nStartCol = rRange.aStart.Col();
265 0 : SCROW nStartRow = rRange.aStart.Row();
266 0 : long nColCount = rRange.aEnd.Col() + 1 - rRange.aStart.Col();
267 0 : long nRowCount = rRange.aEnd.Row() + 1 - rRange.aStart.Row();
268 :
269 0 : String aDocStr;
270 0 : sal_Bool bHasErrors = false;
271 :
272 0 : uno::Sequence< uno::Sequence<uno::Any> > aRowSeq( nRowCount );
273 0 : uno::Sequence<uno::Any>* pRowAry = aRowSeq.getArray();
274 0 : for (long nRow = 0; nRow < nRowCount; nRow++)
275 : {
276 0 : uno::Sequence<uno::Any> aColSeq( nColCount );
277 0 : uno::Any* pColAry = aColSeq.getArray();
278 0 : for (long nCol = 0; nCol < nColCount; nCol++)
279 : {
280 0 : uno::Any& rElement = pColAry[nCol];
281 :
282 0 : ScAddress aPos( (SCCOL)(nStartCol+nCol), (SCROW)(nStartRow+nRow), nTab );
283 0 : ScBaseCell* pCell = pDoc->GetCell( aPos );
284 0 : if ( pCell )
285 : {
286 0 : if ( pCell->GetCellType() == CELLTYPE_FORMULA &&
287 0 : ((ScFormulaCell*)pCell)->GetErrCode() != 0 )
288 : {
289 : // if NV is allowed, leave empty for errors
290 0 : bHasErrors = sal_True;
291 : }
292 0 : else if ( pCell->HasValueData() )
293 0 : rElement <<= (double) lcl_GetValueFromCell( *pCell );
294 : else
295 0 : rElement <<= rtl::OUString( pCell->GetStringData() );
296 : }
297 : else
298 0 : rElement <<= rtl::OUString(); // empty: empty string
299 : }
300 0 : pRowAry[nRow] = aColSeq;
301 0 : }
302 :
303 0 : rAny <<= aRowSeq;
304 0 : return bAllowNV || !bHasErrors;
305 : }
306 :
307 :
308 2 : sal_Bool ScRangeToSequence::FillMixedArray( uno::Any& rAny, const ScMatrix* pMatrix, bool bDataTypes )
309 : {
310 2 : if (!pMatrix)
311 0 : return false;
312 :
313 : SCSIZE nColCount;
314 : SCSIZE nRowCount;
315 2 : pMatrix->GetDimensions( nColCount, nRowCount );
316 :
317 2 : uno::Sequence< uno::Sequence<uno::Any> > aRowSeq( static_cast<sal_Int32>(nRowCount) );
318 2 : uno::Sequence<uno::Any>* pRowAry = aRowSeq.getArray();
319 6 : for (SCSIZE nRow = 0; nRow < nRowCount; nRow++)
320 : {
321 4 : uno::Sequence<uno::Any> aColSeq( static_cast<sal_Int32>(nColCount) );
322 4 : uno::Any* pColAry = aColSeq.getArray();
323 16 : for (SCSIZE nCol = 0; nCol < nColCount; nCol++)
324 : {
325 12 : if ( pMatrix->IsString( nCol, nRow ) )
326 : {
327 0 : String aStr;
328 0 : if ( !pMatrix->IsEmpty( nCol, nRow ) )
329 0 : aStr = pMatrix->GetString( nCol, nRow );
330 0 : pColAry[nCol] <<= rtl::OUString( aStr );
331 : }
332 : else
333 : {
334 12 : double fVal = pMatrix->GetDouble( nCol, nRow );
335 12 : if (bDataTypes && pMatrix->IsBoolean( nCol, nRow ))
336 0 : pColAry[nCol] <<= (fVal ? true : false);
337 : else
338 12 : pColAry[nCol] <<= fVal;
339 : }
340 : }
341 :
342 4 : pRowAry[nRow] = aColSeq;
343 4 : }
344 :
345 2 : rAny <<= aRowSeq;
346 2 : return sal_True;
347 : }
348 :
349 : //------------------------------------------------------------------------
350 :
351 6 : bool ScApiTypeConversion::ConvertAnyToDouble( double & o_fVal,
352 : com::sun::star::uno::TypeClass & o_eClass,
353 : const com::sun::star::uno::Any & rAny )
354 : {
355 6 : bool bRet = false;
356 6 : o_eClass = rAny.getValueTypeClass();
357 6 : switch (o_eClass)
358 : {
359 : //! extract integer values
360 : case uno::TypeClass_ENUM:
361 : case uno::TypeClass_BOOLEAN:
362 : case uno::TypeClass_CHAR:
363 : case uno::TypeClass_BYTE:
364 : case uno::TypeClass_SHORT:
365 : case uno::TypeClass_UNSIGNED_SHORT:
366 : case uno::TypeClass_LONG:
367 : case uno::TypeClass_UNSIGNED_LONG:
368 : case uno::TypeClass_FLOAT:
369 : case uno::TypeClass_DOUBLE:
370 6 : rAny >>= o_fVal;
371 6 : bRet = true;
372 6 : break;
373 : default:
374 : ; // nothing, avoid warning
375 : }
376 6 : if (!bRet)
377 0 : o_fVal = 0.0;
378 6 : return bRet;
379 : }
380 :
381 : //------------------------------------------------------------------------
382 :
383 1 : ScMatrixRef ScSequenceToMatrix::CreateMixedMatrix( const com::sun::star::uno::Any & rAny )
384 : {
385 1 : ScMatrixRef xMatrix;
386 1 : uno::Sequence< uno::Sequence< uno::Any > > aSequence;
387 1 : if ( rAny >>= aSequence )
388 : {
389 1 : sal_Int32 nRowCount = aSequence.getLength();
390 1 : const uno::Sequence<uno::Any>* pRowArr = aSequence.getConstArray();
391 1 : sal_Int32 nMaxColCount = 0;
392 : sal_Int32 nCol, nRow;
393 3 : for (nRow=0; nRow<nRowCount; nRow++)
394 : {
395 2 : sal_Int32 nTmp = pRowArr[nRow].getLength();
396 2 : if ( nTmp > nMaxColCount )
397 1 : nMaxColCount = nTmp;
398 : }
399 1 : if ( nMaxColCount && nRowCount )
400 : {
401 1 : rtl::OUString aUStr;
402 : xMatrix = new ScMatrix(
403 : static_cast<SCSIZE>(nMaxColCount),
404 1 : static_cast<SCSIZE>(nRowCount), 0.0);
405 : SCSIZE nCols, nRows;
406 1 : xMatrix->GetDimensions( nCols, nRows);
407 1 : if (nCols != static_cast<SCSIZE>(nMaxColCount) || nRows != static_cast<SCSIZE>(nRowCount))
408 : {
409 : OSL_FAIL( "ScSequenceToMatrix::CreateMixedMatrix: matrix exceeded max size, returning NULL matrix");
410 0 : return NULL;
411 : }
412 3 : for (nRow=0; nRow<nRowCount; nRow++)
413 : {
414 2 : sal_Int32 nColCount = pRowArr[nRow].getLength();
415 2 : const uno::Any* pColArr = pRowArr[nRow].getConstArray();
416 8 : for (nCol=0; nCol<nColCount; nCol++)
417 : {
418 : double fVal;
419 : uno::TypeClass eClass;
420 6 : if (ScApiTypeConversion::ConvertAnyToDouble( fVal, eClass, pColArr[nCol]))
421 : {
422 6 : if (eClass == uno::TypeClass_BOOLEAN)
423 : xMatrix->PutBoolean( (fVal ? true : false),
424 : static_cast<SCSIZE>(nCol),
425 0 : static_cast<SCSIZE>(nRow) );
426 : else
427 : xMatrix->PutDouble( fVal,
428 : static_cast<SCSIZE>(nCol),
429 6 : static_cast<SCSIZE>(nRow) );
430 : }
431 : else
432 : {
433 : // Try string, else use empty as last resort.
434 :
435 0 : if ( pColArr[nCol] >>= aUStr )
436 : xMatrix->PutString( String( aUStr ),
437 : static_cast<SCSIZE>(nCol),
438 0 : static_cast<SCSIZE>(nRow) );
439 : else
440 : xMatrix->PutEmpty(
441 : static_cast<SCSIZE>(nCol),
442 0 : static_cast<SCSIZE>(nRow) );
443 : }
444 : }
445 2 : for (nCol=nColCount; nCol<nMaxColCount; nCol++)
446 : {
447 : xMatrix->PutEmpty(
448 : static_cast<SCSIZE>(nCol),
449 0 : static_cast<SCSIZE>(nRow) );
450 : }
451 1 : }
452 : }
453 : }
454 1 : return xMatrix;
455 : }
456 :
457 :
458 : //------------------------------------------------------------------------
459 :
460 0 : sal_Bool ScByteSequenceToString::GetString( String& rString, const uno::Any& rAny,
461 : sal_uInt16 nEncoding )
462 : {
463 0 : uno::Sequence<sal_Int8> aSeq;
464 0 : if ( rAny >>= aSeq )
465 : {
466 0 : rString = String( (const sal_Char*)aSeq.getConstArray(),
467 0 : (xub_StrLen)aSeq.getLength(), nEncoding );
468 0 : rString = comphelper::string::stripEnd(rString, 0);
469 0 : return sal_True;
470 : }
471 0 : return false;
472 : }
473 :
474 : //------------------------------------------------------------------------
475 :
476 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|