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
27 // INCLUDE ---------------------------------------------------------------
28
29 #include <svl/zforlist.hxx>
30
31 #include "scitems.hxx"
32 #include "global.hxx"
33 #include "dociter.hxx"
34 #include "document.hxx"
35 #include "table.hxx"
36 #include "column.hxx"
37 #include "cell.hxx"
38 #include "attarray.hxx"
39 #include "patattr.hxx"
40 #include "docoptio.hxx"
41 #include "cellform.hxx"
42
43 #include <vector>
44
45 using ::rtl::math::approxEqual;
46 using ::std::vector;
47 using ::rtl::OUString;
48 using ::std::set;
49
50 // STATIC DATA -----------------------------------------------------------
51
52 namespace {
53
lcl_toUpper(OUString & rStr)54 void lcl_toUpper(OUString& rStr)
55 {
56 rStr = ScGlobal::pCharClass->toUpper(rStr.trim(), 0, static_cast<sal_uInt16>(rStr.getLength()));
57 }
58
59 }
60
ScDocumentIterator(ScDocument * pDocument,SCTAB nStartTable,SCTAB nEndTable)61 ScDocumentIterator::ScDocumentIterator( ScDocument* pDocument,
62 SCTAB nStartTable, SCTAB nEndTable ) :
63 pDoc( pDocument ),
64 nStartTab( nStartTable ),
65 nEndTab( nEndTable )
66 {
67 PutInOrder( nStartTab, nEndTab );
68 if (!ValidTab(nStartTab)) nStartTab = MAXTAB;
69 if (!ValidTab(nEndTab)) nEndTab = MAXTAB;
70
71 pDefPattern = pDoc->GetDefPattern();
72
73 nCol = 0;
74 nRow = 0;
75 nTab = nStartTab;
76
77 nColPos = 0;
78 nAttrPos = 0;
79 }
80
~ScDocumentIterator()81 ScDocumentIterator::~ScDocumentIterator()
82 {
83 }
84
GetThisCol()85 sal_Bool ScDocumentIterator::GetThisCol()
86 {
87 ScTable* pTab;
88 while ( (pTab = pDoc->pTab[nTab]) == NULL )
89 {
90 if ( nTab == nEndTab )
91 {
92 nCol = MAXCOL;
93 nRow = MAXROW;
94 return sal_False;
95 }
96 ++nTab;
97 }
98 ScColumn* pCol = &pTab->aCol[nCol];
99 ScAttrArray* pAtt = pCol->pAttrArray;
100
101 sal_Bool bFound = sal_False;
102 do
103 {
104 SCROW nColRow;
105 SCROW nAttrEnd;
106
107 do
108 {
109 nAttrEnd = pAtt->pData[nAttrPos].nRow;
110 if (nAttrEnd < nRow)
111 ++nAttrPos;
112 }
113 while (nAttrEnd < nRow);
114
115 do
116 {
117 nColRow = (nColPos < pCol->nCount) ? pCol->pItems[nColPos].nRow : MAXROW+1;
118 if (nColRow < nRow)
119 ++nColPos;
120 }
121 while (nColRow < nRow);
122
123 if (nColRow == nRow)
124 {
125 bFound = sal_True;
126 pCell = pCol->pItems[nColPos].pCell;
127 pPattern = pAtt->pData[nAttrPos].pPattern;
128 }
129 else if ( pAtt->pData[nAttrPos].pPattern != pDefPattern )
130 {
131 bFound = sal_True;
132 pCell = NULL;
133 pPattern = pAtt->pData[nAttrPos].pPattern;
134 }
135 else
136 {
137 nRow = Min( (SCROW)nColRow, (SCROW)(nAttrEnd+1) );
138 }
139 }
140 while (!bFound && nRow <= MAXROW);
141
142 return bFound;
143 }
144
GetThis()145 sal_Bool ScDocumentIterator::GetThis()
146 {
147 sal_Bool bEnd = sal_False;
148 sal_Bool bSuccess = sal_False;
149
150 while ( !bSuccess && !bEnd )
151 {
152 if ( nRow > MAXROW )
153 bSuccess = sal_False;
154 else
155 bSuccess = GetThisCol();
156
157 if ( !bSuccess )
158 {
159 ++nCol;
160 if (nCol > MAXCOL)
161 {
162 nCol = 0;
163 ++nTab;
164 if (nTab > nEndTab)
165 bEnd = sal_True;
166 }
167 nRow = 0;
168 nColPos = 0;
169 nAttrPos = 0;
170 }
171 }
172
173 return !bEnd;
174 }
175
GetFirst()176 sal_Bool ScDocumentIterator::GetFirst()
177 {
178 nCol = 0;
179 nTab = nStartTab;
180
181 nRow = 0;
182 nColPos = 0;
183 nAttrPos = 0;
184
185 return GetThis();
186 }
187
GetNext()188 sal_Bool ScDocumentIterator::GetNext()
189 {
190 ++nRow;
191
192 return GetThis();
193 }
194
195 //------------------------------------------------------------------------
196
GetCell()197 ScBaseCell* ScDocumentIterator::GetCell()
198 {
199 return pCell;
200 }
201
GetPattern()202 const ScPatternAttr* ScDocumentIterator::GetPattern()
203 {
204 return pPattern;
205 }
206
GetPos(SCCOL & rCol,SCROW & rRow,SCTAB & rTab)207 void ScDocumentIterator::GetPos( SCCOL& rCol, SCROW& rRow, SCTAB& rTab )
208 {
209 rCol = nCol;
210 rRow = nRow;
211 rTab = nTab;
212 }
213
214
215 //------------------------------------------------------------------------
216 //------------------------------------------------------------------------
lcl_IterGetNumberFormat(sal_uLong & nFormat,const ScAttrArray * & rpArr,SCROW & nAttrEndRow,const ScAttrArray * pNewArr,SCROW nRow,ScDocument * pDoc)217 void lcl_IterGetNumberFormat( sal_uLong& nFormat, const ScAttrArray*& rpArr,
218 SCROW& nAttrEndRow, const ScAttrArray* pNewArr, SCROW nRow,
219 ScDocument* pDoc )
220 {
221 if ( rpArr != pNewArr || nAttrEndRow < nRow )
222 {
223 SCSIZE nPos;
224 pNewArr->Search( nRow, nPos ); // nPos 0 gueltig wenn nicht gefunden
225 const ScPatternAttr* pPattern = pNewArr->pData[nPos].pPattern;
226 nFormat = pPattern->GetNumberFormat( pDoc->GetFormatTable() );
227 rpArr = pNewArr;
228 nAttrEndRow = pNewArr->pData[nPos].nRow;
229 }
230 }
231
232 //UNUSED2008-05 ScValueIterator::ScValueIterator( ScDocument* pDocument,
233 //UNUSED2008-05 SCCOL nSCol, SCROW nSRow, SCTAB nSTab,
234 //UNUSED2008-05 SCCOL nECol, SCROW nERow, SCTAB nETab,
235 //UNUSED2008-05 sal_Bool bSTotal, sal_Bool bTextZero ) :
236 //UNUSED2008-05 pDoc( pDocument ),
237 //UNUSED2008-05 nNumFmtIndex(0),
238 //UNUSED2008-05 nStartCol( nSCol),
239 //UNUSED2008-05 nStartRow( nSRow),
240 //UNUSED2008-05 nStartTab( nSTab ),
241 //UNUSED2008-05 nEndCol( nECol ),
242 //UNUSED2008-05 nEndRow( nERow),
243 //UNUSED2008-05 nEndTab( nETab ),
244 //UNUSED2008-05 nNumFmtType( NUMBERFORMAT_UNDEFINED ),
245 //UNUSED2008-05 bNumValid( sal_False ),
246 //UNUSED2008-05 bSubTotal(bSTotal),
247 //UNUSED2008-05 bNextValid( sal_False ),
248 //UNUSED2008-05 bCalcAsShown( pDocument->GetDocOptions().IsCalcAsShown() ),
249 //UNUSED2008-05 bTextAsZero( bTextZero )
250 //UNUSED2008-05 {
251 //UNUSED2008-05 PutInOrder( nStartCol, nEndCol);
252 //UNUSED2008-05 PutInOrder( nStartRow, nEndRow);
253 //UNUSED2008-05 PutInOrder( nStartTab, nEndTab );
254 //UNUSED2008-05
255 //UNUSED2008-05 if (!ValidCol(nStartCol)) nStartCol = MAXCOL;
256 //UNUSED2008-05 if (!ValidCol(nEndCol)) nEndCol = MAXCOL;
257 //UNUSED2008-05 if (!ValidRow(nStartRow)) nStartRow = MAXROW;
258 //UNUSED2008-05 if (!ValidRow(nEndRow)) nEndRow = MAXROW;
259 //UNUSED2008-05 if (!ValidTab(nStartTab)) nStartTab = MAXTAB;
260 //UNUSED2008-05 if (!ValidTab(nEndTab)) nEndTab = MAXTAB;
261 //UNUSED2008-05
262 //UNUSED2008-05 nCol = nStartCol;
263 //UNUSED2008-05 nRow = nStartRow;
264 //UNUSED2008-05 nTab = nStartTab;
265 //UNUSED2008-05
266 //UNUSED2008-05 nColRow = 0; // wird bei GetFirst initialisiert
267 //UNUSED2008-05
268 //UNUSED2008-05 nNumFormat = 0; // werden bei GetNumberFormat initialisiert
269 //UNUSED2008-05 pAttrArray = 0;
270 //UNUSED2008-05 nAttrEndRow = 0;
271 //UNUSED2008-05 }
272
ScValueIterator(ScDocument * pDocument,const ScRange & rRange,sal_Bool bSTotal,sal_Bool bTextZero)273 ScValueIterator::ScValueIterator( ScDocument* pDocument, const ScRange& rRange,
274 sal_Bool bSTotal, sal_Bool bTextZero ) :
275 pDoc( pDocument ),
276 nNumFmtIndex(0),
277 nStartCol( rRange.aStart.Col() ),
278 nStartRow( rRange.aStart.Row() ),
279 nStartTab( rRange.aStart.Tab() ),
280 nEndCol( rRange.aEnd.Col() ),
281 nEndRow( rRange.aEnd.Row() ),
282 nEndTab( rRange.aEnd.Tab() ),
283 nNumFmtType( NUMBERFORMAT_UNDEFINED ),
284 bNumValid( sal_False ),
285 bSubTotal(bSTotal),
286 bNextValid( sal_False ),
287 bCalcAsShown( pDocument->GetDocOptions().IsCalcAsShown() ),
288 bTextAsZero( bTextZero )
289 {
290 PutInOrder( nStartCol, nEndCol);
291 PutInOrder( nStartRow, nEndRow);
292 PutInOrder( nStartTab, nEndTab );
293
294 if (!ValidCol(nStartCol)) nStartCol = MAXCOL;
295 if (!ValidCol(nEndCol)) nEndCol = MAXCOL;
296 if (!ValidRow(nStartRow)) nStartRow = MAXROW;
297 if (!ValidRow(nEndRow)) nEndRow = MAXROW;
298 if (!ValidTab(nStartTab)) nStartTab = MAXTAB;
299 if (!ValidTab(nEndTab)) nEndTab = MAXTAB;
300
301 nCol = nStartCol;
302 nRow = nStartRow;
303 nTab = nStartTab;
304
305 nColRow = 0; // initialized with GetFirst
306
307 nNumFormat = 0; // initialized with GetNumberFormat
308 pAttrArray = 0;
309 nAttrEndRow = 0;
310 }
311
GetThis(double & rValue,sal_uInt16 & rErr)312 sal_Bool ScValueIterator::GetThis(double& rValue, sal_uInt16& rErr)
313 {
314 ScColumn* pCol = &(pDoc->pTab[nTab])->aCol[nCol];
315 for (;;)
316 {
317 if ( nRow > nEndRow )
318 {
319 nRow = nStartRow;
320 do
321 {
322 nCol++;
323 if ( nCol > nEndCol )
324 {
325 nCol = nStartCol;
326 nTab++;
327 if ( nTab > nEndTab )
328 {
329 // rValue = 0.0; //! do not change caller's value!
330 rErr = 0;
331 return sal_False; // Fine
332 }
333 }
334 pCol = &(pDoc->pTab[nTab])->aCol[nCol];
335 } while ( pCol->nCount == 0 );
336 pCol->Search( nRow, nColRow );
337 }
338
339 while (( nColRow < pCol->nCount ) && ( pCol->pItems[nColRow].nRow < nRow ))
340 nColRow++;
341
342 if ( nColRow < pCol->nCount && pCol->pItems[nColRow].nRow <= nEndRow )
343 {
344 nRow = pCol->pItems[nColRow].nRow + 1;
345 if ( !bSubTotal || !pDoc->pTab[nTab]->RowFiltered( nRow-1 ) )
346 {
347 ScBaseCell* pCell = pCol->pItems[nColRow].pCell;
348 ++nColRow;
349 switch (pCell->GetCellType())
350 {
351 case CELLTYPE_VALUE:
352 {
353 bNumValid = sal_False;
354 rValue = ((ScValueCell*)pCell)->GetValue();
355 rErr = 0;
356 --nRow;
357 if ( bCalcAsShown )
358 {
359 lcl_IterGetNumberFormat( nNumFormat, pAttrArray,
360 nAttrEndRow, pCol->pAttrArray, nRow, pDoc );
361 rValue = pDoc->RoundValueAsShown( rValue, nNumFormat );
362 }
363 //
364 // if the same row another value cell follows and belongs to the same block
365 // read the value too.
366 //
367 if ( nColRow < pCol->nCount &&
368 pCol->pItems[nColRow].nRow <= nEndRow &&
369 pCol->pItems[nColRow].pCell->GetCellType() == CELLTYPE_VALUE &&
370 !bSubTotal )
371 {
372 fNextValue = ((ScValueCell*)pCol->pItems[nColRow].pCell)->GetValue();
373 nNextRow = pCol->pItems[nColRow].nRow;
374 bNextValid = sal_True;
375 if ( bCalcAsShown )
376 {
377 lcl_IterGetNumberFormat( nNumFormat, pAttrArray,
378 nAttrEndRow, pCol->pAttrArray, nNextRow, pDoc );
379 fNextValue = pDoc->RoundValueAsShown( fNextValue, nNumFormat );
380 }
381 }
382
383 return sal_True; // found
384 }
385 // break;
386 case CELLTYPE_FORMULA:
387 {
388 if (!bSubTotal || !((ScFormulaCell*)pCell)->IsSubTotal())
389 {
390 rErr = ((ScFormulaCell*)pCell)->GetErrCode();
391 if ( rErr || ((ScFormulaCell*)pCell)->IsValue() )
392 {
393 rValue = ((ScFormulaCell*)pCell)->GetValue();
394 nRow--;
395 bNumValid = sal_False;
396 return sal_True; // found
397 }
398 else if ( bTextAsZero )
399 {
400 rValue = 0.0;
401 nRow--;
402 bNumValid = sal_False;
403 return sal_True;
404 }
405 }
406 }
407 break;
408 case CELLTYPE_STRING :
409 case CELLTYPE_EDIT :
410 {
411 if ( bTextAsZero )
412 {
413 rErr = 0;
414 rValue = 0.0;
415 nNumFmtType = NUMBERFORMAT_NUMBER;
416 nNumFmtIndex = 0;
417 bNumValid = sal_True;
418 --nRow;
419 return sal_True;
420 }
421 }
422 break;
423 default:
424 {
425 // added to avoid warnings
426 }
427 }
428 }
429 }
430 else
431 nRow = nEndRow + 1; // next row
432 }
433 }
434
GetCurNumFmtInfo(short & nType,sal_uLong & nIndex)435 void ScValueIterator::GetCurNumFmtInfo( short& nType, sal_uLong& nIndex )
436 {
437 if (!bNumValid)
438 {
439 const ScColumn* pCol = &(pDoc->pTab[nTab])->aCol[nCol];
440 nNumFmtIndex = pCol->GetNumberFormat( nRow );
441 if ( (nNumFmtIndex % SV_COUNTRY_LANGUAGE_OFFSET) == 0 )
442 {
443 const ScBaseCell* pCell;
444 SCSIZE nIdx = nColRow - 1;
445 // there might be rearranged something, so be on the safe side
446 if ( nIdx < pCol->nCount && pCol->pItems[nIdx].nRow == nRow )
447 pCell = pCol->pItems[nIdx].pCell;
448 else
449 {
450 if ( pCol->Search( nRow, nIdx ) )
451 pCell = pCol->pItems[nIdx].pCell;
452 else
453 pCell = NULL;
454 }
455 if ( pCell && pCell->GetCellType() == CELLTYPE_FORMULA )
456 ((const ScFormulaCell*)pCell)->GetFormatInfo( nNumFmtType, nNumFmtIndex );
457 else
458 nNumFmtType = pDoc->GetFormatTable()->GetType( nNumFmtIndex );
459 }
460 else
461 nNumFmtType = pDoc->GetFormatTable()->GetType( nNumFmtIndex );
462 bNumValid = sal_True;
463 }
464 nType = nNumFmtType;
465 nIndex = nNumFmtIndex;
466 }
467
GetFirst(double & rValue,sal_uInt16 & rErr)468 sal_Bool ScValueIterator::GetFirst(double& rValue, sal_uInt16& rErr)
469 {
470 nCol = nStartCol;
471 nRow = nStartRow;
472 nTab = nStartTab;
473
474 // nColRow = 0;
475 ScColumn* pCol = &(pDoc->pTab[nTab])->aCol[nCol];
476 pCol->Search( nRow, nColRow );
477
478 nNumFormat = 0; // initialized with GetNumberFormat
479 pAttrArray = 0;
480 nAttrEndRow = 0;
481
482 return GetThis(rValue, rErr);
483 }
484
485 /* is inline:
486 sal_Bool ScValueIterator::GetNext(double& rValue, sal_uInt16& rErr)
487 {
488 ++nRow;
489 return GetThis(rValue, rErr);
490 }
491 */
492
493 // ============================================================================
494
DataAccess(const ScDBQueryDataIterator * pParent)495 ScDBQueryDataIterator::DataAccess::DataAccess(const ScDBQueryDataIterator* pParent) :
496 mpParent(pParent)
497 {
498 }
499
~DataAccess()500 ScDBQueryDataIterator::DataAccess::~DataAccess()
501 {
502 }
503
GetRowByColEntryIndex(ScDocument & rDoc,SCTAB nTab,SCCOL nCol,SCSIZE nColRow)504 SCROW ScDBQueryDataIterator::GetRowByColEntryIndex(ScDocument& rDoc, SCTAB nTab, SCCOL nCol, SCSIZE nColRow)
505 {
506 ScColumn* pCol = &rDoc.pTab[nTab]->aCol[nCol];
507 return pCol->pItems[nColRow].nRow;
508 }
509
GetCellByColEntryIndex(ScDocument & rDoc,SCTAB nTab,SCCOL nCol,SCSIZE nColRow)510 ScBaseCell* ScDBQueryDataIterator::GetCellByColEntryIndex(ScDocument& rDoc, SCTAB nTab, SCCOL nCol, SCSIZE nColRow)
511 {
512 ScColumn* pCol = &rDoc.pTab[nTab]->aCol[nCol];
513 return pCol->pItems[nColRow].pCell;
514 }
515
GetAttrArrayByCol(ScDocument & rDoc,SCTAB nTab,SCCOL nCol)516 ScAttrArray* ScDBQueryDataIterator::GetAttrArrayByCol(ScDocument& rDoc, SCTAB nTab, SCCOL nCol)
517 {
518 ScColumn* pCol = &rDoc.pTab[nTab]->aCol[nCol];
519 return pCol->pAttrArray;
520 }
521
IsQueryValid(ScDocument & rDoc,const ScQueryParam & rParam,SCTAB nTab,SCROW nRow,ScBaseCell * pCell)522 bool ScDBQueryDataIterator::IsQueryValid(ScDocument& rDoc, const ScQueryParam& rParam, SCTAB nTab, SCROW nRow, ScBaseCell* pCell)
523 {
524 return rDoc.pTab[nTab]->ValidQuery(nRow, rParam, NULL, pCell);
525 }
526
SearchColEntryIndex(ScDocument & rDoc,SCTAB nTab,SCROW nRow,SCCOL nCol)527 SCSIZE ScDBQueryDataIterator::SearchColEntryIndex(ScDocument& rDoc, SCTAB nTab, SCROW nRow, SCCOL nCol)
528 {
529 ScColumn* pCol = &rDoc.pTab[nTab]->aCol[nCol];
530 SCSIZE nColRow;
531 pCol->Search(nRow, nColRow);
532 return nColRow;
533 }
534
535 // ----------------------------------------------------------------------------
536
DataAccessInternal(const ScDBQueryDataIterator * pParent,ScDBQueryParamInternal * pParam,ScDocument * pDoc)537 ScDBQueryDataIterator::DataAccessInternal::DataAccessInternal(const ScDBQueryDataIterator* pParent, ScDBQueryParamInternal* pParam, ScDocument* pDoc) :
538 DataAccess(pParent),
539 mpParam(pParam),
540 mpDoc(pDoc),
541 bCalcAsShown( pDoc->GetDocOptions().IsCalcAsShown() )
542 {
543 nCol = mpParam->mnField;
544 nRow = mpParam->nRow1;
545 nTab = mpParam->nTab;
546
547 nColRow = 0; // initialized with GetFirst
548 SCSIZE i;
549 SCSIZE nCount = mpParam->GetEntryCount();
550 for (i=0; (i<nCount) && (mpParam->GetEntry(i).bDoQuery); i++)
551 {
552 ScQueryEntry& rEntry = mpParam->GetEntry(i);
553 sal_uInt32 nIndex = 0;
554 rEntry.bQueryByString =
555 !(mpDoc->GetFormatTable()->IsNumberFormat(*rEntry.pStr, nIndex, rEntry.nVal));
556 }
557 nNumFormat = 0; // Initialized with GetNumberFormat
558 pAttrArray = 0;
559 nAttrEndRow = 0;
560 }
561
~DataAccessInternal()562 ScDBQueryDataIterator::DataAccessInternal::~DataAccessInternal()
563 {
564 }
565
getCurrent(Value & rValue)566 bool ScDBQueryDataIterator::DataAccessInternal::getCurrent(Value& rValue)
567 {
568 SCCOLROW nFirstQueryField = mpParam->GetEntry(0).nField;
569 for ( ;; )
570 {
571 if (nRow > mpParam->nRow2)
572 {
573 // Bottom of the range reached. Bail out.
574 rValue.mnError = 0;
575 return false;
576 }
577
578 SCSIZE nCellCount = mpDoc->GetCellCount(nTab, nCol);
579 SCROW nThisRow = ScDBQueryDataIterator::GetRowByColEntryIndex(*mpDoc, nTab, nCol, nColRow);
580 while ( (nColRow < nCellCount) && (nThisRow < nRow) )
581 nThisRow = ScDBQueryDataIterator::GetRowByColEntryIndex(*mpDoc, nTab, nCol, ++nColRow);
582
583 if ( nColRow < nCellCount && nThisRow <= mpParam->nRow2 )
584 {
585 nRow = nThisRow;
586 ScBaseCell* pCell = NULL;
587 if (nCol == static_cast<SCCOL>(nFirstQueryField))
588 pCell = ScDBQueryDataIterator::GetCellByColEntryIndex(*mpDoc, nTab, nCol, nColRow);
589
590 if (ScDBQueryDataIterator::IsQueryValid(*mpDoc, *mpParam, nTab, nRow, pCell))
591 {
592 // #i109812# get cell here if it wasn't done above
593 if (nCol != static_cast<SCCOL>(nFirstQueryField))
594 pCell = ScDBQueryDataIterator::GetCellByColEntryIndex(*mpDoc, nTab, nCol, nColRow);
595
596 switch (pCell ? pCell->GetCellType() : CELLTYPE_NONE)
597 {
598 case CELLTYPE_VALUE:
599 {
600 rValue.mfValue = ((ScValueCell*)pCell)->GetValue();
601 rValue.mbIsNumber = true;
602 if ( bCalcAsShown )
603 {
604 const ScAttrArray* pNewAttrArray =
605 ScDBQueryDataIterator::GetAttrArrayByCol(*mpDoc, nTab, nCol);
606 lcl_IterGetNumberFormat( nNumFormat, pAttrArray,
607 nAttrEndRow, pNewAttrArray, nRow, mpDoc );
608 rValue.mfValue = mpDoc->RoundValueAsShown( rValue.mfValue, nNumFormat );
609 }
610 nNumFmtType = NUMBERFORMAT_NUMBER;
611 nNumFmtIndex = 0;
612 rValue.mnError = 0;
613 return sal_True; // found
614 }
615 // break;
616 case CELLTYPE_FORMULA:
617 {
618 if (((ScFormulaCell*)pCell)->IsValue())
619 {
620 rValue.mfValue = ((ScFormulaCell*)pCell)->GetValue();
621 rValue.mbIsNumber = true;
622 mpDoc->GetNumberFormatInfo( nNumFmtType,
623 nNumFmtIndex, ScAddress( nCol, nRow, nTab ),
624 pCell );
625 rValue.mnError = ((ScFormulaCell*)pCell)->GetErrCode();
626 return sal_True; // found
627 }
628 else
629 {
630 if (mpParam->mbSkipString)
631 ++nRow;
632 else
633 {
634 rValue.maString = ((ScFormulaCell*)pCell)->GetStringData();
635 rValue.mbIsNumber = false;
636 rValue.mnError = ((ScFormulaCell*)pCell)->GetErrCode();
637 return sal_True;
638 }
639 }
640 }
641 break;
642 case CELLTYPE_STRING:
643 case CELLTYPE_EDIT:
644 if (mpParam->mbSkipString)
645 ++nRow;
646 else
647 {
648 rValue.maString = pCell->GetStringData();
649 rValue.mfValue = 0.0;
650 rValue.mnError = 0;
651 rValue.mbIsNumber = false;
652 return true;
653 }
654 break;
655 default:
656 nRow++;
657 break;
658 }
659 }
660 else
661 nRow++;
662 }
663 else
664 nRow = mpParam->nRow2 + 1; // next Row
665 }
666 // statement unreachable
667 // return false;
668 }
669
getFirst(Value & rValue)670 bool ScDBQueryDataIterator::DataAccessInternal::getFirst(Value& rValue)
671 {
672 if (mpParam->bHasHeader)
673 nRow++;
674
675 nColRow = ScDBQueryDataIterator::SearchColEntryIndex(*mpDoc, nTab, nRow, nCol);
676 return getCurrent(rValue);
677 }
678
getNext(Value & rValue)679 bool ScDBQueryDataIterator::DataAccessInternal::getNext(Value& rValue)
680 {
681 ++nRow;
682 return getCurrent(rValue);
683 }
684
countRecords()685 SCSIZE ScDBQueryDataIterator::DataAccessInternal::countRecords()
686 {
687 // A record can match with an empty cell in a query column ("=", "<>5"),
688 // getCurrent() only visits rows that have a cell in column nCol.
689 ScQueryParam aParam(*mpParam);
690 SCROW nFirstRow = mpParam->bHasHeader ? mpParam->nRow1 + 1 : mpParam->nRow1;
691 SCROW nLastRow = mpParam->nRow2;
692 if (nFirstRow > nLastRow)
693 return 0;
694 // A row without any content in the database columns is not a record.
695 ::std::vector<bool> aHasContent(nLastRow - nFirstRow + 1, false);
696 for (SCCOL nC = mpParam->nCol1; nC <= mpParam->nCol2; ++nC)
697 {
698 SCSIZE nCellCount = mpDoc->GetCellCount(nTab, nC);
699 for (SCSIZE i = ScDBQueryDataIterator::SearchColEntryIndex(*mpDoc, nTab, nFirstRow, nC); i < nCellCount; ++i)
700 {
701 SCROW nThisRow = ScDBQueryDataIterator::GetRowByColEntryIndex(*mpDoc, nTab, nC, i);
702 if (nThisRow > nLastRow)
703 break;
704 ScBaseCell* pCell = ScDBQueryDataIterator::GetCellByColEntryIndex(*mpDoc, nTab, nC, i);
705 if (nThisRow >= nFirstRow && pCell && pCell->GetCellType() != CELLTYPE_NOTE)
706 aHasContent[nThisRow - nFirstRow] = true;
707 }
708 }
709 SCSIZE nCount = 0;
710 for (SCROW nR = nFirstRow; nR <= nLastRow; ++nR)
711 {
712 if (aHasContent[nR - nFirstRow] && ScDBQueryDataIterator::IsQueryValid(*mpDoc, aParam, nTab, nR, NULL))
713 ++nCount;
714 }
715 return nCount;
716 }
717
718 // ----------------------------------------------------------------------------
719
DataAccessMatrix(const ScDBQueryDataIterator * pParent,ScDBQueryParamMatrix * pParam)720 ScDBQueryDataIterator::DataAccessMatrix::DataAccessMatrix(const ScDBQueryDataIterator* pParent, ScDBQueryParamMatrix* pParam) :
721 DataAccess(pParent),
722 mpParam(pParam)
723 {
724 SCSIZE nC, nR;
725 mpParam->mpMatrix->GetDimensions(nC, nR);
726 mnRows = static_cast<SCROW>(nR);
727 mnCols = static_cast<SCCOL>(nC);
728 }
729
~DataAccessMatrix()730 ScDBQueryDataIterator::DataAccessMatrix::~DataAccessMatrix()
731 {
732 }
733
getCurrent(Value & rValue)734 bool ScDBQueryDataIterator::DataAccessMatrix::getCurrent(Value& rValue)
735 {
736 // Starting from row == mnCurRow, get the first row that satisfies all the
737 // query parameters.
738 for ( ;mnCurRow < mnRows; ++mnCurRow)
739 {
740 const ScMatrix& rMat = *mpParam->mpMatrix;
741 if (rMat.IsEmpty(mpParam->mnField, mnCurRow))
742 // Don't take empty values into account.
743 continue;
744
745 bool bIsStrVal = rMat.IsString(mpParam->mnField, mnCurRow);
746 if (bIsStrVal && mpParam->mbSkipString)
747 continue;
748
749 if (isValidQuery(mnCurRow, rMat))
750 {
751 rValue.maString = rMat.GetString(mpParam->mnField, mnCurRow);
752 rValue.mfValue = rMat.GetDouble(mpParam->mnField, mnCurRow);
753 rValue.mbIsNumber = !bIsStrVal;
754 rValue.mnError = 0;
755 return true;
756 }
757 }
758 return false;
759 }
760
getFirst(Value & rValue)761 bool ScDBQueryDataIterator::DataAccessMatrix::getFirst(Value& rValue)
762 {
763 mnCurRow = mpParam->bHasHeader ? 1 : 0;
764 return getCurrent(rValue);
765 }
766
getNext(Value & rValue)767 bool ScDBQueryDataIterator::DataAccessMatrix::getNext(Value& rValue)
768 {
769 ++mnCurRow;
770 return getCurrent(rValue);
771 }
772
countRecords()773 SCSIZE ScDBQueryDataIterator::DataAccessMatrix::countRecords()
774 {
775 const ScMatrix& rMat = *mpParam->mpMatrix;
776 SCSIZE nCount = 0;
777 for (SCROW nR = mpParam->bHasHeader ? 1 : 0; nR < mnRows; ++nR)
778 {
779 bool bHasContent = false;
780 for (SCCOL nC = 0; nC < mnCols && !bHasContent; ++nC)
781 bHasContent = !rMat.IsEmpty(nC, nR);
782 if (bHasContent && isValidQuery(nR, rMat))
783 ++nCount;
784 }
785 return nCount;
786 }
787
788 namespace {
789
lcl_isQueryByValue(const ScQueryEntry & rEntry,const ScMatrix & rMat,SCSIZE nCol,SCSIZE nRow)790 bool lcl_isQueryByValue(const ScQueryEntry& rEntry, const ScMatrix& rMat, SCSIZE nCol, SCSIZE nRow)
791 {
792 if (rEntry.bQueryByString)
793 return false;
794
795 if (!rMat.IsValueOrEmpty(nCol, nRow))
796 return false;
797
798 return true;
799 }
800
lcl_isQueryByString(const ScQueryEntry & rEntry,const ScMatrix & rMat,SCSIZE nCol,SCSIZE nRow)801 bool lcl_isQueryByString(const ScQueryEntry& rEntry, const ScMatrix& rMat, SCSIZE nCol, SCSIZE nRow)
802 {
803 switch (rEntry.eOp)
804 {
805 case SC_EQUAL:
806 case SC_NOT_EQUAL:
807 case SC_CONTAINS:
808 case SC_DOES_NOT_CONTAIN:
809 case SC_BEGINS_WITH:
810 case SC_ENDS_WITH:
811 case SC_DOES_NOT_BEGIN_WITH:
812 case SC_DOES_NOT_END_WITH:
813 return true;
814 default:
815 ;
816 }
817
818 if (rEntry.bQueryByString && rMat.IsString(nCol, nRow))
819 return true;
820
821 return false;
822 }
823
824 }
825
isValidQuery(SCROW nRow,const ScMatrix & rMat) const826 bool ScDBQueryDataIterator::DataAccessMatrix::isValidQuery(SCROW nRow, const ScMatrix& rMat) const
827 {
828 SCSIZE nEntryCount = mpParam->GetEntryCount();
829 vector<bool> aResults;
830 aResults.reserve(nEntryCount);
831
832 const CollatorWrapper& rCollator =
833 mpParam->bCaseSens ? *ScGlobal::GetCaseCollator() : *ScGlobal::GetCollator();
834
835 for (SCSIZE i = 0; i < nEntryCount; ++i)
836 {
837 const ScQueryEntry& rEntry = mpParam->GetEntry(i);
838 if (!rEntry.bDoQuery)
839 continue;
840
841 switch (rEntry.eOp)
842 {
843 case SC_EQUAL:
844 case SC_LESS:
845 case SC_GREATER:
846 case SC_LESS_EQUAL:
847 case SC_GREATER_EQUAL:
848 case SC_NOT_EQUAL:
849 break;
850 default:
851 // Only the above operators are supported.
852 continue;
853 }
854
855 bool bValid = false;
856
857 SCSIZE nField = static_cast<SCSIZE>(rEntry.nField);
858 if (lcl_isQueryByValue(rEntry, rMat, nField, nRow))
859 {
860 // By value
861 double fMatVal = rMat.GetDouble(nField, nRow);
862 bool bEqual = approxEqual(fMatVal, rEntry.nVal);
863 switch (rEntry.eOp)
864 {
865 case SC_EQUAL:
866 bValid = bEqual;
867 break;
868 case SC_LESS:
869 bValid = (fMatVal < rEntry.nVal) && !bEqual;
870 break;
871 case SC_GREATER:
872 bValid = (fMatVal > rEntry.nVal) && !bEqual;
873 break;
874 case SC_LESS_EQUAL:
875 bValid = (fMatVal < rEntry.nVal) || bEqual;
876 break;
877 case SC_GREATER_EQUAL:
878 bValid = (fMatVal > rEntry.nVal) || bEqual;
879 break;
880 case SC_NOT_EQUAL:
881 bValid = !bEqual;
882 break;
883 default:
884 ;
885 }
886 }
887 else if (lcl_isQueryByString(rEntry, rMat, nField, nRow))
888 {
889 // By string
890 do
891 {
892 if (!rEntry.pStr)
893 break;
894
895 // Equality check first.
896
897 OUString aMatStr = rMat.GetString(nField, nRow);
898 lcl_toUpper(aMatStr);
899 OUString aQueryStr = *rEntry.pStr;
900 lcl_toUpper(aQueryStr);
901 bool bDone = false;
902 switch (rEntry.eOp)
903 {
904 case SC_EQUAL:
905 bValid = aMatStr.equals(aQueryStr);
906 bDone = true;
907 break;
908 case SC_NOT_EQUAL:
909 bValid = !aMatStr.equals(aQueryStr);
910 bDone = true;
911 break;
912 default:
913 ;
914 }
915
916 if (bDone)
917 break;
918
919 // Unequality check using collator.
920
921 sal_Int32 nCompare = rCollator.compareString(aMatStr, aQueryStr);
922 switch (rEntry.eOp)
923 {
924 case SC_LESS :
925 bValid = (nCompare < 0);
926 break;
927 case SC_GREATER :
928 bValid = (nCompare > 0);
929 break;
930 case SC_LESS_EQUAL :
931 bValid = (nCompare <= 0);
932 break;
933 case SC_GREATER_EQUAL :
934 bValid = (nCompare >= 0);
935 break;
936 default:
937 ;
938 }
939 }
940 while (false);
941 }
942 else if (mpParam->bMixedComparison)
943 {
944 // Not used at the moment.
945 }
946
947 if (aResults.empty())
948 // First query entry.
949 aResults.push_back(bValid);
950 else if (rEntry.eConnect == SC_AND)
951 {
952 // For AND op, tuck the result into the last result value.
953 size_t n = aResults.size();
954 aResults[n-1] = aResults[n-1] && bValid;
955 }
956 else
957 // For OR op, store its own result.
958 aResults.push_back(bValid);
959 }
960
961 // Row is valid as long as there is at least one result being true.
962 vector<bool>::const_iterator itr = aResults.begin(), itrEnd = aResults.end();
963 for (; itr != itrEnd; ++itr)
964 if (*itr)
965 return true;
966
967 return false;
968 }
969
970 // ----------------------------------------------------------------------------
971
Value()972 ScDBQueryDataIterator::Value::Value() :
973 mnError(0), mbIsNumber(true)
974 {
975 ::rtl::math::setNan(&mfValue);
976 }
977
978 // ----------------------------------------------------------------------------
979
ScDBQueryDataIterator(ScDocument * pDocument,ScDBQueryParamBase * pParam)980 ScDBQueryDataIterator::ScDBQueryDataIterator(ScDocument* pDocument, ScDBQueryParamBase* pParam) :
981 mpParam (pParam)
982 {
983 switch (mpParam->GetType())
984 {
985 case ScDBQueryParamBase::INTERNAL:
986 {
987 ScDBQueryParamInternal* p = static_cast<ScDBQueryParamInternal*>(pParam);
988 mpData.reset(new DataAccessInternal(this, p, pDocument));
989 }
990 break;
991 case ScDBQueryParamBase::MATRIX:
992 {
993 ScDBQueryParamMatrix* p = static_cast<ScDBQueryParamMatrix*>(pParam);
994 mpData.reset(new DataAccessMatrix(this, p));
995 }
996 }
997 }
998
GetFirst(Value & rValue)999 bool ScDBQueryDataIterator::GetFirst(Value& rValue)
1000 {
1001 return mpData->getFirst(rValue);
1002 }
1003
GetNext(Value & rValue)1004 bool ScDBQueryDataIterator::GetNext(Value& rValue)
1005 {
1006 return mpData->getNext(rValue);
1007 }
1008
CountRecords()1009 SCSIZE ScDBQueryDataIterator::CountRecords()
1010 {
1011 return mpData->countRecords();
1012 }
1013
1014 // ============================================================================
1015
ScCellIterator(ScDocument * pDocument,SCCOL nSCol,SCROW nSRow,SCTAB nSTab,SCCOL nECol,SCROW nERow,SCTAB nETab,sal_Bool bSTotal)1016 ScCellIterator::ScCellIterator( ScDocument* pDocument,
1017 SCCOL nSCol, SCROW nSRow, SCTAB nSTab,
1018 SCCOL nECol, SCROW nERow, SCTAB nETab, sal_Bool bSTotal ) :
1019 pDoc( pDocument ),
1020 nStartCol( nSCol),
1021 nStartRow( nSRow),
1022 nStartTab( nSTab ),
1023 nEndCol( nECol ),
1024 nEndRow( nERow),
1025 nEndTab( nETab ),
1026 bSubTotal(bSTotal)
1027
1028 {
1029 PutInOrder( nStartCol, nEndCol);
1030 PutInOrder( nStartRow, nEndRow);
1031 PutInOrder( nStartTab, nEndTab );
1032
1033 if (!ValidCol(nStartCol)) nStartCol = MAXCOL;
1034 if (!ValidCol(nEndCol)) nEndCol = MAXCOL;
1035 if (!ValidRow(nStartRow)) nStartRow = MAXROW;
1036 if (!ValidRow(nEndRow)) nEndRow = MAXROW;
1037 if (!ValidTab(nStartTab)) nStartTab = MAXTAB;
1038 if (!ValidTab(nEndTab)) nEndTab = MAXTAB;
1039
1040 while (nEndTab>0 && !pDoc->pTab[nEndTab])
1041 --nEndTab; // only used tables
1042 if (nStartTab>nEndTab)
1043 nStartTab = nEndTab;
1044
1045 nCol = nStartCol;
1046 nRow = nStartRow;
1047 nTab = nStartTab;
1048 nColRow = 0; // initialized with GetFirst
1049
1050 if (!pDoc->pTab[nTab])
1051 {
1052 DBG_ERROR("Tabelle nicht gefunden");
1053 nStartCol = nCol = MAXCOL+1;
1054 nStartRow = nRow = MAXROW+1;
1055 nStartTab = nTab = MAXTAB+1; // -> cancel with GetFirst
1056 }
1057 }
1058
ScCellIterator(ScDocument * pDocument,const ScRange & rRange,sal_Bool bSTotal)1059 ScCellIterator::ScCellIterator
1060 ( ScDocument* pDocument, const ScRange& rRange, sal_Bool bSTotal ) :
1061 pDoc( pDocument ),
1062 nStartCol( rRange.aStart.Col() ),
1063 nStartRow( rRange.aStart.Row() ),
1064 nStartTab( rRange.aStart.Tab() ),
1065 nEndCol( rRange.aEnd.Col() ),
1066 nEndRow( rRange.aEnd.Row() ),
1067 nEndTab( rRange.aEnd.Tab() ),
1068 bSubTotal(bSTotal)
1069
1070 {
1071 PutInOrder( nStartCol, nEndCol);
1072 PutInOrder( nStartRow, nEndRow);
1073 PutInOrder( nStartTab, nEndTab );
1074
1075 if (!ValidCol(nStartCol)) nStartCol = MAXCOL;
1076 if (!ValidCol(nEndCol)) nEndCol = MAXCOL;
1077 if (!ValidRow(nStartRow)) nStartRow = MAXROW;
1078 if (!ValidRow(nEndRow)) nEndRow = MAXROW;
1079 if (!ValidTab(nStartTab)) nStartTab = MAXTAB;
1080 if (!ValidTab(nEndTab)) nEndTab = MAXTAB;
1081
1082 while (nEndTab>0 && !pDoc->pTab[nEndTab])
1083 --nEndTab; // only used tables
1084 if (nStartTab>nEndTab)
1085 nStartTab = nEndTab;
1086
1087 nCol = nStartCol;
1088 nRow = nStartRow;
1089 nTab = nStartTab;
1090 nColRow = 0; // initialized with GetFirst
1091
1092 if (!pDoc->pTab[nTab])
1093 {
1094 DBG_ERROR("Tabelle nicht gefunden");
1095 nStartCol = nCol = MAXCOL+1;
1096 nStartRow = nRow = MAXROW+1;
1097 nStartTab = nTab = MAXTAB+1; // -> cancel at GetFirst
1098 }
1099 }
1100
GetThis()1101 ScBaseCell* ScCellIterator::GetThis()
1102 {
1103 ScColumn* pCol = &(pDoc->pTab[nTab])->aCol[nCol];
1104 for ( ;; )
1105 {
1106 if ( nRow > nEndRow )
1107 {
1108 nRow = nStartRow;
1109 do
1110 {
1111 nCol++;
1112 if ( nCol > nEndCol )
1113 {
1114 nCol = nStartCol;
1115 nTab++;
1116 if ( nTab > nEndTab )
1117 return NULL; // Fine
1118 }
1119 pCol = &(pDoc->pTab[nTab])->aCol[nCol];
1120 } while ( pCol->nCount == 0 );
1121 pCol->Search( nRow, nColRow );
1122 }
1123
1124 while ( (nColRow < pCol->nCount) && (pCol->pItems[nColRow].nRow < nRow) )
1125 nColRow++;
1126
1127 if ( nColRow < pCol->nCount && pCol->pItems[nColRow].nRow <= nEndRow )
1128 {
1129 nRow = pCol->pItems[nColRow].nRow;
1130 if ( !bSubTotal || !pDoc->pTab[nTab]->RowFiltered( nRow ) )
1131 {
1132 ScBaseCell* pCell = pCol->pItems[nColRow].pCell;
1133
1134 if ( bSubTotal && pCell->GetCellType() == CELLTYPE_FORMULA
1135 && ((ScFormulaCell*)pCell)->IsSubTotal() )
1136 nRow++; // Sub-Total-rows not found
1137 else
1138 return pCell; // Sub-Total-rows found
1139 }
1140 else
1141 nRow++;
1142 }
1143 else
1144 nRow = nEndRow + 1; // next row
1145 }
1146 }
1147
GetFirst()1148 ScBaseCell* ScCellIterator::GetFirst()
1149 {
1150 if ( !ValidTab(nTab) )
1151 return NULL;
1152 nCol = nStartCol;
1153 nRow = nStartRow;
1154 nTab = nStartTab;
1155 // nColRow = 0;
1156 ScColumn* pCol = &(pDoc->pTab[nTab])->aCol[nCol];
1157 pCol->Search( nRow, nColRow );
1158 return GetThis();
1159 }
1160
GetNext()1161 ScBaseCell* ScCellIterator::GetNext()
1162 {
1163 ++nRow;
1164 return GetThis();
1165 }
1166
1167 //-------------------------------------------------------------------------------
1168
ScQueryCellIterator(ScDocument * pDocument,SCTAB nTable,const ScQueryParam & rParam,sal_Bool bMod)1169 ScQueryCellIterator::ScQueryCellIterator(ScDocument* pDocument, SCTAB nTable,
1170 const ScQueryParam& rParam, sal_Bool bMod ) :
1171 aParam (rParam),
1172 pDoc( pDocument ),
1173 nTab( nTable),
1174 nStopOnMismatch( nStopOnMismatchDisabled ),
1175 nTestEqualCondition( nTestEqualConditionDisabled ),
1176 bAdvanceQuery( sal_False ),
1177 bIgnoreMismatchOnLeadingStrings( sal_False )
1178 {
1179 nCol = aParam.nCol1;
1180 nRow = aParam.nRow1;
1181 nColRow = 0; // initialized with GetFirst
1182 SCSIZE i;
1183 if (bMod) // recorded otherwise
1184 {
1185 for (i=0; (i<MAXQUERY) && (aParam.GetEntry(i).bDoQuery); i++)
1186 {
1187 ScQueryEntry& rEntry = aParam.GetEntry(i);
1188 sal_uInt32 nIndex = 0;
1189 rEntry.bQueryByString =
1190 !(pDoc->GetFormatTable()->IsNumberFormat(*rEntry.pStr,
1191 nIndex, rEntry.nVal));
1192 }
1193 }
1194 nNumFormat = 0; // initialized with GetNumberFormat
1195 pAttrArray = 0;
1196 nAttrEndRow = 0;
1197 }
1198
GetThis()1199 ScBaseCell* ScQueryCellIterator::GetThis()
1200 {
1201 ScColumn* pCol = &(pDoc->pTab[nTab])->aCol[nCol];
1202 const ScQueryEntry& rEntry = aParam.GetEntry(0);
1203 SCCOLROW nFirstQueryField = rEntry.nField;
1204 bool bAllStringIgnore = bIgnoreMismatchOnLeadingStrings &&
1205 !rEntry.bQueryByString;
1206 bool bFirstStringIgnore = bIgnoreMismatchOnLeadingStrings &&
1207 !aParam.bHasHeader && rEntry.bQueryByString &&
1208 ((aParam.bByRow && nRow == aParam.nRow1) ||
1209 (!aParam.bByRow && nCol == aParam.nCol1));
1210 for ( ;; )
1211 {
1212 if ( nRow > aParam.nRow2 )
1213 {
1214 nRow = aParam.nRow1;
1215 if (aParam.bHasHeader && aParam.bByRow)
1216 nRow++;
1217 do
1218 {
1219 if ( ++nCol > aParam.nCol2 )
1220 return NULL; // fine
1221 if ( bAdvanceQuery )
1222 {
1223 AdvanceQueryParamEntryField();
1224 nFirstQueryField = rEntry.nField;
1225 }
1226 pCol = &(pDoc->pTab[nTab])->aCol[nCol];
1227 } while ( pCol->nCount == 0 );
1228 pCol->Search( nRow, nColRow );
1229 bFirstStringIgnore = bIgnoreMismatchOnLeadingStrings &&
1230 !aParam.bHasHeader && rEntry.bQueryByString &&
1231 aParam.bByRow;
1232 }
1233
1234 while ( nColRow < pCol->nCount && pCol->pItems[nColRow].nRow < nRow )
1235 nColRow++;
1236
1237 if ( nColRow < pCol->nCount &&
1238 (nRow = pCol->pItems[nColRow].nRow) <= aParam.nRow2 )
1239 {
1240 ScBaseCell* pCell = pCol->pItems[nColRow].pCell;
1241 if ( pCell->GetCellType() == CELLTYPE_NOTE )
1242 ++nRow;
1243 else if (bAllStringIgnore && pCell->HasStringData())
1244 ++nRow;
1245 else
1246 {
1247 sal_Bool bTestEqualCondition;
1248 if ( (pDoc->pTab[nTab])->ValidQuery( nRow, aParam, NULL,
1249 (nCol == static_cast<SCCOL>(nFirstQueryField) ? pCell : NULL),
1250 (nTestEqualCondition ? &bTestEqualCondition : NULL) ) )
1251 {
1252 if ( nTestEqualCondition && bTestEqualCondition )
1253 nTestEqualCondition |= nTestEqualConditionMatched;
1254 return pCell; // found
1255 }
1256 else if ( nStopOnMismatch )
1257 {
1258 // Yes, even a mismatch may have a fulfilled equal
1259 // condition if regular expressions were involved and
1260 // SC_LESS_EQUAL or SC_GREATER_EQUAL were queried.
1261 if ( nTestEqualCondition && bTestEqualCondition )
1262 {
1263 nTestEqualCondition |= nTestEqualConditionMatched;
1264 nStopOnMismatch |= nStopOnMismatchOccured;
1265 return NULL;
1266 }
1267 bool bStop;
1268 if (bFirstStringIgnore)
1269 {
1270 if (pCell->HasStringData())
1271 {
1272 ++nRow;
1273 bStop = false;
1274 }
1275 else
1276 bStop = true;
1277 }
1278 else
1279 bStop = true;
1280 if (bStop)
1281 {
1282 nStopOnMismatch |= nStopOnMismatchOccured;
1283 return NULL;
1284 }
1285 }
1286 else
1287 nRow++;
1288 }
1289 }
1290 else
1291 nRow = aParam.nRow2 + 1; // next row
1292 bFirstStringIgnore = false;
1293 }
1294 }
1295
GetFirst()1296 ScBaseCell* ScQueryCellIterator::GetFirst()
1297 {
1298 nCol = aParam.nCol1;
1299 nRow = aParam.nRow1;
1300 if (aParam.bHasHeader)
1301 nRow++;
1302 // nColRow = 0;
1303 ScColumn* pCol = &(pDoc->pTab[nTab])->aCol[nCol];
1304 pCol->Search( nRow, nColRow );
1305 return GetThis();
1306 }
1307
GetNext()1308 ScBaseCell* ScQueryCellIterator::GetNext()
1309 {
1310 ++nRow;
1311 if ( nStopOnMismatch )
1312 nStopOnMismatch = nStopOnMismatchEnabled;
1313 if ( nTestEqualCondition )
1314 nTestEqualCondition = nTestEqualConditionEnabled;
1315 return GetThis();
1316 }
1317
AdvanceQueryParamEntryField()1318 void ScQueryCellIterator::AdvanceQueryParamEntryField()
1319 {
1320 SCSIZE nEntries = aParam.GetEntryCount();
1321 for ( SCSIZE j = 0; j < nEntries; j++ )
1322 {
1323 ScQueryEntry& rEntry = aParam.GetEntry( j );
1324 if ( rEntry.bDoQuery )
1325 {
1326 if ( rEntry.nField < MAXCOL )
1327 rEntry.nField++;
1328 else
1329 {
1330 DBG_ERRORFILE( "AdvanceQueryParamEntryField: ++rEntry.nField > MAXCOL" );
1331 }
1332 }
1333 else
1334 break; // for
1335 }
1336 }
1337
1338
FindEqualOrSortedLastInRange(SCCOL & nFoundCol,SCROW & nFoundRow,sal_Bool bSearchForEqualAfterMismatch,sal_Bool bIgnoreMismatchOnLeadingStringsP)1339 sal_Bool ScQueryCellIterator::FindEqualOrSortedLastInRange( SCCOL& nFoundCol,
1340 SCROW& nFoundRow, sal_Bool bSearchForEqualAfterMismatch,
1341 sal_Bool bIgnoreMismatchOnLeadingStringsP )
1342 {
1343 nFoundCol = MAXCOL+1;
1344 nFoundRow = MAXROW+1;
1345 SetStopOnMismatch( sal_True ); // assume sorted keys
1346 SetTestEqualCondition( sal_True );
1347 bIgnoreMismatchOnLeadingStrings = bIgnoreMismatchOnLeadingStringsP;
1348 bool bRegExp = aParam.bRegExp && aParam.GetEntry(0).bQueryByString;
1349 bool bBinary = !bRegExp && aParam.bByRow && (aParam.GetEntry(0).eOp ==
1350 SC_LESS_EQUAL || aParam.GetEntry(0).eOp == SC_GREATER_EQUAL);
1351 if (bBinary ? (BinarySearch() ? GetThis() : 0) : GetFirst())
1352 {
1353 // First equal entry or last smaller than (greater than) entry.
1354 SCSIZE nColRowSave;
1355 ScBaseCell* pNext = 0;
1356 do
1357 {
1358 nFoundCol = GetCol();
1359 nFoundRow = GetRow();
1360 nColRowSave = nColRow;
1361 } while ( !IsEqualConditionFulfilled() && (pNext = GetNext()) != NULL );
1362 // There may be no pNext but equal condition fulfilled if regular
1363 // expressions are involved. Keep the found entry and proceed.
1364 if (!pNext && !IsEqualConditionFulfilled())
1365 {
1366 // Step back to last in range and adjust position markers for
1367 // GetNumberFormat() or similar.
1368 nCol = nFoundCol;
1369 nRow = nFoundRow;
1370 nColRow = nColRowSave;
1371 }
1372 }
1373 if ( IsEqualConditionFulfilled() )
1374 {
1375 // Position on last equal entry.
1376 SCSIZE nEntries = aParam.GetEntryCount();
1377 for ( SCSIZE j = 0; j < nEntries; j++ )
1378 {
1379 ScQueryEntry& rEntry = aParam.GetEntry( j );
1380 if ( rEntry.bDoQuery )
1381 {
1382 switch ( rEntry.eOp )
1383 {
1384 case SC_LESS_EQUAL :
1385 case SC_GREATER_EQUAL :
1386 rEntry.eOp = SC_EQUAL;
1387 break;
1388 default:
1389 {
1390 // added to avoid warnings
1391 }
1392 }
1393 }
1394 else
1395 break; // for
1396 }
1397 SCSIZE nColRowSave;
1398 bIgnoreMismatchOnLeadingStrings = sal_False;
1399 SetTestEqualCondition( sal_False );
1400 do
1401 {
1402 nFoundCol = GetCol();
1403 nFoundRow = GetRow();
1404 nColRowSave = nColRow;
1405 } while (GetNext());
1406 // Step back conditions same as above
1407 nCol = nFoundCol;
1408 nRow = nFoundRow;
1409 nColRow = nColRowSave;
1410 return sal_True;
1411 }
1412 if ( (bSearchForEqualAfterMismatch || aParam.bRegExp) &&
1413 StoppedOnMismatch() )
1414 {
1415 // Assume found entry to be the last value less than respectively
1416 // greater than the query. But keep on searching for an equal match.
1417 SCSIZE nEntries = aParam.GetEntryCount();
1418 for ( SCSIZE j = 0; j < nEntries; j++ )
1419 {
1420 ScQueryEntry& rEntry = aParam.GetEntry( j );
1421 if ( rEntry.bDoQuery )
1422 {
1423 switch ( rEntry.eOp )
1424 {
1425 case SC_LESS_EQUAL :
1426 case SC_GREATER_EQUAL :
1427 rEntry.eOp = SC_EQUAL;
1428 break;
1429 default:
1430 {
1431 // added to avoid warnings
1432 }
1433 }
1434 }
1435 else
1436 break; // for
1437 }
1438 SetStopOnMismatch( sal_False );
1439 SetTestEqualCondition( sal_False );
1440 if (GetNext())
1441 {
1442 // Last of a consecutive area, avoid searching the entire parameter
1443 // range as it is a real performance bottleneck in case of regular
1444 // expressions.
1445 SCSIZE nColRowSave;
1446 do
1447 {
1448 nFoundCol = GetCol();
1449 nFoundRow = GetRow();
1450 nColRowSave = nColRow;
1451 SetStopOnMismatch( sal_True );
1452 } while (GetNext());
1453 nCol = nFoundCol;
1454 nRow = nFoundRow;
1455 nColRow = nColRowSave;
1456 }
1457 }
1458 return (nFoundCol <= MAXCOL) && (nFoundRow <= MAXROW);
1459 }
1460
1461
BinarySearch()1462 ScBaseCell* ScQueryCellIterator::BinarySearch()
1463 {
1464 nCol = aParam.nCol1;
1465 ScColumn* pCol = &(pDoc->pTab[nTab])->aCol[nCol];
1466 if (!pCol->nCount)
1467 return 0;
1468
1469 ScBaseCell* pCell;
1470 SCSIZE nHi, nLo;
1471 CollatorWrapper* pCollator = (aParam.bCaseSens ? ScGlobal::GetCaseCollator() :
1472 ScGlobal::GetCollator());
1473 SvNumberFormatter& rFormatter = *(pDoc->GetFormatTable());
1474 const ScQueryEntry& rEntry = aParam.GetEntry(0);
1475 bool bLessEqual = rEntry.eOp == SC_LESS_EQUAL;
1476 bool bByString = rEntry.bQueryByString;
1477 bool bAllStringIgnore = bIgnoreMismatchOnLeadingStrings && !bByString;
1478 bool bFirstStringIgnore = bIgnoreMismatchOnLeadingStrings &&
1479 !aParam.bHasHeader && bByString;
1480
1481 nRow = aParam.nRow1;
1482 if (aParam.bHasHeader)
1483 nRow++;
1484 const ColEntry* pItems = pCol->pItems;
1485 if (pCol->Search( nRow, nLo ) && bFirstStringIgnore &&
1486 pItems[nLo].pCell->HasStringData())
1487 {
1488 String aCellStr;
1489 sal_uLong nFormat = pCol->GetNumberFormat( pItems[nLo].nRow);
1490 ScCellFormat::GetInputString( pItems[nLo].pCell, nFormat, aCellStr,
1491 rFormatter);
1492 sal_Int32 nTmp = pCollator->compareString( aCellStr, *rEntry.pStr);
1493 if ((rEntry.eOp == SC_LESS_EQUAL && nTmp > 0) ||
1494 (rEntry.eOp == SC_GREATER_EQUAL && nTmp < 0) ||
1495 (rEntry.eOp == SC_EQUAL && nTmp != 0))
1496 ++nLo;
1497 }
1498 if (!pCol->Search( aParam.nRow2, nHi ) && nHi>0)
1499 --nHi;
1500 while (bAllStringIgnore && nLo <= nHi && nLo < pCol->nCount &&
1501 pItems[nLo].pCell->HasStringData())
1502 ++nLo;
1503
1504 // Bookkeeping values for breaking up the binary search in case the data
1505 // range isn't strictly sorted.
1506 SCSIZE nLastInRange = nLo;
1507 SCSIZE nFirstLastInRange = nLastInRange;
1508 double fLastInRangeValue = bLessEqual ?
1509 -(::std::numeric_limits<double>::max()) :
1510 ::std::numeric_limits<double>::max();
1511 String aLastInRangeString;
1512 if (!bLessEqual)
1513 aLastInRangeString.Assign( sal_Unicode(0xFFFF));
1514 if (nLastInRange < pCol->nCount)
1515 {
1516 pCell = pItems[nLastInRange].pCell;
1517 if (pCell->HasStringData())
1518 {
1519 sal_uLong nFormat = pCol->GetNumberFormat( pItems[nLastInRange].nRow);
1520 ScCellFormat::GetInputString( pCell, nFormat, aLastInRangeString,
1521 rFormatter);
1522 }
1523 else
1524 {
1525 switch ( pCell->GetCellType() )
1526 {
1527 case CELLTYPE_VALUE :
1528 fLastInRangeValue =
1529 static_cast<ScValueCell*>(pCell)->GetValue();
1530 break;
1531 case CELLTYPE_FORMULA :
1532 fLastInRangeValue =
1533 static_cast<ScFormulaCell*>(pCell)->GetValue();
1534 break;
1535 default:
1536 {
1537 // added to avoid warnings
1538 }
1539 }
1540 }
1541 }
1542
1543 sal_Int32 nRes = 0;
1544 bool bFound = false;
1545 bool bDone = false;
1546 while (nLo <= nHi && !bDone)
1547 {
1548 SCSIZE nMid = (nLo+nHi)/2;
1549 SCSIZE i = nMid;
1550 while (i <= nHi && pItems[i].pCell->GetCellType() == CELLTYPE_NOTE)
1551 ++i;
1552 if (i > nHi)
1553 {
1554 if (nMid > 0)
1555 nHi = nMid - 1;
1556 else
1557 bDone = true;
1558 continue; // while
1559 }
1560 sal_Bool bStr = pItems[i].pCell->HasStringData();
1561 nRes = 0;
1562 // compares are content<query:-1, content>query:1
1563 // Cell value comparison similar to ScTable::ValidQuery()
1564 if (!bStr && !bByString)
1565 {
1566 double nCellVal;
1567 pCell = pItems[i].pCell;
1568 switch ( pCell->GetCellType() )
1569 {
1570 case CELLTYPE_VALUE :
1571 nCellVal = static_cast<ScValueCell*>(pCell)->GetValue();
1572 break;
1573 case CELLTYPE_FORMULA :
1574 nCellVal = static_cast<ScFormulaCell*>(pCell)->GetValue();
1575 break;
1576 default:
1577 nCellVal = 0.0;
1578 }
1579 if ((nCellVal < rEntry.nVal) && !::rtl::math::approxEqual(
1580 nCellVal, rEntry.nVal))
1581 {
1582 nRes = -1;
1583 if (bLessEqual)
1584 {
1585 if (fLastInRangeValue < nCellVal)
1586 {
1587 fLastInRangeValue = nCellVal;
1588 nLastInRange = i;
1589 }
1590 else if (fLastInRangeValue > nCellVal)
1591 {
1592 // not strictly sorted, continue with GetThis()
1593 nLastInRange = nFirstLastInRange;
1594 bDone = true;
1595 }
1596 }
1597 }
1598 else if ((nCellVal > rEntry.nVal) && !::rtl::math::approxEqual(
1599 nCellVal, rEntry.nVal))
1600 {
1601 nRes = 1;
1602 if (!bLessEqual)
1603 {
1604 if (fLastInRangeValue > nCellVal)
1605 {
1606 fLastInRangeValue = nCellVal;
1607 nLastInRange = i;
1608 }
1609 else if (fLastInRangeValue < nCellVal)
1610 {
1611 // not strictly sorted, continue with GetThis()
1612 nLastInRange = nFirstLastInRange;
1613 bDone = true;
1614 }
1615 }
1616 }
1617 }
1618 else if (bStr && bByString)
1619 {
1620 String aCellStr;
1621 sal_uLong nFormat = pCol->GetNumberFormat( pItems[i].nRow);
1622 ScCellFormat::GetInputString( pItems[i].pCell, nFormat, aCellStr,
1623 rFormatter);
1624 nRes = pCollator->compareString( aCellStr, *rEntry.pStr);
1625 if (nRes < 0 && bLessEqual)
1626 {
1627 sal_Int32 nTmp = pCollator->compareString( aLastInRangeString,
1628 aCellStr);
1629 if (nTmp < 0)
1630 {
1631 aLastInRangeString = aCellStr;
1632 nLastInRange = i;
1633 }
1634 else if (nTmp > 0)
1635 {
1636 // not strictly sorted, continue with GetThis()
1637 nLastInRange = nFirstLastInRange;
1638 bDone = true;
1639 }
1640 }
1641 else if (nRes > 0 && !bLessEqual)
1642 {
1643 sal_Int32 nTmp = pCollator->compareString( aLastInRangeString,
1644 aCellStr);
1645 if (nTmp > 0)
1646 {
1647 aLastInRangeString = aCellStr;
1648 nLastInRange = i;
1649 }
1650 else if (nTmp < 0)
1651 {
1652 // not strictly sorted, continue with GetThis()
1653 nLastInRange = nFirstLastInRange;
1654 bDone = true;
1655 }
1656 }
1657 }
1658 else if (!bStr && bByString)
1659 {
1660 nRes = -1; // numeric < string
1661 if (bLessEqual)
1662 nLastInRange = i;
1663 }
1664 else // if (bStr && !bByString)
1665 {
1666 nRes = 1; // string > numeric
1667 if (!bLessEqual)
1668 nLastInRange = i;
1669 }
1670 if (nRes < 0)
1671 {
1672 if (bLessEqual)
1673 nLo = nMid + 1;
1674 else // assumed to be SC_GREATER_EQUAL
1675 {
1676 if (nMid > 0)
1677 nHi = nMid - 1;
1678 else
1679 bDone = true;
1680 }
1681 }
1682 else if (nRes > 0)
1683 {
1684 if (bLessEqual)
1685 {
1686 if (nMid > 0)
1687 nHi = nMid - 1;
1688 else
1689 bDone = true;
1690 }
1691 else // assumed to be SC_GREATER_EQUAL
1692 nLo = nMid + 1;
1693 }
1694 else
1695 {
1696 nLo = i;
1697 bDone = bFound = true;
1698 }
1699 }
1700 if (!bFound)
1701 {
1702 // If all hits didn't result in a moving limit there's something
1703 // strange, e.g. data range not properly sorted, or only identical
1704 // values encountered, which doesn't mean there aren't any others in
1705 // between.. leave it to GetThis(). The condition for this would be
1706 // if (nLastInRange == nFirstLastInRange) nLo = nFirstLastInRange;
1707 // Else, in case no exact match was found, we step back for a
1708 // subsequent GetThis() to find the last in range. Effectively this is
1709 // --nLo with nLastInRange == nLo-1. Both conditions combined yield:
1710 nLo = nLastInRange;
1711 }
1712 if (nLo < pCol->nCount && pCol->pItems[nLo].nRow <= aParam.nRow2)
1713 {
1714 nRow = pItems[nLo].nRow;
1715 pCell = pItems[nLo].pCell;
1716 nColRow = nLo;
1717 }
1718 else
1719 {
1720 nRow = aParam.nRow2 + 1;
1721 pCell = 0;
1722 nColRow = pCol->nCount - 1;
1723 }
1724 return pCell;
1725 }
1726
1727
1728 //-------------------------------------------------------------------------------
1729
ScHorizontalCellIterator(ScDocument * pDocument,SCTAB nTable,SCCOL nCol1,SCROW nRow1,SCCOL nCol2,SCROW nRow2)1730 ScHorizontalCellIterator::ScHorizontalCellIterator(ScDocument* pDocument, SCTAB nTable,
1731 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2 ) :
1732 pDoc( pDocument ),
1733 nTab( nTable ),
1734 nStartCol( nCol1 ),
1735 nEndCol( nCol2 ),
1736 nStartRow( nRow1 ),
1737 nEndRow( nRow2 ),
1738 nCol( nCol1 ),
1739 nRow( nRow1 ),
1740 bMore( sal_True )
1741 {
1742
1743 pNextRows = new SCROW[ nCol2-nCol1+1 ];
1744 pNextIndices = new SCSIZE[ nCol2-nCol1+1 ];
1745
1746 SetTab( nTab );
1747 }
1748
~ScHorizontalCellIterator()1749 ScHorizontalCellIterator::~ScHorizontalCellIterator()
1750 {
1751 delete [] pNextRows;
1752 delete [] pNextIndices;
1753 }
1754
SetTab(SCTAB nTabP)1755 void ScHorizontalCellIterator::SetTab( SCTAB nTabP )
1756 {
1757 nTab = nTabP;
1758 nRow = nStartRow;
1759 nCol = nStartCol;
1760 bMore = sal_True;
1761
1762 for (SCCOL i=nStartCol; i<=nEndCol; i++)
1763 {
1764 ScColumn* pCol = &pDoc->pTab[nTab]->aCol[i];
1765
1766 SCSIZE nIndex;
1767 pCol->Search( nStartRow, nIndex );
1768 if ( nIndex < pCol->nCount )
1769 {
1770 pNextRows[i-nStartCol] = pCol->pItems[nIndex].nRow;
1771 pNextIndices[i-nStartCol] = nIndex;
1772 }
1773 else
1774 {
1775 pNextRows[i-nStartCol] = MAXROWCOUNT; // nothing found
1776 pNextIndices[i-nStartCol] = MAXROWCOUNT;
1777 }
1778 }
1779
1780 if (pNextRows[0] != nStartRow)
1781 Advance();
1782 }
1783
GetNext(SCCOL & rCol,SCROW & rRow)1784 ScBaseCell* ScHorizontalCellIterator::GetNext( SCCOL& rCol, SCROW& rRow )
1785 {
1786 if ( bMore )
1787 {
1788 rCol = nCol;
1789 rRow = nRow;
1790
1791 ScColumn* pCol = &pDoc->pTab[nTab]->aCol[nCol];
1792 SCSIZE nIndex = pNextIndices[nCol-nStartCol];
1793 DBG_ASSERT( nIndex < pCol->nCount, "ScHorizontalCellIterator::GetNext: nIndex out of range" );
1794 ScBaseCell* pCell = pCol->pItems[nIndex].pCell;
1795 if ( ++nIndex < pCol->nCount )
1796 {
1797 pNextRows[nCol-nStartCol] = pCol->pItems[nIndex].nRow;
1798 pNextIndices[nCol-nStartCol] = nIndex;
1799 }
1800 else
1801 {
1802 pNextRows[nCol-nStartCol] = MAXROWCOUNT; // nothing found
1803 pNextIndices[nCol-nStartCol] = MAXROWCOUNT;
1804 }
1805
1806 Advance();
1807 return pCell;
1808 }
1809 else
1810 return NULL;
1811 }
1812
ReturnNext(SCCOL & rCol,SCROW & rRow)1813 sal_Bool ScHorizontalCellIterator::ReturnNext( SCCOL& rCol, SCROW& rRow )
1814 {
1815 rCol = nCol;
1816 rRow = nRow;
1817 return bMore;
1818 }
1819
Advance()1820 void ScHorizontalCellIterator::Advance()
1821 {
1822 sal_Bool bFound = sal_False;
1823 SCCOL i;
1824
1825 for (i=nCol+1; i<=nEndCol && !bFound; i++)
1826 if (pNextRows[i-nStartCol] == nRow)
1827 {
1828 nCol = i;
1829 bFound = sal_True;
1830 }
1831
1832 if (!bFound)
1833 {
1834 SCROW nMinRow = MAXROW+1;
1835 for (i=nStartCol; i<=nEndCol; i++)
1836 if (pNextRows[i-nStartCol] < nMinRow)
1837 {
1838 nCol = i;
1839 nMinRow = pNextRows[i-nStartCol];
1840 }
1841
1842 if (nMinRow <= nEndRow)
1843 {
1844 nRow = nMinRow;
1845 bFound = sal_True;
1846 }
1847 }
1848
1849 if ( !bFound )
1850 bMore = sal_False;
1851 }
1852
1853 //------------------------------------------------------------------------
1854
ScHorizontalValueIterator(ScDocument * pDocument,const ScRange & rRange,bool bSTotal,bool bTextZero)1855 ScHorizontalValueIterator::ScHorizontalValueIterator( ScDocument* pDocument,
1856 const ScRange& rRange, bool bSTotal, bool bTextZero ) :
1857 pDoc( pDocument ),
1858 nNumFmtIndex(0),
1859 nEndTab( rRange.aEnd.Tab() ),
1860 nNumFmtType( NUMBERFORMAT_UNDEFINED ),
1861 bNumValid( false ),
1862 bSubTotal( bSTotal ),
1863 bCalcAsShown( pDocument->GetDocOptions().IsCalcAsShown() ),
1864 bTextAsZero( bTextZero )
1865 {
1866 SCCOL nStartCol = rRange.aStart.Col();
1867 SCROW nStartRow = rRange.aStart.Row();
1868 SCTAB nStartTab = rRange.aStart.Tab();
1869 SCCOL nEndCol = rRange.aEnd.Col();
1870 SCROW nEndRow = rRange.aEnd.Row();
1871 PutInOrder( nStartCol, nEndCol);
1872 PutInOrder( nStartRow, nEndRow);
1873 PutInOrder( nStartTab, nEndTab );
1874
1875 if (!ValidCol(nStartCol)) nStartCol = MAXCOL;
1876 if (!ValidCol(nEndCol)) nEndCol = MAXCOL;
1877 if (!ValidRow(nStartRow)) nStartRow = MAXROW;
1878 if (!ValidRow(nEndRow)) nEndRow = MAXROW;
1879 if (!ValidTab(nStartTab)) nStartTab = MAXTAB;
1880 if (!ValidTab(nEndTab)) nEndTab = MAXTAB;
1881
1882 nCurCol = nStartCol;
1883 nCurRow = nStartRow;
1884 nCurTab = nStartTab;
1885
1886 nNumFormat = 0; // will be initialized in GetNumberFormat()
1887 pAttrArray = 0;
1888 nAttrEndRow = 0;
1889
1890 pCellIter = new ScHorizontalCellIterator( pDoc, nStartTab, nStartCol,
1891 nStartRow, nEndCol, nEndRow );
1892 }
1893
~ScHorizontalValueIterator()1894 ScHorizontalValueIterator::~ScHorizontalValueIterator()
1895 {
1896 delete pCellIter;
1897 }
1898
GetNext(double & rValue,sal_uInt16 & rErr)1899 bool ScHorizontalValueIterator::GetNext( double& rValue, sal_uInt16& rErr )
1900 {
1901 bool bFound = false;
1902 while ( !bFound )
1903 {
1904 ScBaseCell* pCell = pCellIter->GetNext( nCurCol, nCurRow );
1905 while ( !pCell )
1906 {
1907 if ( nCurTab < nEndTab )
1908 {
1909 pCellIter->SetTab( ++nCurTab);
1910 pCell = pCellIter->GetNext( nCurCol, nCurRow );
1911 }
1912 else
1913 return false;
1914 }
1915 if ( !bSubTotal || !pDoc->pTab[nCurTab]->RowFiltered( nCurRow ) )
1916 {
1917 switch (pCell->GetCellType())
1918 {
1919 case CELLTYPE_VALUE:
1920 {
1921 bNumValid = false;
1922 rValue = ((ScValueCell*)pCell)->GetValue();
1923 rErr = 0;
1924 if ( bCalcAsShown )
1925 {
1926 ScColumn* pCol = &pDoc->pTab[nCurTab]->aCol[nCurCol];
1927 lcl_IterGetNumberFormat( nNumFormat, pAttrArray,
1928 nAttrEndRow, pCol->pAttrArray, nCurRow, pDoc );
1929 rValue = pDoc->RoundValueAsShown( rValue, nNumFormat );
1930 }
1931 bFound = true;
1932 }
1933 break;
1934 case CELLTYPE_FORMULA:
1935 {
1936 if (!bSubTotal || !((ScFormulaCell*)pCell)->IsSubTotal())
1937 {
1938 rErr = ((ScFormulaCell*)pCell)->GetErrCode();
1939 if ( rErr || ((ScFormulaCell*)pCell)->IsValue() )
1940 {
1941 rValue = ((ScFormulaCell*)pCell)->GetValue();
1942 bNumValid = false;
1943 bFound = true;
1944 }
1945 else if ( bTextAsZero )
1946 {
1947 rValue = 0.0;
1948 bNumValid = false;
1949 bFound = true;
1950 }
1951 }
1952 }
1953 break;
1954 case CELLTYPE_STRING :
1955 case CELLTYPE_EDIT :
1956 {
1957 if ( bTextAsZero )
1958 {
1959 rErr = 0;
1960 rValue = 0.0;
1961 nNumFmtType = NUMBERFORMAT_NUMBER;
1962 nNumFmtIndex = 0;
1963 bNumValid = true;
1964 bFound = true;
1965 }
1966 }
1967 break;
1968 default:
1969 ; // nothing
1970 }
1971 }
1972 }
1973 return bFound;
1974 }
1975
GetCurNumFmtInfo(short & nType,sal_uLong & nIndex)1976 void ScHorizontalValueIterator::GetCurNumFmtInfo( short& nType, sal_uLong& nIndex )
1977 {
1978 if (!bNumValid)
1979 {
1980 const ScColumn* pCol = &(pDoc->pTab[nCurTab])->aCol[nCurCol];
1981 nNumFmtIndex = pCol->GetNumberFormat( nCurRow );
1982 if ( (nNumFmtIndex % SV_COUNTRY_LANGUAGE_OFFSET) == 0 )
1983 {
1984 const ScBaseCell* pCell;
1985 SCSIZE nCurIndex;
1986 if ( pCol->Search( nCurRow, nCurIndex ) )
1987 pCell = pCol->pItems[nCurIndex].pCell;
1988 else
1989 pCell = NULL;
1990 if ( pCell && pCell->GetCellType() == CELLTYPE_FORMULA )
1991 ((const ScFormulaCell*)pCell)->GetFormatInfo( nNumFmtType, nNumFmtIndex );
1992 else
1993 nNumFmtType = pDoc->GetFormatTable()->GetType( nNumFmtIndex );
1994 }
1995 else
1996 nNumFmtType = pDoc->GetFormatTable()->GetType( nNumFmtIndex );
1997 bNumValid = true;
1998 }
1999 nType = nNumFmtType;
2000 nIndex = nNumFmtIndex;
2001 }
2002
2003 //-------------------------------------------------------------------------------
2004
ScHorizontalAttrIterator(ScDocument * pDocument,SCTAB nTable,SCCOL nCol1,SCROW nRow1,SCCOL nCol2,SCROW nRow2)2005 ScHorizontalAttrIterator::ScHorizontalAttrIterator( ScDocument* pDocument, SCTAB nTable,
2006 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2 ) :
2007 pDoc( pDocument ),
2008 nTab( nTable ),
2009 nStartCol( nCol1 ),
2010 nStartRow( nRow1 ),
2011 nEndCol( nCol2 ),
2012 nEndRow( nRow2 )
2013 {
2014 DBG_ASSERT( pDoc->pTab[nTab], "Table missing" );
2015
2016 SCCOL i;
2017
2018 nRow = nStartRow;
2019 nCol = nStartCol;
2020 bRowEmpty = sal_False;
2021
2022 pIndices = new SCSIZE[nEndCol-nStartCol+1];
2023 pNextEnd = new SCROW[nEndCol-nStartCol+1];
2024 ppPatterns = new const ScPatternAttr*[nEndCol-nStartCol+1];
2025
2026 SCROW nSkipTo = MAXROW;
2027 sal_Bool bEmpty = sal_True;
2028 for (i=nStartCol; i<=nEndCol; i++)
2029 {
2030 SCCOL nPos = i - nStartCol;
2031 ScAttrArray* pArray = pDoc->pTab[nTab]->aCol[i].pAttrArray;
2032 DBG_ASSERT( pArray, "pArray == 0" );
2033
2034 SCSIZE nIndex;
2035 pArray->Search( nStartRow, nIndex );
2036
2037 const ScPatternAttr* pPattern = pArray->pData[nIndex].pPattern;
2038 SCROW nThisEnd = pArray->pData[nIndex].nRow;
2039 if ( IsDefaultItem( pPattern ) )
2040 {
2041 pPattern = NULL;
2042 if ( nThisEnd < nSkipTo )
2043 nSkipTo = nThisEnd; // nSkipTo can setted equal in this location
2044 }
2045 else
2046 bEmpty = sal_False; // Attribute found
2047
2048 pIndices[nPos] = nIndex;
2049 pNextEnd[nPos] = nThisEnd;
2050 ppPatterns[nPos] = pPattern;
2051 }
2052
2053 if (bEmpty)
2054 nRow = nSkipTo; // jump to next area end
2055 bRowEmpty = bEmpty;
2056 }
2057
~ScHorizontalAttrIterator()2058 ScHorizontalAttrIterator::~ScHorizontalAttrIterator()
2059 {
2060 delete[] (ScPatternAttr**)ppPatterns;
2061 delete[] pNextEnd;
2062 delete[] pIndices;
2063 }
2064
GetNext(SCCOL & rCol1,SCCOL & rCol2,SCROW & rRow)2065 const ScPatternAttr* ScHorizontalAttrIterator::GetNext( SCCOL& rCol1, SCCOL& rCol2, SCROW& rRow )
2066 {
2067 for (;;)
2068 {
2069 if (!bRowEmpty)
2070 {
2071 // search in this line
2072
2073 while ( nCol <= nEndCol && !ppPatterns[nCol-nStartCol] )
2074 ++nCol;
2075
2076 if ( nCol <= nEndCol )
2077 {
2078 const ScPatternAttr* pPat = ppPatterns[nCol-nStartCol];
2079 rRow = nRow;
2080 rCol1 = nCol;
2081 while ( nCol < nEndCol && ppPatterns[nCol+1-nStartCol] == pPat )
2082 ++nCol;
2083 rCol2 = nCol;
2084 ++nCol; // count up for next call
2085 return pPat; // found
2086 }
2087 }
2088
2089 // next row
2090
2091 ++nRow;
2092 if ( nRow > nEndRow ) // already done?
2093 return NULL; // nothing found
2094
2095 sal_Bool bEmpty = sal_True;
2096 SCCOL i;
2097
2098 for ( i = nStartCol; i <= nEndCol; i++)
2099 {
2100 SCCOL nPos = i-nStartCol;
2101 if ( pNextEnd[nPos] < nRow )
2102 {
2103 ScAttrArray* pArray = pDoc->pTab[nTab]->aCol[i].pAttrArray;
2104
2105 SCSIZE nIndex = ++pIndices[nPos];
2106 if ( nIndex < pArray->nCount )
2107 {
2108 const ScPatternAttr* pPattern = pArray->pData[nIndex].pPattern;
2109 SCROW nThisEnd = pArray->pData[nIndex].nRow;
2110 if ( IsDefaultItem( pPattern ) )
2111 pPattern = NULL;
2112 else
2113 bEmpty = sal_False; // found Attribute
2114
2115 pNextEnd[nPos] = nThisEnd;
2116 ppPatterns[nPos] = pPattern;
2117
2118 DBG_ASSERT( pNextEnd[nPos] >= nRow, "not sorted" );
2119 }
2120 else
2121 {
2122 DBG_ERROR("AttrArray does not reacht to MAXROW");
2123 pNextEnd[nPos] = MAXROW;
2124 ppPatterns[nPos] = NULL;
2125 }
2126 }
2127 else if ( ppPatterns[nPos] )
2128 bEmpty = sal_False; // Area not finished
2129 }
2130
2131 if (bEmpty)
2132 {
2133 SCCOL nCount = nEndCol-nStartCol+1;
2134 SCROW nSkipTo = pNextEnd[0]; // search next sector
2135 for (i=1; i<nCount; i++)
2136 if ( pNextEnd[i] < nSkipTo )
2137 nSkipTo = pNextEnd[i];
2138 nRow = nSkipTo; // skip empty lines
2139 }
2140 bRowEmpty = bEmpty;
2141 nCol = nStartCol; // start again left
2142 }
2143
2144 // return NULL;
2145 }
2146
2147 //-------------------------------------------------------------------------------
2148
IsGreater(SCCOL nCol1,SCROW nRow1,SCCOL nCol2,SCROW nRow2)2149 inline sal_Bool IsGreater( SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2 )
2150 {
2151 return ( nRow1 > nRow2 ) || ( nRow1 == nRow2 && nCol1 > nCol2 );
2152 }
2153
ScUsedAreaIterator(ScDocument * pDocument,SCTAB nTable,SCCOL nCol1,SCROW nRow1,SCCOL nCol2,SCROW nRow2)2154 ScUsedAreaIterator::ScUsedAreaIterator( ScDocument* pDocument, SCTAB nTable,
2155 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2 ) :
2156 aCellIter( pDocument, nTable, nCol1, nRow1, nCol2, nRow2 ),
2157 aAttrIter( pDocument, nTable, nCol1, nRow1, nCol2, nRow2 ),
2158 nNextCol( nCol1 ),
2159 nNextRow( nRow1 )
2160 {
2161 pCell = aCellIter.GetNext( nCellCol, nCellRow );
2162 pPattern = aAttrIter.GetNext( nAttrCol1, nAttrCol2, nAttrRow );
2163 }
2164
~ScUsedAreaIterator()2165 ScUsedAreaIterator::~ScUsedAreaIterator()
2166 {
2167 }
2168
GetNext()2169 sal_Bool ScUsedAreaIterator::GetNext()
2170 {
2171 // count Iterators
2172
2173 if ( pCell && IsGreater( nNextCol, nNextRow, nCellCol, nCellRow ) )
2174 pCell = aCellIter.GetNext( nCellCol, nCellRow );
2175
2176 while ( pCell && pCell->IsBlank() )
2177 pCell = aCellIter.GetNext( nCellCol, nCellRow );
2178
2179 if ( pPattern && IsGreater( nNextCol, nNextRow, nAttrCol2, nAttrRow ) )
2180 pPattern = aAttrIter.GetNext( nAttrCol1, nAttrCol2, nAttrRow );
2181
2182 if ( pPattern && nAttrRow == nNextRow && nAttrCol1 < nNextCol )
2183 nAttrCol1 = nNextCol;
2184
2185 // get next sector
2186
2187 sal_Bool bFound = sal_True;
2188 sal_Bool bUseCell = sal_False;
2189
2190 if ( pCell && pPattern )
2191 {
2192 if ( IsGreater( nCellCol, nCellRow, nAttrCol1, nAttrRow ) ) // vorne nur Attribute ?
2193 {
2194 pFoundCell = NULL;
2195 pFoundPattern = pPattern;
2196 nFoundRow = nAttrRow;
2197 nFoundStartCol = nAttrCol1;
2198 if ( nCellRow == nAttrRow && nCellCol <= nAttrCol2 ) // is cell in area ?
2199 nFoundEndCol = nCellCol - 1; // only until cell
2200 else
2201 nFoundEndCol = nAttrCol2; // all
2202 }
2203 else
2204 {
2205 bUseCell = sal_True;
2206 if ( nAttrRow == nCellRow && nAttrCol1 == nCellCol ) // Attribute set on cell ?
2207 pFoundPattern = pPattern;
2208 else
2209 pFoundPattern = NULL;
2210 }
2211 }
2212 else if ( pCell ) // only cell -> direct select
2213 {
2214 pFoundPattern = NULL;
2215 bUseCell = sal_True; // Position of cell
2216 }
2217 else if ( pPattern ) // only Attribute -> direct select
2218 {
2219 pFoundCell = NULL;
2220 pFoundPattern = pPattern;
2221 nFoundRow = nAttrRow;
2222 nFoundStartCol = nAttrCol1;
2223 nFoundEndCol = nAttrCol2;
2224 }
2225 else // nothing
2226 bFound = sal_False;
2227
2228 if ( bUseCell ) // Position of cell
2229 {
2230 pFoundCell = pCell;
2231 nFoundRow = nCellRow;
2232 nFoundStartCol = nFoundEndCol = nCellCol;
2233 }
2234
2235 if (bFound)
2236 {
2237 nNextRow = nFoundRow;
2238 nNextCol = nFoundEndCol + 1;
2239 }
2240
2241 return bFound;
2242 }
2243
2244 //-------------------------------------------------------------------------------
2245
ScDocAttrIterator(ScDocument * pDocument,SCTAB nTable,SCCOL nCol1,SCROW nRow1,SCCOL nCol2,SCROW nRow2)2246 ScDocAttrIterator::ScDocAttrIterator(ScDocument* pDocument, SCTAB nTable,
2247 SCCOL nCol1, SCROW nRow1,
2248 SCCOL nCol2, SCROW nRow2) :
2249 pDoc( pDocument ),
2250 nTab( nTable ),
2251 nEndCol( nCol2 ),
2252 nStartRow( nRow1 ),
2253 nEndRow( nRow2 ),
2254 nCol( nCol1 )
2255 {
2256 if ( ValidTab(nTab) && pDoc->pTab[nTab] )
2257 pColIter = pDoc->pTab[nTab]->aCol[nCol].CreateAttrIterator( nStartRow, nEndRow );
2258 else
2259 pColIter = NULL;
2260 }
2261
~ScDocAttrIterator()2262 ScDocAttrIterator::~ScDocAttrIterator()
2263 {
2264 delete pColIter;
2265 }
2266
GetNext(SCCOL & rCol,SCROW & rRow1,SCROW & rRow2)2267 const ScPatternAttr* ScDocAttrIterator::GetNext( SCCOL& rCol, SCROW& rRow1, SCROW& rRow2 )
2268 {
2269 while ( pColIter )
2270 {
2271 const ScPatternAttr* pPattern = pColIter->Next( rRow1, rRow2 );
2272 if ( pPattern )
2273 {
2274 rCol = nCol;
2275 return pPattern;
2276 }
2277
2278 delete pColIter;
2279 ++nCol;
2280 if ( nCol <= nEndCol )
2281 pColIter = pDoc->pTab[nTab]->aCol[nCol].CreateAttrIterator( nStartRow, nEndRow );
2282 else
2283 pColIter = NULL;
2284 }
2285 return NULL; // is nothing anymore
2286 }
2287
2288 //-------------------------------------------------------------------------------
2289
ScAttrRectIterator(ScDocument * pDocument,SCTAB nTable,SCCOL nCol1,SCROW nRow1,SCCOL nCol2,SCROW nRow2)2290 ScAttrRectIterator::ScAttrRectIterator(ScDocument* pDocument, SCTAB nTable,
2291 SCCOL nCol1, SCROW nRow1,
2292 SCCOL nCol2, SCROW nRow2) :
2293 pDoc( pDocument ),
2294 nTab( nTable ),
2295 nEndCol( nCol2 ),
2296 nStartRow( nRow1 ),
2297 nEndRow( nRow2 ),
2298 nIterStartCol( nCol1 ),
2299 nIterEndCol( nCol1 )
2300 {
2301 if ( ValidTab(nTab) && pDoc->pTab[nTab] )
2302 {
2303 pColIter = pDoc->pTab[nTab]->aCol[nIterStartCol].CreateAttrIterator( nStartRow, nEndRow );
2304 while ( nIterEndCol < nEndCol &&
2305 pDoc->pTab[nTab]->aCol[nIterEndCol].IsAllAttrEqual(
2306 pDoc->pTab[nTab]->aCol[nIterEndCol+1], nStartRow, nEndRow ) )
2307 ++nIterEndCol;
2308 }
2309 else
2310 pColIter = NULL;
2311 }
2312
~ScAttrRectIterator()2313 ScAttrRectIterator::~ScAttrRectIterator()
2314 {
2315 delete pColIter;
2316 }
2317
DataChanged()2318 void ScAttrRectIterator::DataChanged()
2319 {
2320 if (pColIter)
2321 {
2322 SCROW nNextRow = pColIter->GetNextRow();
2323 delete pColIter;
2324 pColIter = pDoc->pTab[nTab]->aCol[nIterStartCol].CreateAttrIterator( nNextRow, nEndRow );
2325 }
2326 }
2327
GetNext(SCCOL & rCol1,SCCOL & rCol2,SCROW & rRow1,SCROW & rRow2)2328 const ScPatternAttr* ScAttrRectIterator::GetNext( SCCOL& rCol1, SCCOL& rCol2,
2329 SCROW& rRow1, SCROW& rRow2 )
2330 {
2331 while ( pColIter )
2332 {
2333 const ScPatternAttr* pPattern = pColIter->Next( rRow1, rRow2 );
2334 if ( pPattern )
2335 {
2336 rCol1 = nIterStartCol;
2337 rCol2 = nIterEndCol;
2338 return pPattern;
2339 }
2340
2341 delete pColIter;
2342 nIterStartCol = nIterEndCol+1;
2343 if ( nIterStartCol <= nEndCol )
2344 {
2345 nIterEndCol = nIterStartCol;
2346 pColIter = pDoc->pTab[nTab]->aCol[nIterStartCol].CreateAttrIterator( nStartRow, nEndRow );
2347 while ( nIterEndCol < nEndCol &&
2348 pDoc->pTab[nTab]->aCol[nIterEndCol].IsAllAttrEqual(
2349 pDoc->pTab[nTab]->aCol[nIterEndCol+1], nStartRow, nEndRow ) )
2350 ++nIterEndCol;
2351 }
2352 else
2353 pColIter = NULL;
2354 }
2355 return NULL; // is nothing anymore
2356 }
2357
2358 // ============================================================================
2359
2360 SCROW ScRowBreakIterator::NOT_FOUND = -1;
2361
ScRowBreakIterator(set<SCROW> & rBreaks)2362 ScRowBreakIterator::ScRowBreakIterator(set<SCROW>& rBreaks) :
2363 mrBreaks(rBreaks),
2364 maItr(rBreaks.begin()), maEnd(rBreaks.end())
2365 {
2366 }
2367
first()2368 SCROW ScRowBreakIterator::first()
2369 {
2370 maItr = mrBreaks.begin();
2371 return maItr == maEnd ? NOT_FOUND : *maItr;
2372 }
2373
next()2374 SCROW ScRowBreakIterator::next()
2375 {
2376 ++maItr;
2377 return maItr == maEnd ? NOT_FOUND : *maItr;
2378 }
2379