tdf#125661 : Move the delayed-grouping logic...

introduced in commit

169a1b542165f3444791fd6e672d56d3fe48bd66
avoid possible expensive repetitive formula-group
changes (tdf#102364)

to ScTable::CopyToTable() from ScDocument::CopyToDocument()

Rationale :
In tdf#125661 a delayed-grouping is needed but
ScDocument::CopyTab() is directly called which makes it
skip the delayed-grouping logic in ScDocument::CopyToDocument().
One option is to clone the delayed-grouping logic to CopyTab() too.
But doing this triggers the assert

!pDoc->IsDelayedFormulaGrouping() in
bool ScBroadcastAreaSlot::StartListeningArea()

when running the unit test testCondCopyPasteSheet() in
ucalc_condformat.cxx. This seems to be due to calling
ScTable::CopyConditionalFormat() towards the end in
ScTable::CopyToTable(). So having delayed-grouping
"block" that contains a call(s) to ScTable::CopyToTable() is
not safe. So lets move this inside ScTable::CopyToTable()
covering just the ScColumn::CopyToColumn() calls.

With this patch, sheet copy on the bug-document completes
in about 1m40s.

Change-Id: I5272f495aadab5f93f2698aba11cf2701a604c23
Reviewed-on: https://gerrit.libreoffice.org/74607
Tested-by: Jenkins
Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
diff --git a/sc/source/core/data/document.cxx b/sc/source/core/data/document.cxx
index 106ca90..3a65ae0 100644
--- a/sc/source/core/data/document.cxx
+++ b/sc/source/core/data/document.cxx
@@ -2110,11 +2110,6 @@ void ScDocument::CopyToDocument(const ScRange& rRange,

    sc::AutoCalcSwitch aACSwitch(rDestDoc, false); // avoid multiple calculations

    // tdf#102364 - in some pathological cases CopyToDocument() replacing cells with new cells
    // can lead to repetitive splitting and rejoining of the same formula group, which can get
    // quadratically expensive with large groups. So do the grouping just once at the end.
    sc::DelayFormulaGroupingSwitch delayGrouping( rDestDoc, true );

    sc::CopyToDocContext aCxt(rDestDoc);
    aCxt.setStartListening(false);

@@ -2132,7 +2127,6 @@ void ScDocument::CopyToDocument(const ScRange& rRange,
            /*bGlobalNamesToLocal*/false, /*bCopyCaptions*/true);
    }

    delayGrouping.reset(); // groups need to be updated before setting up listeners
    rDestDoc.StartAllListeners(aNewRange);
}

diff --git a/sc/source/core/data/table2.cxx b/sc/source/core/data/table2.cxx
index 27127d4..2aee672 100644
--- a/sc/source/core/data/table2.cxx
+++ b/sc/source/core/data/table2.cxx
@@ -1128,6 +1128,10 @@ void ScTable::CopyToTable(
    {
        InsertDeleteFlags nTempFlags( nFlags &
                ~InsertDeleteFlags( InsertDeleteFlags::NOTE | InsertDeleteFlags::ADDNOTES));
        // tdf#102364 - in some pathological cases CopyToTable() replacing cells with new cells
        // can lead to repetitive splitting and rejoining of the same formula group, which can get
        // quadratically expensive with large groups. So do the grouping just once at the end.
        sc::DelayFormulaGroupingSwitch delayGrouping( *pDestTab->pDocument, true );
        for (SCCOL i = nCol1; i <= ClampToAllocatedColumns(nCol2); i++)
            aCol[i].CopyToColumn(rCxt, nRow1, nRow2, bIsUndoDoc ? nFlags : nTempFlags, bMarked,
                                 pDestTab->CreateColumnIfNotExists(i), pMarkData, bAsLink, bGlobalNamesToLocal);