1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "scitems.hxx"
22 #include <sfx2/bindings.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/msgbox.hxx>
25 #include <vcl/waitobj.hxx>
26 #include <svl/zforlist.hxx>
27 #include <sfx2/app.hxx>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/container/XNameAccess.hpp>
30 #include <com/sun/star/sheet/DataPilotFieldFilter.hpp>
31 #include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
32 #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
33 #include <com/sun/star/sheet/DataPilotFieldSortMode.hpp>
34 #include <com/sun/star/sheet/DataPilotTableHeaderData.hpp>
35 #include <com/sun/star/sheet/GeneralFunction.hpp>
36 #include <com/sun/star/sheet/MemberResultFlags.hpp>
37 #include <com/sun/star/sheet/XDimensionsSupplier.hpp>
38 #include <com/sun/star/sheet/XDrillDownDataSupplier.hpp>
41 #include "globstr.hrc"
43 #include "undotab.hxx"
44 #include "undodat.hxx"
46 #include "rangenam.hxx"
47 #include "rangeutl.hxx"
49 #include "olinetab.hxx"
50 #include "consoli.hxx"
51 #include "olinefun.hxx"
52 #include "dpobject.hxx"
54 #include "dpdimsave.hxx"
55 #include "dbdocfun.hxx"
56 #include "dpoutput.hxx"
57 #include "dptabsrc.hxx"
58 #include "dpshttab.hxx"
59 #include "dpsdbtab.hxx"
60 #include "editable.hxx"
61 #include "docpool.hxx"
62 #include "patattr.hxx"
63 #include "unonames.hxx"
64 #include "formulacell.hxx"
65 #include "userlist.hxx"
66 #include "queryentry.hxx"
67 #include "markdata.hxx"
68 #include "stringutil.hxx"
70 #include <boost/scoped_ptr.hpp>
71 #include <boost/unordered_set.hpp>
72 #include <boost/unordered_map.hpp>
76 using namespace com::sun::star
;
77 using ::com::sun::star::uno::Any
;
78 using ::com::sun::star::uno::Sequence
;
79 using ::com::sun::star::uno::Reference
;
80 using ::com::sun::star::uno::UNO_QUERY
;
81 using ::com::sun::star::beans::XPropertySet
;
82 using ::com::sun::star::container::XNameAccess
;
83 using ::com::sun::star::sheet::XDimensionsSupplier
;
87 // STATIC DATA -----------------------------------------------------------
94 // Outline-Gruppierung erzeugen
96 void ScDBFunc::MakeOutline( bool bColumns
, bool bRecord
)
99 if (GetViewData()->GetSimpleArea(aRange
) == SC_MARK_SIMPLE
)
101 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
102 ScOutlineDocFunc
aFunc(*pDocSh
);
103 aFunc
.MakeOutline( aRange
, bColumns
, bRecord
, false );
106 ErrorMessage(STR_NOMULTISELECT
);
109 // Outline-Gruppierung loeschen
111 void ScDBFunc::RemoveOutline( bool bColumns
, bool bRecord
)
114 if (GetViewData()->GetSimpleArea(aRange
) == SC_MARK_SIMPLE
)
116 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
117 ScOutlineDocFunc
aFunc(*pDocSh
);
118 aFunc
.RemoveOutline( aRange
, bColumns
, bRecord
, false );
121 ErrorMessage(STR_NOMULTISELECT
);
124 // Menue-Status: Outlines loeschen
126 void ScDBFunc::TestRemoveOutline( bool& rCol
, bool& rRow
)
128 bool bColFound
= false;
129 bool bRowFound
= false;
131 SCCOL nStartCol
, nEndCol
;
132 SCROW nStartRow
, nEndRow
;
133 SCTAB nStartTab
, nEndTab
;
134 if (GetViewData()->GetSimpleArea(nStartCol
,nStartRow
,nStartTab
,nEndCol
,nEndRow
,nEndTab
) == SC_MARK_SIMPLE
)
136 SCTAB nTab
= nStartTab
;
137 ScDocument
* pDoc
= GetViewData()->GetDocument();
138 ScOutlineTable
* pTable
= pDoc
->GetOutlineTable( nTab
);
141 ScOutlineArray
* pArray
;
142 ScOutlineEntry
* pEntry
;
145 bool bColMarked
= ( nStartRow
== 0 && nEndRow
== MAXROW
);
146 bool bRowMarked
= ( nStartCol
== 0 && nEndCol
== MAXCOL
);
150 if ( !bRowMarked
|| bColMarked
) // nicht wenn ganze Zeilen markiert
152 pArray
= pTable
->GetColArray();
153 ScSubOutlineIterator
aColIter( pArray
);
154 while ((pEntry
=aColIter
.GetNext()) != NULL
&& !bColFound
)
156 nStart
= pEntry
->GetStart();
157 nEnd
= pEntry
->GetEnd();
158 if ( nStartCol
<=static_cast<SCCOL
>(nEnd
) && nEndCol
>=static_cast<SCCOL
>(nStart
) )
165 if ( !bColMarked
|| bRowMarked
) // nicht wenn ganze Spalten markiert
167 pArray
= pTable
->GetRowArray();
168 ScSubOutlineIterator
aRowIter( pArray
);
169 while ((pEntry
=aRowIter
.GetNext()) != NULL
&& !bRowFound
)
171 nStart
= pEntry
->GetStart();
172 nEnd
= pEntry
->GetEnd();
173 if ( nStartRow
<=nEnd
&& nEndRow
>=nStart
)
184 void ScDBFunc::RemoveAllOutlines( bool bRecord
)
186 SCTAB nTab
= GetViewData()->GetTabNo();
187 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
188 ScOutlineDocFunc
aFunc(*pDocSh
);
190 bool bOk
= aFunc
.RemoveAllOutlines( nTab
, bRecord
);
198 void ScDBFunc::AutoOutline( bool bRecord
)
200 SCTAB nTab
= GetViewData()->GetTabNo();
201 ScRange
aRange( 0,0,nTab
, MAXCOL
,MAXROW
,nTab
); // ganze Tabelle, wenn nichts markiert
202 ScMarkData
& rMark
= GetViewData()->GetMarkData();
203 if ( rMark
.IsMarked() || rMark
.IsMultiMarked() )
206 rMark
.GetMultiMarkArea( aRange
);
209 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
210 ScOutlineDocFunc
aFunc(*pDocSh
);
211 aFunc
.AutoOutline( aRange
, bRecord
);
214 // Outline-Ebene auswaehlen
216 void ScDBFunc::SelectLevel( bool bColumns
, sal_uInt16 nLevel
, bool bRecord
, bool bPaint
)
218 SCTAB nTab
= GetViewData()->GetTabNo();
219 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
220 ScOutlineDocFunc
aFunc(*pDocSh
);
222 bool bOk
= aFunc
.SelectLevel( nTab
, bColumns
, nLevel
, bRecord
, bPaint
);
228 // einzelne Outline-Gruppe einblenden
230 void ScDBFunc::ShowOutline( bool bColumns
, sal_uInt16 nLevel
, sal_uInt16 nEntry
, bool bRecord
, bool bPaint
)
232 SCTAB nTab
= GetViewData()->GetTabNo();
233 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
234 ScOutlineDocFunc
aFunc(*pDocSh
);
236 bool bOk
= aFunc
.ShowOutline( nTab
, bColumns
, nLevel
, nEntry
, bRecord
, bPaint
);
242 // einzelne Outline-Gruppe ausblenden
244 void ScDBFunc::HideOutline( bool bColumns
, sal_uInt16 nLevel
, sal_uInt16 nEntry
, bool bRecord
, bool bPaint
)
246 SCTAB nTab
= GetViewData()->GetTabNo();
247 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
248 ScOutlineDocFunc
aFunc(*pDocSh
);
250 bool bOk
= aFunc
.HideOutline( nTab
, bColumns
, nLevel
, nEntry
, bRecord
, bPaint
);
256 // Menue-Status: markierten Bereich ein-/ausblenden
258 bool ScDBFunc::OutlinePossible(bool bHide
)
260 bool bEnable
= false;
269 if (GetViewData()->GetSimpleArea(nStartCol
,nStartRow
,nStartTab
,nEndCol
,nEndRow
,nEndTab
) == SC_MARK_SIMPLE
)
271 ScDocument
* pDoc
= GetViewData()->GetDocument();
272 SCTAB nTab
= GetViewData()->GetTabNo();
273 ScOutlineTable
* pTable
= pDoc
->GetOutlineTable( nTab
);
276 ScOutlineArray
* pArray
;
277 ScOutlineEntry
* pEntry
;
283 pArray
= pTable
->GetColArray();
284 ScSubOutlineIterator
aColIter( pArray
);
285 while ((pEntry
=aColIter
.GetNext()) != NULL
&& !bEnable
)
287 nStart
= pEntry
->GetStart();
288 nEnd
= pEntry
->GetEnd();
291 if ( nStartCol
<=static_cast<SCCOL
>(nEnd
) && nEndCol
>=static_cast<SCCOL
>(nStart
) )
292 if (!pEntry
->IsHidden())
297 if ( nStart
>=nStartCol
&& nEnd
<=nEndCol
)
298 if (pEntry
->IsHidden())
305 pArray
= pTable
->GetRowArray();
306 ScSubOutlineIterator
aRowIter( pArray
);
307 while ((pEntry
=aRowIter
.GetNext()) != NULL
)
309 nStart
= pEntry
->GetStart();
310 nEnd
= pEntry
->GetEnd();
313 if ( nStartRow
<=nEnd
&& nEndRow
>=nStart
)
314 if (!pEntry
->IsHidden())
319 if ( nStart
>=nStartRow
&& nEnd
<=nEndRow
)
320 if (pEntry
->IsHidden())
330 // markierten Bereich einblenden
332 void ScDBFunc::ShowMarkedOutlines( bool bRecord
)
335 if (GetViewData()->GetSimpleArea(aRange
) == SC_MARK_SIMPLE
)
337 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
338 ScOutlineDocFunc
aFunc(*pDocSh
);
339 bool bDone
= aFunc
.ShowMarkedOutlines( aRange
, bRecord
);
344 ErrorMessage(STR_NOMULTISELECT
);
347 // markierten Bereich ausblenden
349 void ScDBFunc::HideMarkedOutlines( bool bRecord
)
352 if (GetViewData()->GetSimpleArea(aRange
) == SC_MARK_SIMPLE
)
354 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
355 ScOutlineDocFunc
aFunc(*pDocSh
);
356 bool bDone
= aFunc
.HideMarkedOutlines( aRange
, bRecord
);
361 ErrorMessage(STR_NOMULTISELECT
);
368 void ScDBFunc::DoSubTotals( const ScSubTotalParam
& rParam
, bool bRecord
,
369 const ScSortParam
* pForceNewSort
)
371 bool bDo
= !rParam
.bRemoveOnly
; // sal_False = nur loeschen
373 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
374 ScDocument
* pDoc
= pDocSh
->GetDocument();
375 ScMarkData
& rMark
= GetViewData()->GetMarkData();
376 SCTAB nTab
= GetViewData()->GetTabNo();
377 if (bRecord
&& !pDoc
->IsUndoEnabled())
380 ScDBData
* pDBData
= pDoc
->GetDBAtArea( nTab
, rParam
.nCol1
, rParam
.nRow1
,
381 rParam
.nCol2
, rParam
.nRow2
);
384 OSL_FAIL( "SubTotals: keine DBData" );
388 ScEditableTester
aTester( pDoc
, nTab
, 0,rParam
.nRow1
+1, MAXCOL
,MAXROW
);
389 if (!aTester
.IsEditable())
391 ErrorMessage(aTester
.GetMessageId());
395 if (pDoc
->HasAttrib( rParam
.nCol1
, rParam
.nRow1
+1, nTab
,
396 rParam
.nCol2
, rParam
.nRow2
, nTab
, HASATTR_MERGED
| HASATTR_OVERLAPPED
))
398 ErrorMessage(STR_MSSG_INSERTCELLS_0
); // nicht in zusammengefasste einfuegen
402 WaitObject
aWait( GetViewData()->GetDialogParent() );
405 if (pDoc
->TestRemoveSubTotals( nTab
, rParam
))
407 bOk
= ( MessBox( GetViewData()->GetDialogParent(), WinBits(WB_YES_NO
| WB_DEF_YES
),
408 // "StarCalc" "Daten loeschen?"
409 ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_0
),
410 ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_1
) ).Execute()
416 ScDocShellModificator
aModificator( *pDocSh
);
418 ScSubTotalParam
aNewParam( rParam
); // Bereichsende wird veraendert
419 ScDocument
* pUndoDoc
= NULL
;
420 ScOutlineTable
* pUndoTab
= NULL
;
421 ScRangeName
* pUndoRange
= NULL
;
422 ScDBCollection
* pUndoDB
= NULL
;
424 if (bRecord
) // alte Daten sichern
426 bool bOldFilter
= bDo
&& rParam
.bDoSort
;
427 SCTAB nTabCount
= pDoc
->GetTableCount();
428 pUndoDoc
= new ScDocument( SCDOCMODE_UNDO
);
429 ScOutlineTable
* pTable
= pDoc
->GetOutlineTable( nTab
);
432 pUndoTab
= new ScOutlineTable( *pTable
);
434 SCCOLROW nOutStartCol
; // Zeilen/Spaltenstatus
435 SCCOLROW nOutStartRow
;
438 pTable
->GetColArray()->GetRange( nOutStartCol
, nOutEndCol
);
439 pTable
->GetRowArray()->GetRange( nOutStartRow
, nOutEndRow
);
441 pUndoDoc
->InitUndo( pDoc
, nTab
, nTab
, true, true );
442 pDoc
->CopyToDocument( static_cast<SCCOL
>(nOutStartCol
), 0, nTab
, static_cast<SCCOL
>(nOutEndCol
), MAXROW
, nTab
, IDF_NONE
, false, pUndoDoc
);
443 pDoc
->CopyToDocument( 0, nOutStartRow
, nTab
, MAXCOL
, nOutEndRow
, nTab
, IDF_NONE
, false, pUndoDoc
);
446 pUndoDoc
->InitUndo( pDoc
, nTab
, nTab
, false, bOldFilter
);
448 // Datenbereich sichern - incl. Filter-Ergebnis
449 pDoc
->CopyToDocument( 0,rParam
.nRow1
+1,nTab
, MAXCOL
,rParam
.nRow2
,nTab
,
450 IDF_ALL
, false, pUndoDoc
);
452 // alle Formeln wegen Referenzen
453 pDoc
->CopyToDocument( 0,0,0, MAXCOL
,MAXROW
,nTabCount
-1,
454 IDF_FORMULA
, false, pUndoDoc
);
456 // DB- und andere Bereiche
457 ScRangeName
* pDocRange
= pDoc
->GetRangeName();
458 if (!pDocRange
->empty())
459 pUndoRange
= new ScRangeName( *pDocRange
);
460 ScDBCollection
* pDocDB
= pDoc
->GetDBCollection();
461 if (!pDocDB
->empty())
462 pUndoDB
= new ScDBCollection( *pDocDB
);
465 ScOutlineTable
* pOut
= pDoc
->GetOutlineTable( nTab
);
468 // Remove all existing outlines in the specified range.
469 ScOutlineArray
* pRowArray
= pOut
->GetRowArray();
470 sal_uInt16 nDepth
= pRowArray
->GetDepth();
471 for (sal_uInt16 i
= 0; i
< nDepth
; ++i
)
474 pRowArray
->Remove(aNewParam
.nRow1
, aNewParam
.nRow2
, bSize
);
479 pDoc
->RemoveSubTotals( nTab
, aNewParam
);
480 bool bSuccess
= true;
484 if ( rParam
.bDoSort
|| pForceNewSort
)
486 pDBData
->SetArea( nTab
, aNewParam
.nCol1
,aNewParam
.nRow1
, aNewParam
.nCol2
,aNewParam
.nRow2
);
488 // Teilergebnis-Felder vor die Sortierung setzen
489 // (doppelte werden weggelassen, kann darum auch wieder aufgerufen werden)
491 ScSortParam aOldSort
;
492 pDBData
->GetSortParam( aOldSort
);
493 ScSortParam
aSortParam( aNewParam
, pForceNewSort
? *pForceNewSort
: aOldSort
);
494 Sort( aSortParam
, false, false );
497 bSuccess
= pDoc
->DoSubTotals( nTab
, aNewParam
);
499 ScRange
aDirtyRange( aNewParam
.nCol1
, aNewParam
.nRow1
, nTab
,
500 aNewParam
.nCol2
, aNewParam
.nRow2
, nTab
);
501 pDoc
->SetDirty( aDirtyRange
);
505 pDocSh
->GetUndoManager()->AddUndoAction(
506 new ScUndoSubTotals( pDocSh
, nTab
,
507 rParam
, aNewParam
.nRow2
,
508 pUndoDoc
, pUndoTab
, // pUndoDBData,
509 pUndoRange
, pUndoDB
) );
514 // "Kann keine Zeilen einfuegen"
515 ErrorMessage(STR_MSSG_DOSUBTOTALS_2
);
519 pDBData
->SetSubTotalParam( aNewParam
);
520 pDBData
->SetArea( nTab
, aNewParam
.nCol1
,aNewParam
.nRow1
, aNewParam
.nCol2
,aNewParam
.nRow2
);
521 pDoc
->CompileDBFormula();
525 rMark
.SetMarkArea( ScRange( aNewParam
.nCol1
,aNewParam
.nRow1
,nTab
,
526 aNewParam
.nCol2
,aNewParam
.nRow2
,nTab
) );
529 pDocSh
->PostPaint(ScRange(0, 0, nTab
, MAXCOL
, MAXROW
, nTab
),
530 PAINT_GRID
| PAINT_LEFT
| PAINT_TOP
| PAINT_SIZE
);
532 aModificator
.SetDocumentModified();
542 void ScDBFunc::Consolidate( const ScConsolidateParam
& rParam
, bool bRecord
)
544 ScDocShell
* pDocShell
= GetViewData()->GetDocShell();
545 pDocShell
->DoConsolidate( rParam
, bRecord
);
546 SetTabNo( rParam
.nTab
, true );
553 static OUString
lcl_MakePivotTabName( const OUString
& rPrefix
, SCTAB nNumber
)
555 OUString aName
= rPrefix
+ OUString::number( nNumber
);
559 bool ScDBFunc::MakePivotTable(
560 const ScDPSaveData
& rData
, const ScRange
& rDest
, bool bNewTable
,
561 const ScDPObject
& rSource
, bool bApi
)
563 // error message if no fields are set
564 // this must be removed when drag&drop of fields from a toolbox is available
566 if ( rData
.IsEmpty() && !bApi
)
568 ErrorMessage(STR_PIVOT_NODATA
);
572 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
573 ScDocument
* pDoc
= GetViewData()->GetDocument();
574 bool bUndo
= pDoc
->IsUndoEnabled();
576 ScRange aDestRange
= rDest
;
579 SCTAB nSrcTab
= GetViewData()->GetTabNo();
581 OUString
aName( ScGlobal::GetRscString(STR_PIVOT_TABLE
) );
584 pDoc
->GetName( nSrcTab
, aStr
);
589 SCTAB nNewTab
= nSrcTab
+1;
592 while ( !pDoc
->InsertTab( nNewTab
, lcl_MakePivotTabName( aName
, i
) ) && i
<= MAXTAB
)
595 bool bAppend
= ( nNewTab
+1 == pDoc
->GetTableCount() );
598 pDocSh
->GetUndoManager()->AddUndoAction(
599 new ScUndoInsertTab( pDocSh
, nNewTab
, bAppend
, lcl_MakePivotTabName( aName
, i
) ));
602 GetViewData()->InsertTab( nNewTab
);
603 SetTabNo(nNewTab
, true);
605 aDestRange
= ScRange( 0, 0, nNewTab
);
608 ScDPObject
* pDPObj
= pDoc
->GetDPAtCursor(
609 aDestRange
.aStart
.Col(), aDestRange
.aStart
.Row(), aDestRange
.aStart
.Tab() );
611 ScDPObject
aObj( rSource
);
612 aObj
.SetOutRange( aDestRange
);
613 if ( pDPObj
&& !rData
.GetExistingDimensionData() )
615 // copy dimension data from old object - lost in the dialog
616 //! change the dialog to keep the dimension data
618 ScDPSaveData
aNewData( rData
);
619 const ScDPSaveData
* pOldData
= pDPObj
->GetSaveData();
622 const ScDPDimensionSaveData
* pDimSave
= pOldData
->GetExistingDimensionData();
623 aNewData
.SetDimensionData( pDimSave
);
625 aObj
.SetSaveData( aNewData
);
628 aObj
.SetSaveData( rData
);
630 bool bAllowMove
= (pDPObj
!= NULL
); // allow re-positioning when editing existing table
632 ScDBDocFunc
aFunc( *pDocSh
);
633 bool bSuccess
= aFunc
.DataPilotUpdate(pDPObj
, &aObj
, true, false, bAllowMove
);
635 CursorPosChanged(); // shells may be switched
639 pDocSh
->PostPaintExtras();
640 SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_TABLES_CHANGED
) );
646 void ScDBFunc::DeletePivotTable()
648 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
649 ScDocument
* pDoc
= pDocSh
->GetDocument();
650 ScDPObject
* pDPObj
= pDoc
->GetDPAtCursor( GetViewData()->GetCurX(),
651 GetViewData()->GetCurY(),
652 GetViewData()->GetTabNo() );
655 ScDBDocFunc
aFunc( *pDocSh
);
656 aFunc
.RemovePivotTable(*pDPObj
, true, false);
657 CursorPosChanged(); // shells may be switched
660 ErrorMessage(STR_PIVOT_NOTFOUND
);
663 void ScDBFunc::RecalcPivotTable()
665 ScDocShell
* pDocSh
= GetViewData()->GetDocShell();
666 ScDocument
* pDoc
= GetViewData()->GetDocument();
668 ScDPObject
* pDPObj
= pDoc
->GetDPAtCursor( GetViewData()->GetCurX(),
669 GetViewData()->GetCurY(),
670 GetViewData()->GetTabNo() );
673 // Remove existing data cache for the data that this datapilot uses,
674 // to force re-build data cache.
675 ScDBDocFunc
aFunc(*pDocSh
);
676 aFunc
.RefreshPivotTables(pDPObj
, false);
678 CursorPosChanged(); // shells may be switched
681 ErrorMessage(STR_PIVOT_NOTFOUND
);
684 void ScDBFunc::GetSelectedMemberList(ScDPUniqueStringSet
& rEntries
, long& rDimension
)
686 ScDPObject
* pDPObj
= GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
687 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
691 long nStartDimension
= -1;
692 long nStartHierarchy
= -1;
693 long nStartLevel
= -1;
695 ScRangeListRef xRanges
;
696 GetViewData()->GetMultiArea( xRanges
); // incl. cursor if nothing is selected
697 size_t nRangeCount
= xRanges
->size();
698 bool bContinue
= true;
700 for (size_t nRangePos
=0; nRangePos
< nRangeCount
&& bContinue
; nRangePos
++)
702 ScRange aRange
= *(*xRanges
)[nRangePos
];
703 SCCOL nStartCol
= aRange
.aStart
.Col();
704 SCROW nStartRow
= aRange
.aStart
.Row();
705 SCCOL nEndCol
= aRange
.aEnd
.Col();
706 SCROW nEndRow
= aRange
.aEnd
.Row();
707 SCTAB nTab
= aRange
.aStart
.Tab();
709 for (SCROW nRow
=nStartRow
; nRow
<=nEndRow
&& bContinue
; nRow
++)
710 for (SCCOL nCol
=nStartCol
; nCol
<=nEndCol
&& bContinue
; nCol
++)
712 sheet::DataPilotTableHeaderData aData
;
713 pDPObj
->GetHeaderPositionData(ScAddress(nCol
, nRow
, nTab
), aData
);
714 if ( aData
.Dimension
< 0 )
715 bContinue
= false; // not part of any dimension
718 if ( nStartDimension
< 0 ) // first member?
720 nStartDimension
= aData
.Dimension
;
721 nStartHierarchy
= aData
.Hierarchy
;
722 nStartLevel
= aData
.Level
;
724 if ( aData
.Dimension
!= nStartDimension
||
725 aData
.Hierarchy
!= nStartHierarchy
||
726 aData
.Level
!= nStartLevel
)
728 bContinue
= false; // cannot mix dimensions
733 // accept any part of a member description, also subtotals,
734 // but don't stop if empty parts are contained
735 if ( aData
.Flags
& sheet::MemberResultFlags::HASMEMBER
)
736 rEntries
.insert(aData
.MemberName
);
741 rDimension
= nStartDimension
; // dimension from which the found members came
743 rEntries
.clear(); // remove all if not valid
746 bool ScDBFunc::HasSelectionForDateGroup( ScDPNumGroupInfo
& rOldInfo
, sal_Int32
& rParts
)
748 // determine if the date group dialog has to be shown for the current selection
752 SCCOL nCurX
= GetViewData()->GetCurX();
753 SCROW nCurY
= GetViewData()->GetCurY();
754 SCTAB nTab
= GetViewData()->GetTabNo();
755 ScDocument
* pDoc
= GetViewData()->GetDocument();
757 ScDPObject
* pDPObj
= pDoc
->GetDPAtCursor( nCurX
, nCurY
, nTab
);
760 ScDPUniqueStringSet aEntries
;
761 long nSelectDimension
= -1;
762 GetSelectedMemberList( aEntries
, nSelectDimension
);
764 if (!aEntries
.empty())
767 OUString aDimName
= pDPObj
->GetDimName( nSelectDimension
, bIsDataLayout
);
768 OUString
aBaseDimName( aDimName
);
770 bool bInGroupDim
= false;
771 bool bFoundParts
= false;
773 ScDPDimensionSaveData
* pDimData
=
774 const_cast<ScDPDimensionSaveData
*>( pDPObj
->GetSaveData()->GetExistingDimensionData() );
777 const ScDPSaveNumGroupDimension
* pNumGroupDim
= pDimData
->GetNumGroupDim( aDimName
);
778 const ScDPSaveGroupDimension
* pGroupDim
= pDimData
->GetNamedGroupDim( aDimName
);
781 // existing num group dimension
783 if ( pNumGroupDim
->GetDatePart() != 0 )
785 // dimension has date info -> edit settings of this dimension
786 // (parts are collected below)
788 rOldInfo
= pNumGroupDim
->GetDateInfo();
791 else if ( pNumGroupDim
->GetInfo().mbDateValues
)
793 // Numerical grouping with DateValues flag is used for grouping
794 // of days with a "Number of days" value.
796 rOldInfo
= pNumGroupDim
->GetInfo();
797 rParts
= com::sun::star::sheet::DataPilotFieldGroupBy::DAYS
; // not found in CollectDateParts
803 else if ( pGroupDim
)
805 // existing additional group dimension
807 if ( pGroupDim
->GetDatePart() != 0 )
809 // dimension has date info -> edit settings of this dimension
810 // (parts are collected below)
812 rOldInfo
= pGroupDim
->GetDateInfo();
813 aBaseDimName
= pGroupDim
->GetSourceDimName();
819 if ( bFound
&& !bFoundParts
)
821 // collect date parts from all group dimensions
822 rParts
= pDimData
->CollectDateParts( aBaseDimName
);
824 if ( !bFound
&& !bInGroupDim
)
826 // create new date group dimensions if the selection is a single cell
827 // in a normal dimension with date content
830 if ( (GetViewData()->GetSimpleArea( aSelRange
) == SC_MARK_SIMPLE
) &&
831 aSelRange
.aStart
== aSelRange
.aEnd
)
833 SCCOL nSelCol
= aSelRange
.aStart
.Col();
834 SCROW nSelRow
= aSelRange
.aStart
.Row();
835 SCTAB nSelTab
= aSelRange
.aStart
.Tab();
836 if ( pDoc
->HasValueData( nSelCol
, nSelRow
, nSelTab
) )
838 sal_uLong nIndex
= static_cast<const SfxUInt32Item
*>(pDoc
->GetAttr(
839 nSelCol
, nSelRow
, nSelTab
, ATTR_VALUE_FORMAT
))->GetValue();
840 short nType
= pDoc
->GetFormatTable()->GetType(nIndex
);
841 if ( nType
== NUMBERFORMAT_DATE
|| nType
== NUMBERFORMAT_TIME
|| nType
== NUMBERFORMAT_DATETIME
)
844 // use currently selected value for automatic limits
845 if( rOldInfo
.mbAutoStart
)
846 rOldInfo
.mfStart
= pDoc
->GetValue( aSelRange
.aStart
);
847 if( rOldInfo
.mbAutoEnd
)
848 rOldInfo
.mfEnd
= pDoc
->GetValue( aSelRange
.aStart
);
859 bool ScDBFunc::HasSelectionForNumGroup( ScDPNumGroupInfo
& rOldInfo
)
861 // determine if the numeric group dialog has to be shown for the current selection
865 SCCOL nCurX
= GetViewData()->GetCurX();
866 SCROW nCurY
= GetViewData()->GetCurY();
867 SCTAB nTab
= GetViewData()->GetTabNo();
868 ScDocument
* pDoc
= GetViewData()->GetDocument();
870 ScDPObject
* pDPObj
= pDoc
->GetDPAtCursor( nCurX
, nCurY
, nTab
);
873 ScDPUniqueStringSet aEntries
;
874 long nSelectDimension
= -1;
875 GetSelectedMemberList( aEntries
, nSelectDimension
);
877 if (!aEntries
.empty())
880 OUString aDimName
= pDPObj
->GetDimName( nSelectDimension
, bIsDataLayout
);
882 bool bInGroupDim
= false;
884 ScDPDimensionSaveData
* pDimData
=
885 const_cast<ScDPDimensionSaveData
*>( pDPObj
->GetSaveData()->GetExistingDimensionData() );
888 const ScDPSaveNumGroupDimension
* pNumGroupDim
= pDimData
->GetNumGroupDim( aDimName
);
891 // existing num group dimension
892 // -> edit settings of this dimension
894 rOldInfo
= pNumGroupDim
->GetInfo();
897 else if ( pDimData
->GetNamedGroupDim( aDimName
) )
898 bInGroupDim
= true; // in a group dimension
900 if ( !bFound
&& !bInGroupDim
)
902 // create a new num group dimension if the selection is a single cell
903 // in a normal dimension with numeric content
906 if ( (GetViewData()->GetSimpleArea( aSelRange
) == SC_MARK_SIMPLE
) &&
907 aSelRange
.aStart
== aSelRange
.aEnd
)
909 if ( pDoc
->HasValueData( aSelRange
.aStart
.Col(), aSelRange
.aStart
.Row(),
910 aSelRange
.aStart
.Tab() ) )
913 // use currently selected value for automatic limits
914 if( rOldInfo
.mbAutoStart
)
915 rOldInfo
.mfStart
= pDoc
->GetValue( aSelRange
.aStart
);
916 if( rOldInfo
.mbAutoEnd
)
917 rOldInfo
.mfEnd
= pDoc
->GetValue( aSelRange
.aStart
);
927 void ScDBFunc::DateGroupDataPilot( const ScDPNumGroupInfo
& rInfo
, sal_Int32 nParts
)
929 ScDPObject
* pDPObj
= GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
930 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
934 ScDPUniqueStringSet aEntries
;
935 long nSelectDimension
= -1;
936 GetSelectedMemberList( aEntries
, nSelectDimension
);
938 if (aEntries
.empty())
941 std::vector
<OUString
> aDeletedNames
;
943 OUString aDimName
= pDPObj
->GetDimName( nSelectDimension
, bIsDataLayout
);
945 ScDPSaveData
aData( *pDPObj
->GetSaveData() );
946 ScDPDimensionSaveData
* pDimData
= aData
.GetDimensionData(); // created if not there
948 // find the source dimension name.
949 OUString aBaseDimName
= aDimName
;
950 if( const ScDPSaveGroupDimension
* pBaseGroupDim
= pDimData
->GetNamedGroupDim( aDimName
) )
951 aBaseDimName
= pBaseGroupDim
->GetSourceDimName();
953 // Remove all group dimensions associated with this source dimension. For
954 // date grouping, we need to remove all existing groups for the affected
955 // source dimension and build new one(s) from scratch. Keep the deleted
956 // names so that they can be reused during re-construction.
957 aData
.RemoveAllGroupDimensions(aBaseDimName
, &aDeletedNames
);
961 // create date group dimensions
963 ScDPNumGroupInfo aEmpty
;
966 for (sal_uInt16 nBit
=0; nBit
<32; nBit
++)
968 if ( nParts
& nMask
)
972 // innermost part: create NumGroupDimension (replacing original values)
973 // Dimension name is left unchanged
975 if ( (nParts
== sheet::DataPilotFieldGroupBy::DAYS
) && (rInfo
.mfStep
>= 1.0) )
977 // only days, and a step value specified: use numerical grouping
978 // with DateValues flag, not date grouping
980 ScDPNumGroupInfo
aNumInfo( rInfo
);
981 aNumInfo
.mbDateValues
= true;
983 ScDPSaveNumGroupDimension
aNumGroupDim( aBaseDimName
, aNumInfo
);
984 pDimData
->AddNumGroupDimension( aNumGroupDim
);
988 ScDPSaveNumGroupDimension
aNumGroupDim( aBaseDimName
, rInfo
, nMask
);
989 pDimData
->AddNumGroupDimension( aNumGroupDim
);
996 // additional parts: create GroupDimension (shown as additional dimensions)
997 OUString aGroupDimName
=
998 pDimData
->CreateDateGroupDimName(nMask
, *pDPObj
, true, &aDeletedNames
);
999 ScDPSaveGroupDimension
aGroupDim( aBaseDimName
, aGroupDimName
);
1000 aGroupDim
.SetDateInfo( rInfo
, nMask
);
1001 pDimData
->AddGroupDimension( aGroupDim
);
1004 ScDPSaveDimension
* pSaveDimension
= aData
.GetDimensionByName( aGroupDimName
);
1005 if ( pSaveDimension
->GetOrientation() == sheet::DataPilotFieldOrientation_HIDDEN
)
1007 ScDPSaveDimension
* pOldDimension
= aData
.GetDimensionByName( aBaseDimName
);
1008 pSaveDimension
->SetOrientation( pOldDimension
->GetOrientation() );
1009 long nPosition
= 0; //! before (immediate) base
1010 aData
.SetPosition( pSaveDimension
, nPosition
);
1019 ScDBDocFunc
aFunc( *GetViewData()->GetDocShell() );
1020 pDPObj
->SetSaveData( aData
);
1021 aFunc
.RefreshPivotTableGroups(pDPObj
);
1023 // unmark cell selection
1027 void ScDBFunc::NumGroupDataPilot( const ScDPNumGroupInfo
& rInfo
)
1029 ScDPObject
* pDPObj
= GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
1030 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
1034 ScDPUniqueStringSet aEntries
;
1035 long nSelectDimension
= -1;
1036 GetSelectedMemberList( aEntries
, nSelectDimension
);
1038 if (aEntries
.empty())
1042 OUString aDimName
= pDPObj
->GetDimName( nSelectDimension
, bIsDataLayout
);
1044 ScDPSaveData
aData( *pDPObj
->GetSaveData() );
1045 ScDPDimensionSaveData
* pDimData
= aData
.GetDimensionData(); // created if not there
1047 ScDPSaveNumGroupDimension
* pExisting
= pDimData
->GetNumGroupDimAcc( aDimName
);
1050 // modify existing group dimension
1051 pExisting
->SetGroupInfo( rInfo
);
1055 // create new group dimension
1056 ScDPSaveNumGroupDimension
aNumGroupDim( aDimName
, rInfo
);
1057 pDimData
->AddNumGroupDimension( aNumGroupDim
);
1061 ScDBDocFunc
aFunc( *GetViewData()->GetDocShell() );
1062 pDPObj
->SetSaveData( aData
);
1063 aFunc
.RefreshPivotTableGroups(pDPObj
);
1065 // unmark cell selection
1069 void ScDBFunc::GroupDataPilot()
1071 ScDPObject
* pDPObj
= GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
1072 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
1076 ScDPUniqueStringSet aEntries
;
1077 long nSelectDimension
= -1;
1078 GetSelectedMemberList( aEntries
, nSelectDimension
);
1080 if (aEntries
.empty())
1084 OUString aDimName
= pDPObj
->GetDimName( nSelectDimension
, bIsDataLayout
);
1086 ScDPSaveData
aData( *pDPObj
->GetSaveData() );
1087 ScDPDimensionSaveData
* pDimData
= aData
.GetDimensionData(); // created if not there
1089 // find original base
1090 OUString aBaseDimName
= aDimName
;
1091 const ScDPSaveGroupDimension
* pBaseGroupDim
= pDimData
->GetNamedGroupDim( aDimName
);
1092 if ( pBaseGroupDim
)
1094 // any entry's SourceDimName is the original base
1095 aBaseDimName
= pBaseGroupDim
->GetSourceDimName();
1098 // find existing group dimension
1099 // (using the selected dim, can be intermediate group dim)
1100 ScDPSaveGroupDimension
* pGroupDimension
= pDimData
->GetGroupDimAccForBase( aDimName
);
1102 // remove the selected items from their groups
1103 // (empty groups are removed, too)
1104 if ( pGroupDimension
)
1106 ScDPUniqueStringSet::const_iterator it
= aEntries
.begin(), itEnd
= aEntries
.end();
1107 for (; it
!= itEnd
; ++it
)
1109 const OUString
& aEntryName
= *it
;
1110 if ( pBaseGroupDim
)
1112 // for each selected (intermediate) group, remove all its items
1113 // (same logic as for adding, below)
1114 const ScDPSaveGroupItem
* pBaseGroup
= pBaseGroupDim
->GetNamedGroup( aEntryName
);
1116 pBaseGroup
->RemoveElementsFromGroups( *pGroupDimension
); // remove all elements
1118 pGroupDimension
->RemoveFromGroups( aEntryName
);
1121 pGroupDimension
->RemoveFromGroups( aEntryName
);
1125 ScDPSaveGroupDimension
* pNewGroupDim
= NULL
;
1126 if ( !pGroupDimension
)
1128 // create a new group dimension
1129 OUString aGroupDimName
=
1130 pDimData
->CreateGroupDimName(aBaseDimName
, *pDPObj
, false, NULL
);
1131 pNewGroupDim
= new ScDPSaveGroupDimension( aBaseDimName
, aGroupDimName
);
1133 pGroupDimension
= pNewGroupDim
; // make changes to the new dim if none existed
1135 if ( pBaseGroupDim
)
1137 // If it's a higher-order group dimension, pre-allocate groups for all
1138 // non-selected original groups, so the individual base members aren't
1139 // used for automatic groups (this would make the original groups hard
1141 //! Also do this when removing groups?
1142 //! Handle this case dynamically with automatic groups?
1144 long nGroupCount
= pBaseGroupDim
->GetGroupCount();
1145 for ( long nGroup
= 0; nGroup
< nGroupCount
; nGroup
++ )
1147 const ScDPSaveGroupItem
* pBaseGroup
= pBaseGroupDim
->GetGroupByIndex( nGroup
);
1149 if (!aEntries
.count(pBaseGroup
->GetGroupName()))
1151 // add an additional group for each item that is not in the selection
1152 ScDPSaveGroupItem
aGroup( pBaseGroup
->GetGroupName() );
1153 aGroup
.AddElementsFromGroup( *pBaseGroup
);
1154 pGroupDimension
->AddGroupItem( aGroup
);
1159 OUString aGroupDimName
= pGroupDimension
->GetGroupDimName();
1161 OUString aGroupName
= pGroupDimension
->CreateGroupName(ScGlobal::GetRscString(STR_PIVOT_GROUP
));
1162 ScDPSaveGroupItem
aGroup( aGroupName
);
1163 ScDPUniqueStringSet::const_iterator it
= aEntries
.begin(), itEnd
= aEntries
.end();
1164 for (; it
!= itEnd
; ++it
)
1166 const OUString
& aEntryName
= *it
;
1167 if ( pBaseGroupDim
)
1169 // for each selected (intermediate) group, add all its items
1170 const ScDPSaveGroupItem
* pBaseGroup
= pBaseGroupDim
->GetNamedGroup( aEntryName
);
1172 aGroup
.AddElementsFromGroup( *pBaseGroup
);
1174 aGroup
.AddElement( aEntryName
); // no group found -> automatic group, add the item itself
1177 aGroup
.AddElement( aEntryName
); // no group dimension, add all items directly
1180 pGroupDimension
->AddGroupItem( aGroup
);
1184 pDimData
->AddGroupDimension( *pNewGroupDim
);
1185 delete pNewGroupDim
; // AddGroupDimension copies the object
1186 // don't access pGroupDimension after here
1188 pGroupDimension
= pNewGroupDim
= NULL
;
1191 ScDPSaveDimension
* pSaveDimension
= aData
.GetDimensionByName( aGroupDimName
);
1192 if ( pSaveDimension
->GetOrientation() == sheet::DataPilotFieldOrientation_HIDDEN
)
1194 ScDPSaveDimension
* pOldDimension
= aData
.GetDimensionByName( aDimName
);
1195 pSaveDimension
->SetOrientation( pOldDimension
->GetOrientation() );
1196 long nPosition
= 0; //! before (immediate) base
1197 aData
.SetPosition( pSaveDimension
, nPosition
);
1201 ScDBDocFunc
aFunc( *GetViewData()->GetDocShell() );
1202 pDPObj
->SetSaveData( aData
);
1203 aFunc
.RefreshPivotTableGroups(pDPObj
);
1205 // unmark cell selection
1209 void ScDBFunc::UngroupDataPilot()
1211 ScDPObject
* pDPObj
= GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
1212 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
1216 ScDPUniqueStringSet aEntries
;
1217 long nSelectDimension
= -1;
1218 GetSelectedMemberList( aEntries
, nSelectDimension
);
1220 if (aEntries
.empty())
1224 OUString aDimName
= pDPObj
->GetDimName( nSelectDimension
, bIsDataLayout
);
1226 ScDPSaveData
aData( *pDPObj
->GetSaveData() );
1227 if (!aData
.GetExistingDimensionData())
1228 // There is nothing to ungroup.
1231 ScDPDimensionSaveData
* pDimData
= aData
.GetDimensionData();
1233 ScDPSaveGroupDimension
* pGroupDim
= pDimData
->GetNamedGroupDimAcc( aDimName
);
1234 const ScDPSaveNumGroupDimension
* pNumGroupDim
= pDimData
->GetNumGroupDim( aDimName
);
1235 if ( ( pGroupDim
&& pGroupDim
->GetDatePart() != 0 ) ||
1236 ( pNumGroupDim
&& pNumGroupDim
->GetDatePart() != 0 ) )
1238 // Date grouping: need to remove all affected group dimensions.
1239 // This is done using DateGroupDataPilot with nParts=0.
1241 DateGroupDataPilot( ScDPNumGroupInfo(), 0 );
1247 ScDPUniqueStringSet::const_iterator it
= aEntries
.begin(), itEnd
= aEntries
.end();
1248 for (; it
!= itEnd
; ++it
)
1249 pGroupDim
->RemoveGroup(*it
);
1251 // remove group dimension if empty
1252 bool bEmptyDim
= pGroupDim
->IsEmpty();
1255 // If all remaining groups in the dimension aren't shown, remove
1256 // the dimension too, as if it was completely empty.
1257 ScDPUniqueStringSet aVisibleEntries
;
1258 pDPObj
->GetMemberResultNames( aVisibleEntries
, nSelectDimension
);
1259 bEmptyDim
= pGroupDim
->HasOnlyHidden( aVisibleEntries
);
1263 pDimData
->RemoveGroupDimension( aDimName
); // pGroupDim is deleted
1265 // also remove SaveData settings for the dimension that no longer exists
1266 aData
.RemoveDimensionByName( aDimName
);
1269 else if ( pNumGroupDim
)
1271 // remove the numerical grouping
1272 pDimData
->RemoveNumGroupDimension( aDimName
);
1273 // SaveData settings can remain unchanged - the same dimension still exists
1277 ScDBDocFunc
aFunc( *GetViewData()->GetDocShell() );
1278 pDPObj
->SetSaveData( aData
);
1279 aFunc
.RefreshPivotTableGroups(pDPObj
);
1281 // unmark cell selection
1285 static OUString
lcl_replaceMemberNameInSubtotal(const OUString
& rSubtotal
, const OUString
& rMemberName
)
1287 sal_Int32 n
= rSubtotal
.getLength();
1288 const sal_Unicode
* p
= rSubtotal
.getStr();
1289 OUStringBuffer aBuf
, aWordBuf
;
1290 for (sal_Int32 i
= 0; i
< n
; ++i
)
1292 sal_Unicode c
= p
[i
];
1295 OUString aWord
= aWordBuf
.makeStringAndClear();
1296 if (aWord
.equals(rMemberName
))
1304 // Escape a backslash character.
1310 // A literal '?' must be escaped with a backslash ('\');
1311 aWordBuf
.append('\\');
1318 if (!aWordBuf
.isEmpty())
1320 OUString aWord
= aWordBuf
.makeStringAndClear();
1321 if (aWord
.equals(rMemberName
))
1327 return aBuf
.makeStringAndClear();
1330 void ScDBFunc::DataPilotInput( const ScAddress
& rPos
, const OUString
& rString
)
1332 using namespace ::com::sun::star::sheet
;
1334 ScDocument
* pDoc
= GetViewData()->GetDocument();
1335 ScDPObject
* pDPObj
= pDoc
->GetDPAtCursor( rPos
.Col(), rPos
.Row(), rPos
.Tab() );
1339 OUString aOldText
= pDoc
->GetString(rPos
.Col(), rPos
.Row(), rPos
.Tab());
1341 if ( aOldText
== rString
)
1343 // nothing to do: silently exit
1347 sal_uInt16 nErrorId
= 0;
1349 pDPObj
->BuildAllDimensionMembers();
1350 ScDPSaveData
aData( *pDPObj
->GetSaveData() );
1351 bool bChange
= false;
1353 sal_uInt16 nOrient
= DataPilotFieldOrientation_HIDDEN
;
1354 long nField
= pDPObj
->GetHeaderDim( rPos
, nOrient
);
1357 // changing a field title
1358 if ( aData
.GetExistingDimensionData() )
1360 // only group dimensions can be renamed
1362 ScDPDimensionSaveData
* pDimData
= aData
.GetDimensionData();
1363 ScDPSaveGroupDimension
* pGroupDim
= pDimData
->GetNamedGroupDimAcc( aOldText
);
1366 // valid name: not empty, no existing dimension (group or other)
1367 if (!rString
.isEmpty() && !pDPObj
->IsDimNameInUse(rString
))
1369 pGroupDim
->Rename( rString
);
1371 // also rename in SaveData to preserve the field settings
1372 ScDPSaveDimension
* pSaveDim
= aData
.GetDimensionByName( aOldText
);
1373 pSaveDim
->SetName( rString
);
1378 nErrorId
= STR_INVALIDNAME
;
1381 else if (nOrient
== DataPilotFieldOrientation_COLUMN
|| nOrient
== DataPilotFieldOrientation_ROW
)
1383 bool bDataLayout
= false;
1384 OUString aDimName
= pDPObj
->GetDimName(nField
, bDataLayout
);
1385 ScDPSaveDimension
* pDim
= bDataLayout
? aData
.GetDataLayoutDimension() : aData
.GetDimensionByName(aDimName
);
1388 if (!rString
.isEmpty())
1390 if (rString
.equalsIgnoreAsciiCase(aDimName
))
1392 pDim
->RemoveLayoutName();
1395 else if (!pDPObj
->IsDimNameInUse(rString
))
1397 pDim
->SetLayoutName(rString
);
1401 nErrorId
= STR_INVALIDNAME
;
1404 nErrorId
= STR_INVALIDNAME
;
1408 else if (pDPObj
->IsDataDescriptionCell(rPos
))
1410 // There is only one data dimension.
1411 ScDPSaveDimension
* pDim
= aData
.GetFirstDimension(sheet::DataPilotFieldOrientation_DATA
);
1414 if (!rString
.isEmpty())
1416 if (pDim
->GetName().equalsIgnoreAsciiCase(rString
))
1418 pDim
->RemoveLayoutName();
1421 else if (!pDPObj
->IsDimNameInUse(rString
))
1423 pDim
->SetLayoutName(rString
);
1427 nErrorId
= STR_INVALIDNAME
;
1430 nErrorId
= STR_INVALIDNAME
;
1435 // This is not a field header.
1436 sheet::DataPilotTableHeaderData aPosData
;
1437 pDPObj
->GetHeaderPositionData(rPos
, aPosData
);
1439 if ((aPosData
.Flags
& MemberResultFlags::HASMEMBER
) && !aOldText
.isEmpty())
1441 if ( aData
.GetExistingDimensionData() && !(aPosData
.Flags
& MemberResultFlags::SUBTOTAL
))
1444 OUString aDimName
= pDPObj
->GetDimName( aPosData
.Dimension
, bIsDataLayout
);
1446 ScDPDimensionSaveData
* pDimData
= aData
.GetDimensionData();
1447 ScDPSaveGroupDimension
* pGroupDim
= pDimData
->GetNamedGroupDimAcc( aDimName
);
1450 // valid name: not empty, no existing group in this dimension
1452 if (!rString
.isEmpty() && !pGroupDim
->GetNamedGroup(rString
))
1454 ScDPSaveGroupItem
* pGroup
= pGroupDim
->GetNamedGroupAcc( aOldText
);
1456 pGroup
->Rename( rString
); // rename the existing group
1459 // create a new group to replace the automatic group
1460 ScDPSaveGroupItem
aGroup( rString
);
1461 aGroup
.AddElement( aOldText
);
1462 pGroupDim
->AddGroupItem( aGroup
);
1465 // in both cases also adjust savedata, to preserve member settings (show details)
1466 ScDPSaveDimension
* pSaveDim
= aData
.GetDimensionByName( aDimName
);
1467 ScDPSaveMember
* pSaveMember
= pSaveDim
->GetExistingMemberByName( aOldText
);
1469 pSaveMember
->SetName( rString
);
1474 nErrorId
= STR_INVALIDNAME
;
1477 else if ((aPosData
.Flags
& MemberResultFlags::GRANDTOTAL
))
1479 aData
.SetGrandTotalName(rString
);
1482 else if (aPosData
.Dimension
>= 0 && !aPosData
.MemberName
.isEmpty())
1484 bool bDataLayout
= false;
1485 OUString aDimName
= pDPObj
->GetDimName(static_cast<long>(aPosData
.Dimension
), bDataLayout
);
1491 if ((aPosData
.Flags
& MemberResultFlags::SUBTOTAL
))
1494 ScDPSaveDimension
* pDim
= aData
.GetDimensionByName(aPosData
.MemberName
);
1498 if (rString
.isEmpty())
1500 nErrorId
= STR_INVALIDNAME
;
1504 if (aPosData
.MemberName
.equalsIgnoreAsciiCase(rString
))
1506 pDim
->RemoveLayoutName();
1509 else if (!pDPObj
->IsDimNameInUse(rString
))
1511 pDim
->SetLayoutName(rString
);
1515 nErrorId
= STR_INVALIDNAME
;
1524 ScDPSaveDimension
* pDim
= aData
.GetDimensionByName(aDimName
);
1528 ScDPSaveMember
* pMem
= pDim
->GetExistingMemberByName(aPosData
.MemberName
);
1532 if ((aPosData
.Flags
& MemberResultFlags::SUBTOTAL
))
1534 // Change subtotal only when the table has one data dimension.
1535 if (aData
.GetDataDimensionCount() > 1)
1538 // display name for subtotal is allowed only if the subtotal type is 'Automatic'.
1539 if (pDim
->GetSubTotalsCount() != 1)
1542 if (pDim
->GetSubTotalFunc(0) != sheet::GeneralFunction_AUTO
)
1545 const OUString
* pLayoutName
= pMem
->GetLayoutName();
1546 OUString aMemberName
;
1548 aMemberName
= *pLayoutName
;
1550 aMemberName
= aPosData
.MemberName
;
1552 OUString aNew
= lcl_replaceMemberNameInSubtotal(rString
, aMemberName
);
1553 pDim
->SetSubtotalName(aNew
);
1558 // Check to make sure the member name isn't
1560 if (!rString
.isEmpty())
1562 if (rString
.equalsIgnoreAsciiCase(pMem
->GetName()))
1564 pMem
->RemoveLayoutName();
1567 else if (!pDim
->IsMemberNameInUse(rString
))
1569 pMem
->SetLayoutName(rString
);
1573 nErrorId
= STR_INVALIDNAME
;
1576 nErrorId
= STR_INVALIDNAME
;
1588 ScDBDocFunc
aFunc( *GetViewData()->GetDocShell() );
1589 pDPObj
->SetSaveData( aData
);
1590 aFunc
.UpdatePivotTable(*pDPObj
, true, false);
1595 nErrorId
= STR_ERR_DATAPILOT_INPUT
;
1596 ErrorMessage( nErrorId
);
1600 static void lcl_MoveToEnd( ScDPSaveDimension
& rDim
, const OUString
& rItemName
)
1602 ScDPSaveMember
* pNewMember
= NULL
;
1603 const ScDPSaveMember
* pOldMember
= rDim
.GetExistingMemberByName( rItemName
);
1605 pNewMember
= new ScDPSaveMember( *pOldMember
);
1607 pNewMember
= new ScDPSaveMember( rItemName
);
1608 rDim
.AddMember( pNewMember
);
1609 // AddMember takes ownership of the new pointer,
1610 // puts it to the end of the list even if it was in the list before.
1613 struct ScOUStringCollate
1615 CollatorWrapper
* mpCollator
;
1617 ScOUStringCollate(CollatorWrapper
* pColl
) : mpCollator(pColl
) {}
1619 bool operator()(const OUString
& rStr1
, const OUString
& rStr2
) const
1621 return ( mpCollator
->compareString(rStr1
, rStr2
) < 0 );
1625 bool ScDBFunc::DataPilotSort( const ScAddress
& rPos
, bool bAscending
, sal_uInt16
* pUserListId
)
1627 ScDocument
* pDoc
= GetViewData()->GetDocument();
1628 ScDPObject
* pDPObj
= pDoc
->GetDPAtCursor(rPos
.Col(), rPos
.Row(), rPos
.Tab());
1632 // We need to run this to get all members later.
1634 pDPObj
->BuildAllDimensionMembers();
1636 sal_uInt16 nOrientation
;
1637 long nDimIndex
= pDPObj
->GetHeaderDim(rPos
, nOrientation
);
1639 // Invalid dimension index. Bail out.
1642 ScDPSaveData
* pSaveData
= pDPObj
->GetSaveData();
1646 ScDPSaveData
aNewSaveData(*pSaveData
);
1648 OUString aDimName
= pDPObj
->GetDimName(nDimIndex
, bDataLayout
);
1649 ScDPSaveDimension
* pSaveDim
= aNewSaveData
.GetDimensionByName(aDimName
);
1653 // manual evaluation of sort order is only needed if a user list id is given
1656 typedef ScDPSaveDimension::MemberList MemList
;
1657 const MemList
& rDimMembers
= pSaveDim
->GetMembers();
1658 list
<OUString
> aMembers
;
1659 boost::unordered_set
<OUString
, OUStringHash
> aMemberSet
;
1660 size_t nMemberCount
= 0;
1661 for (MemList::const_iterator itr
= rDimMembers
.begin(), itrEnd
= rDimMembers
.end();
1662 itr
!= itrEnd
; ++itr
)
1664 ScDPSaveMember
* pMem
= *itr
;
1665 aMembers
.push_back(pMem
->GetName());
1666 aMemberSet
.insert(pMem
->GetName());
1670 // Sort the member list in ascending order.
1671 ScOUStringCollate
aCollate( ScGlobal::GetCollator() );
1672 aMembers
.sort(aCollate
);
1674 // Collect and rank those custom sort strings that also exist in the member name list.
1676 typedef boost::unordered_map
<OUString
, sal_uInt16
, OUStringHash
> UserSortMap
;
1677 UserSortMap aSubStrs
;
1678 sal_uInt16 nSubCount
= 0;
1681 ScUserList
* pUserList
= ScGlobal::GetUserList();
1686 size_t n
= pUserList
->size();
1687 if (!n
|| *pUserListId
>= static_cast<sal_uInt16
>(n
))
1691 const ScUserListData
* pData
= (*pUserList
)[*pUserListId
];
1694 sal_uInt16 n
= pData
->GetSubCount();
1695 for (sal_uInt16 i
= 0; i
< n
; ++i
)
1697 OUString aSub
= pData
->GetSubStr(i
);
1698 if (!aMemberSet
.count(aSub
))
1699 // This string doesn't exist in the member name set. Don't add this.
1702 aSubStrs
.insert(UserSortMap::value_type(aSub
, nSubCount
++));
1707 // Rank all members.
1709 vector
<OUString
> aRankedNames(nMemberCount
);
1710 sal_uInt16 nCurStrId
= 0;
1711 for (list
<OUString
>::const_iterator itr
= aMembers
.begin(), itrEnd
= aMembers
.end();
1712 itr
!= itrEnd
; ++itr
)
1714 OUString aName
= *itr
;
1715 sal_uInt16 nRank
= 0;
1716 UserSortMap::const_iterator itrSub
= aSubStrs
.find(aName
);
1717 if (itrSub
== aSubStrs
.end())
1718 nRank
= nSubCount
+ nCurStrId
++;
1720 nRank
= itrSub
->second
;
1723 nRank
= static_cast< sal_uInt16
>( nMemberCount
- nRank
- 1 );
1725 aRankedNames
[nRank
] = aName
;
1728 // Re-order ScDPSaveMember instances with the new ranks.
1730 for (vector
<OUString
>::const_iterator itr
= aRankedNames
.begin(), itrEnd
= aRankedNames
.end();
1731 itr
!= itrEnd
; ++itr
)
1733 const ScDPSaveMember
* pOldMem
= pSaveDim
->GetExistingMemberByName(*itr
);
1735 // All members are supposed to be present.
1738 ScDPSaveMember
* pNewMem
= new ScDPSaveMember(*pOldMem
);
1739 pSaveDim
->AddMember(pNewMem
);
1742 // Set the sorting mode to manual for now. We may introduce a new sorting
1745 sheet::DataPilotFieldSortInfo aSortInfo
;
1746 aSortInfo
.Mode
= sheet::DataPilotFieldSortMode::MANUAL
;
1747 pSaveDim
->SetSortInfo(&aSortInfo
);
1751 // without user list id, just apply sorting mode
1753 sheet::DataPilotFieldSortInfo aSortInfo
;
1754 aSortInfo
.Mode
= sheet::DataPilotFieldSortMode::NAME
;
1755 aSortInfo
.IsAscending
= bAscending
;
1756 pSaveDim
->SetSortInfo(&aSortInfo
);
1759 // Update the datapilot with the newly sorted field members.
1761 boost::scoped_ptr
<ScDPObject
> pNewObj(new ScDPObject(*pDPObj
));
1762 pNewObj
->SetSaveData(aNewSaveData
);
1763 ScDBDocFunc
aFunc(*GetViewData()->GetDocShell());
1765 return aFunc
.DataPilotUpdate(pDPObj
, pNewObj
.get(), true, false);
1768 bool ScDBFunc::DataPilotMove( const ScRange
& rSource
, const ScAddress
& rDest
)
1771 ScDocument
* pDoc
= GetViewData()->GetDocument();
1772 ScDPObject
* pDPObj
= pDoc
->GetDPAtCursor( rSource
.aStart
.Col(), rSource
.aStart
.Row(), rSource
.aStart
.Tab() );
1773 if ( pDPObj
&& pDPObj
== pDoc
->GetDPAtCursor( rDest
.Col(), rDest
.Row(), rDest
.Tab() ) )
1775 sheet::DataPilotTableHeaderData aDestData
;
1776 pDPObj
->GetHeaderPositionData( rDest
, aDestData
);
1777 bool bValid
= ( aDestData
.Dimension
>= 0 ); // dropping onto a field
1779 // look through the source range
1780 boost::unordered_set
< OUString
, OUStringHash
, std::equal_to
<OUString
> > aMembersSet
; // for lookup
1781 std::vector
< OUString
> aMembersVector
; // members in original order, for inserting
1782 aMembersVector
.reserve( std::max( static_cast<SCSIZE
>( rSource
.aEnd
.Col() - rSource
.aStart
.Col() + 1 ),
1783 static_cast<SCSIZE
>( rSource
.aEnd
.Row() - rSource
.aStart
.Row() + 1 ) ) );
1784 for (SCROW nRow
= rSource
.aStart
.Row(); bValid
&& nRow
<= rSource
.aEnd
.Row(); ++nRow
)
1785 for (SCCOL nCol
= rSource
.aStart
.Col(); bValid
&& nCol
<= rSource
.aEnd
.Col(); ++nCol
)
1787 sheet::DataPilotTableHeaderData aSourceData
;
1788 pDPObj
->GetHeaderPositionData( ScAddress( nCol
, nRow
, rSource
.aStart
.Tab() ), aSourceData
);
1789 if ( aSourceData
.Dimension
== aDestData
.Dimension
&& !aSourceData
.MemberName
.isEmpty() )
1791 if ( aMembersSet
.find( aSourceData
.MemberName
) == aMembersSet
.end() )
1793 aMembersSet
.insert( aSourceData
.MemberName
);
1794 aMembersVector
.push_back( aSourceData
.MemberName
);
1796 // duplicates are ignored
1799 bValid
= false; // empty (subtotal) or different field
1805 OUString aDimName
= pDPObj
->GetDimName( aDestData
.Dimension
, bIsDataLayout
);
1806 if ( !bIsDataLayout
)
1808 ScDPSaveData
aData( *pDPObj
->GetSaveData() );
1809 ScDPSaveDimension
* pDim
= aData
.GetDimensionByName( aDimName
);
1811 // get all member names in source order
1812 uno::Sequence
<OUString
> aMemberNames
;
1813 pDPObj
->GetMemberNames( aDestData
.Dimension
, aMemberNames
);
1815 bool bInserted
= false;
1817 sal_Int32 nMemberCount
= aMemberNames
.getLength();
1818 for (sal_Int32 nMemberPos
=0; nMemberPos
<nMemberCount
; ++nMemberPos
)
1820 OUString
aMemberStr( aMemberNames
[nMemberPos
] );
1822 if ( !bInserted
&& aMemberNames
[nMemberPos
] == aDestData
.MemberName
)
1824 // insert dragged items before this item
1825 for ( std::vector
<OUString
>::const_iterator aIter
= aMembersVector
.begin();
1826 aIter
!= aMembersVector
.end(); ++aIter
)
1827 lcl_MoveToEnd( *pDim
, *aIter
);
1831 if ( aMembersSet
.find( aMemberStr
) == aMembersSet
.end() ) // skip dragged items
1832 lcl_MoveToEnd( *pDim
, aMemberStr
);
1834 // insert dragged item at end if dest wasn't found (for example, empty)
1836 for ( std::vector
<OUString
>::const_iterator aIter
= aMembersVector
.begin();
1837 aIter
!= aMembersVector
.end(); ++aIter
)
1838 lcl_MoveToEnd( *pDim
, *aIter
);
1840 // Items that were in SaveData, but not in the source, end up at the start of the list.
1842 // set flag for manual sorting
1843 sheet::DataPilotFieldSortInfo aSortInfo
;
1844 aSortInfo
.Mode
= sheet::DataPilotFieldSortMode::MANUAL
;
1845 pDim
->SetSortInfo( &aSortInfo
);
1848 ScDBDocFunc
aFunc( *GetViewData()->GetDocShell() );
1849 boost::scoped_ptr
<ScDPObject
> pNewObj(new ScDPObject( *pDPObj
));
1850 pNewObj
->SetSaveData( aData
);
1851 aFunc
.DataPilotUpdate( pDPObj
, pNewObj
.get(), true, false ); //! bApi for drag&drop?
1854 Unmark(); // entry was moved - no use in leaving the old cell selected
1864 bool ScDBFunc::HasSelectionForDrillDown( sal_uInt16
& rOrientation
)
1868 ScDPObject
* pDPObj
= GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
1869 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
1872 ScDPUniqueStringSet aEntries
;
1873 long nSelectDimension
= -1;
1874 GetSelectedMemberList( aEntries
, nSelectDimension
);
1876 if (!aEntries
.empty())
1879 OUString aDimName
= pDPObj
->GetDimName( nSelectDimension
, bIsDataLayout
);
1880 if ( !bIsDataLayout
)
1882 ScDPSaveData
* pSaveData
= pDPObj
->GetSaveData();
1883 ScDPSaveDimension
* pDim
= pSaveData
->GetExistingDimensionByName( aDimName
);
1886 sal_uInt16 nDimOrient
= pDim
->GetOrientation();
1887 ScDPSaveDimension
* pInner
= pSaveData
->GetInnermostDimension( nDimOrient
);
1888 if ( pDim
== pInner
)
1890 rOrientation
= nDimOrient
;
1901 void ScDBFunc::SetDataPilotDetails(bool bShow
, const OUString
* pNewDimensionName
)
1903 ScDPObject
* pDPObj
= GetViewData()->GetDocument()->GetDPAtCursor( GetViewData()->GetCurX(),
1904 GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
1907 ScDPUniqueStringSet aEntries
;
1908 long nSelectDimension
= -1;
1909 GetSelectedMemberList( aEntries
, nSelectDimension
);
1911 if (!aEntries
.empty())
1914 OUString aDimName
= pDPObj
->GetDimName( nSelectDimension
, bIsDataLayout
);
1915 if ( !bIsDataLayout
)
1917 ScDPSaveData
aData( *pDPObj
->GetSaveData() );
1918 ScDPSaveDimension
* pDim
= aData
.GetDimensionByName( aDimName
);
1920 if ( bShow
&& pNewDimensionName
)
1922 // add the new dimension with the same orientation, at the end
1924 ScDPSaveDimension
* pNewDim
= aData
.GetDimensionByName( *pNewDimensionName
);
1925 ScDPSaveDimension
* pDuplicated
= NULL
;
1926 if ( pNewDim
->GetOrientation() == sheet::DataPilotFieldOrientation_DATA
)
1928 // Need to duplicate the dimension, create column/row in addition to data:
1929 // The duplicated dimension inherits the existing settings, pNewDim is modified below.
1930 pDuplicated
= aData
.DuplicateDimension( *pNewDimensionName
);
1933 sal_uInt16 nOrientation
= pDim
->GetOrientation();
1934 pNewDim
->SetOrientation( nOrientation
);
1936 long nPosition
= LONG_MAX
;
1937 aData
.SetPosition( pNewDim
, nPosition
);
1939 ScDPSaveDimension
* pDataLayout
= aData
.GetDataLayoutDimension();
1940 if ( pDataLayout
->GetOrientation() == nOrientation
&&
1941 aData
.GetDataDimensionCount() <= 1 )
1943 // If there is only one data dimension, the data layout dimension
1944 // must still be the last one in its orientation.
1945 aData
.SetPosition( pDataLayout
, nPosition
);
1950 // The duplicated (data) dimension needs to be behind the original dimension
1951 aData
.SetPosition( pDuplicated
, nPosition
);
1954 // Hide details for all visible members (selected are changed below).
1955 //! Use all members from source level instead (including non-visible)?
1957 ScDPUniqueStringSet aVisibleEntries
;
1958 pDPObj
->GetMemberResultNames( aVisibleEntries
, nSelectDimension
);
1960 ScDPUniqueStringSet::const_iterator it
= aVisibleEntries
.begin(), itEnd
= aVisibleEntries
.end();
1961 for (; it
!= itEnd
; ++it
)
1963 const OUString
& aVisName
= *it
;
1964 ScDPSaveMember
* pMember
= pDim
->GetMemberByName( aVisName
);
1965 pMember
->SetShowDetails( false );
1969 ScDPUniqueStringSet::const_iterator it
= aEntries
.begin(), itEnd
= aEntries
.end();
1970 for (; it
!= itEnd
; ++it
)
1972 ScDPSaveMember
* pMember
= pDim
->GetMemberByName(*it
);
1973 pMember
->SetShowDetails( bShow
);
1977 ScDBDocFunc
aFunc( *GetViewData()->GetDocShell() );
1978 boost::scoped_ptr
<ScDPObject
> pNewObj(new ScDPObject( *pDPObj
));
1979 pNewObj
->SetSaveData( aData
);
1980 aFunc
.DataPilotUpdate( pDPObj
, pNewObj
.get(), true, false );
1983 // unmark cell selection
1990 void ScDBFunc::ShowDataPilotSourceData( ScDPObject
& rDPObj
, const Sequence
<sheet::DataPilotFieldFilter
>& rFilters
)
1992 ScDocument
* pDoc
= GetViewData()->GetDocument();
1993 if (pDoc
->GetDocumentShell()->IsReadOnly())
1995 ErrorMessage(STR_READONLYERR
);
1999 Reference
<sheet::XDimensionsSupplier
> xDimSupplier
= rDPObj
.GetSource();
2000 Reference
<container::XNameAccess
> xDims
= xDimSupplier
->getDimensions();
2001 Reference
<sheet::XDrillDownDataSupplier
> xDDSupplier(xDimSupplier
, UNO_QUERY
);
2002 if (!xDDSupplier
.is())
2005 Sequence
< Sequence
<Any
> > aTabData
= xDDSupplier
->getDrillDownData(rFilters
);
2006 sal_Int32 nRowSize
= aTabData
.getLength();
2008 // There is no data to show. Bail out.
2011 sal_Int32 nColSize
= aTabData
[0].getLength();
2013 SCTAB nNewTab
= GetViewData()->GetTabNo();
2015 boost::scoped_ptr
<ScDocument
> pInsDoc(new ScDocument(SCDOCMODE_CLIP
));
2016 pInsDoc
->ResetClip( pDoc
, nNewTab
);
2017 for (SCROW nRow
= 0; nRow
< nRowSize
; ++nRow
)
2019 for (SCCOL nCol
= 0; nCol
< nColSize
; ++nCol
)
2021 const Any
& rAny
= aTabData
[nRow
][nCol
];
2026 ScSetStringParam aParam
;
2027 aParam
.setTextInput();
2028 pInsDoc
->SetString(ScAddress(nCol
,nRow
,nNewTab
), aStr
);
2030 else if (rAny
>>= fVal
)
2031 pInsDoc
->SetValue(nCol
, nRow
, nNewTab
, fVal
);
2035 // set number format (important for dates)
2036 for (SCCOL nCol
= 0; nCol
< nColSize
; ++nCol
)
2039 if (!(aTabData
[0][nCol
] >>= aStr
))
2042 Reference
<XPropertySet
> xPropSet(xDims
->getByName(aStr
), UNO_QUERY
);
2046 Any any
= xPropSet
->getPropertyValue( OUString(SC_UNO_DP_NUMBERFO
) );
2047 sal_Int32 nNumFmt
= 0;
2048 if (!(any
>>= nNumFmt
))
2051 ScPatternAttr
aPattern( pInsDoc
->GetPool() );
2052 aPattern
.GetItemSet().Put( SfxUInt32Item(ATTR_VALUE_FORMAT
, static_cast<sal_uInt32
>(nNumFmt
)) );
2053 pInsDoc
->ApplyPatternAreaTab(nCol
, 1, nCol
, nRowSize
-1, nNewTab
, aPattern
);
2058 pInsDoc
->GetCellArea( nNewTab
, nEndCol
, nEndRow
);
2059 pInsDoc
->SetClipArea( ScRange( 0, 0, nNewTab
, nEndCol
, nEndRow
, nNewTab
) );
2061 ::svl::IUndoManager
* pMgr
= GetViewData()->GetDocShell()->GetUndoManager();
2062 OUString aUndo
= ScGlobal::GetRscString( STR_UNDO_DOOUTLINE
);
2063 pMgr
->EnterListAction( aUndo
, aUndo
);
2065 OUString aNewTabName
;
2066 pDoc
->CreateValidTabName(aNewTabName
);
2067 if ( InsertTable(aNewTabName
, nNewTab
) )
2068 PasteFromClip( IDF_ALL
, pInsDoc
.get() );
2070 pMgr
->LeaveListAction();
2074 // DB-Operationen (Sortieren, Filtern, Teilergebnisse) wiederholen
2077 void ScDBFunc::RepeatDB( bool bRecord
)
2079 SCCOL nCurX
= GetViewData()->GetCurX();
2080 SCROW nCurY
= GetViewData()->GetCurY();
2081 SCTAB nTab
= GetViewData()->GetTabNo();
2082 ScDocument
* pDoc
= GetViewData()->GetDocument();
2083 ScDBData
* pDBData
= GetDBData();
2084 if (bRecord
&& !pDoc
->IsUndoEnabled())
2087 ScQueryParam aQueryParam
;
2088 pDBData
->GetQueryParam( aQueryParam
);
2089 bool bQuery
= aQueryParam
.GetEntry(0).bDoQuery
;
2091 ScSortParam aSortParam
;
2092 pDBData
->GetSortParam( aSortParam
);
2093 bool bSort
= aSortParam
.maKeyState
[0].bDoSort
;
2095 ScSubTotalParam aSubTotalParam
;
2096 pDBData
->GetSubTotalParam( aSubTotalParam
);
2097 bool bSubTotal
= aSubTotalParam
.bGroupActive
[0] && !aSubTotalParam
.bRemoveOnly
;
2099 if ( bQuery
|| bSort
|| bSubTotal
)
2101 bool bQuerySize
= false;
2104 if (bQuery
&& !aQueryParam
.bInplace
)
2106 ScDBData
* pDest
= pDoc
->GetDBAtCursor( aQueryParam
.nDestCol
, aQueryParam
.nDestRow
,
2107 aQueryParam
.nDestTab
, true );
2108 if (pDest
&& pDest
->IsDoSize())
2110 pDest
->GetArea( aOldQuery
);
2120 pDBData
->GetArea( nDummy
, nStartCol
, nStartRow
, nEndCol
, nEndRow
);
2122 //! Undo nur benoetigte Daten ?
2124 ScDocument
* pUndoDoc
= NULL
;
2125 ScOutlineTable
* pUndoTab
= NULL
;
2126 ScRangeName
* pUndoRange
= NULL
;
2127 ScDBCollection
* pUndoDB
= NULL
;
2131 SCTAB nTabCount
= pDoc
->GetTableCount();
2132 pUndoDoc
= new ScDocument( SCDOCMODE_UNDO
);
2133 ScOutlineTable
* pTable
= pDoc
->GetOutlineTable( nTab
);
2136 pUndoTab
= new ScOutlineTable( *pTable
);
2138 SCCOLROW nOutStartCol
; // Zeilen/Spaltenstatus
2139 SCCOLROW nOutStartRow
;
2140 SCCOLROW nOutEndCol
;
2141 SCCOLROW nOutEndRow
;
2142 pTable
->GetColArray()->GetRange( nOutStartCol
, nOutEndCol
);
2143 pTable
->GetRowArray()->GetRange( nOutStartRow
, nOutEndRow
);
2145 pUndoDoc
->InitUndo( pDoc
, nTab
, nTab
, true, true );
2146 pDoc
->CopyToDocument( static_cast<SCCOL
>(nOutStartCol
), 0, nTab
, static_cast<SCCOL
>(nOutEndCol
), MAXROW
, nTab
, IDF_NONE
, false, pUndoDoc
);
2147 pDoc
->CopyToDocument( 0, nOutStartRow
, nTab
, MAXCOL
, nOutEndRow
, nTab
, IDF_NONE
, false, pUndoDoc
);
2150 pUndoDoc
->InitUndo( pDoc
, nTab
, nTab
, false, true );
2152 // Datenbereich sichern - incl. Filter-Ergebnis
2153 pDoc
->CopyToDocument( 0,nStartRow
,nTab
, MAXCOL
,nEndRow
,nTab
, IDF_ALL
, false, pUndoDoc
);
2155 // alle Formeln wegen Referenzen
2156 pDoc
->CopyToDocument( 0,0,0, MAXCOL
,MAXROW
,nTabCount
-1, IDF_FORMULA
, false, pUndoDoc
);
2158 // DB- und andere Bereiche
2159 ScRangeName
* pDocRange
= pDoc
->GetRangeName();
2160 if (!pDocRange
->empty())
2161 pUndoRange
= new ScRangeName( *pDocRange
);
2162 ScDBCollection
* pDocDB
= pDoc
->GetDBCollection();
2163 if (!pDocDB
->empty())
2164 pUndoDB
= new ScDBCollection( *pDocDB
);
2167 if (bSort
&& bSubTotal
)
2169 // Sortieren ohne SubTotals
2171 aSubTotalParam
.bRemoveOnly
= true; // wird unten wieder zurueckgesetzt
2172 DoSubTotals( aSubTotalParam
, false );
2177 pDBData
->GetSortParam( aSortParam
); // Bereich kann sich geaendert haben
2178 Sort( aSortParam
, false, false);
2182 pDBData
->GetQueryParam( aQueryParam
); // Bereich kann sich geaendert haben
2184 if (pDBData
->GetAdvancedQuerySource(aAdvSource
))
2186 pDoc
->CreateQueryParam(
2187 aAdvSource
.aStart
.Col(), aAdvSource
.aStart
.Row(),
2188 aAdvSource
.aEnd
.Col(), aAdvSource
.aEnd
.Row(),
2189 aAdvSource
.aStart
.Tab(), aQueryParam
);
2190 Query( aQueryParam
, &aAdvSource
, false );
2193 Query( aQueryParam
, NULL
, false );
2195 // bei nicht-inplace kann die Tabelle umgestellt worden sein
2196 if ( !aQueryParam
.bInplace
&& aQueryParam
.nDestTab
!= nTab
)
2201 pDBData
->GetSubTotalParam( aSubTotalParam
); // Bereich kann sich geaendert haben
2202 aSubTotalParam
.bRemoveOnly
= false;
2203 DoSubTotals( aSubTotalParam
, false );
2210 SCROW nDummyRow
, nNewEndRow
;
2211 pDBData
->GetArea( nDummyTab
, nDummyCol
,nDummyRow
, nDummyCol
,nNewEndRow
);
2213 const ScRange
* pOld
= NULL
;
2214 const ScRange
* pNew
= NULL
;
2217 ScDBData
* pDest
= pDoc
->GetDBAtCursor( aQueryParam
.nDestCol
, aQueryParam
.nDestRow
,
2218 aQueryParam
.nDestTab
, true );
2221 pDest
->GetArea( aNewQuery
);
2227 GetViewData()->GetDocShell()->GetUndoManager()->AddUndoAction(
2228 new ScUndoRepeatDB( GetViewData()->GetDocShell(), nTab
,
2229 nStartCol
, nStartRow
, nEndCol
, nEndRow
,
2233 pUndoRange
, pUndoDB
,
2237 GetViewData()->GetDocShell()->PostPaint(
2238 ScRange(0, 0, nTab
, MAXCOL
, MAXROW
, nTab
),
2239 PAINT_GRID
| PAINT_LEFT
| PAINT_TOP
| PAINT_SIZE
);
2241 else // "Keine Operationen auszufuehren"
2242 ErrorMessage(STR_MSSG_REPEATDB_0
);
2245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */