sc/Library_sc.mk                           |    1 
 sc/inc/TableContentCopier.hxx              |   45 +++++++++++
 sc/source/core/data/TableContentCopier.cxx |  107 ++++++++++++++++++++++++++++
 sc/source/core/data/documen2.cxx           |  110 -----------------------------
 4 files changed, 155 insertions(+), 108 deletions(-)

New commits:
commit b6ea1518d07c017fe2526bebc715f6a978e4ef8e
Author:     Tomaž Vajngerl <[email protected]>
AuthorDate: Mon Mar 2 18:29:13 2026 +0900
Commit:     Miklos Vajna <[email protected]>
CommitDate: Wed Mar 4 08:39:38 2026 +0100

    sc: move TableContentCopier, comment, fix wrong return value
    
    Move TableContentCopier into own file, so it is easier.
    Document TableContentCopier class.
    Fix the wrong return value in ScDocument::OverwriteContent.
    
    Change-Id: I82ac4f0f663636ed385d5801fe40c03e3709d5fa
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/200779
    Tested-by: Jenkins CollaboraOffice <[email protected]>
    Reviewed-by: Miklos Vajna <[email protected]>

diff --git a/sc/Library_sc.mk b/sc/Library_sc.mk
index 33d8839923a3..8b11c02c904d 100644
--- a/sc/Library_sc.mk
+++ b/sc/Library_sc.mk
@@ -198,6 +198,7 @@ $(eval $(call gb_Library_add_exception_objects,sc,\
     sc/source/core/data/stlsheet \
     sc/source/core/data/subtotalparam \
     sc/source/core/data/tabbgcolor \
+    sc/source/core/data/TableContentCopier \
     sc/source/core/data/table1 \
     sc/source/core/data/table2 \
     sc/source/core/data/table3 \
diff --git a/sc/inc/TableContentCopier.hxx b/sc/inc/TableContentCopier.hxx
new file mode 100644
index 000000000000..9154a5d44700
--- /dev/null
+++ b/sc/inc/TableContentCopier.hxx
@@ -0,0 +1,45 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#pragma once
+
+#include "address.hxx"
+#include "global.hxx"
+
+class ScDocument;
+class ScTable;
+class ScMarkData;
+
+namespace sc
+{
+struct RefUpdateInsertTabContext;
+
+/** Copies the content of one table (sheet) to another within the same 
document. */
+class TableContentCopier
+{
+    ScDocument& mrDoc;
+    const SCTAB mnSourceTabNo;
+    const SCTAB mnTargetTabNo;
+    ScTable* const mpSourceTab;
+    ScTable* const mpTargetTab;
+
+public:
+    TableContentCopier(ScDocument& rDoc, SCTAB nSourceTabNo, SCTAB 
nTargetTabNo);
+
+    ~TableContentCopier();
+
+    void performCopy(const ScMarkData* pOnlyMarked, ScCloneFlags nCloneFlags,
+                     SCTAB nPreviousSourceTabNo = -1);
+    void updateReferencesAfterTabInsertion(RefUpdateInsertTabContext& 
rContext);
+    void recompileTargetFormulas();
+};
+
+} // namespace sc
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/core/data/TableContentCopier.cxx 
b/sc/source/core/data/TableContentCopier.cxx
new file mode 100644
index 000000000000..e090e65a8579
--- /dev/null
+++ b/sc/source/core/data/TableContentCopier.cxx
@@ -0,0 +1,107 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <TableContentCopier.hxx>
+#include <document.hxx>
+#include <table.hxx>
+#include <rangenam.hxx>
+#include <dbdata.hxx>
+#include <markdata.hxx>
+#include <dpobject.hxx>
+#include <refupdatecontext.hxx>
+#include <clipcontext.hxx>
+#include <formulacell.hxx>
+
+sc::TableContentCopier::TableContentCopier(ScDocument& rDoc, SCTAB 
nSourceTabNo, SCTAB nTargetTabNo)
+    : mrDoc(rDoc)
+    , mnSourceTabNo(nSourceTabNo)
+    , mnTargetTabNo(nTargetTabNo)
+    , mpSourceTab(mrDoc.FetchTable(nSourceTabNo))
+    , mpTargetTab(mrDoc.FetchTable(nTargetTabNo))
+{
+}
+
+void sc::TableContentCopier::performCopy(const ScMarkData* pOnlyMarked, 
ScCloneFlags nCloneFlags,
+                                         SCTAB nPreviousSourceTabNo)
+{
+    if (nPreviousSourceTabNo < 0)
+        nPreviousSourceTabNo = mnSourceTabNo;
+
+    const bool bGlobalNamesToLocal = true;
+    const ScRangeName* pNames = mrDoc.GetRangeName(mnSourceTabNo);
+    if (pNames)
+        pNames->CopyUsedNames(mnSourceTabNo, nPreviousSourceTabNo, 
mnTargetTabNo, mrDoc, mrDoc,
+                              bGlobalNamesToLocal);
+    mrDoc.GetRangeName()->CopyUsedNames(-1, nPreviousSourceTabNo, 
mnTargetTabNo, mrDoc, mrDoc,
+                                        bGlobalNamesToLocal);
+
+    sc::CopyToDocContext aCopyDocCxt(mrDoc);
+    if (mrDoc.pDBCollection)
+        mrDoc.pDBCollection->CopyToTable(mnSourceTabNo, mnTargetTabNo);
+    mpSourceTab->CopyToTable(aCopyDocCxt, 0, 0, mrDoc.MaxCol(), mrDoc.MaxRow(),
+                             InsertDeleteFlags::ALL, (pOnlyMarked != nullptr), 
mpTargetTab,
+                             pOnlyMarked, false /*bAsLink*/, true 
/*bColRowFlags*/, nCloneFlags,
+                             false /*bCopyCaptions*/);
+    mpTargetTab->SetTabBgColor(mpSourceTab->GetTabBgColor());
+
+    SCTAB nDz = mnTargetTabNo - mnSourceTabNo;
+    sc::RefUpdateContext aRefContext(mrDoc);
+    aRefContext.meMode = URM_COPY;
+    aRefContext.maRange
+        = ScRange(0, 0, mnTargetTabNo, mrDoc.MaxCol(), mrDoc.MaxRow(), 
mnTargetTabNo);
+    aRefContext.mnTabDelta = nDz;
+    mpTargetTab->UpdateReference(aRefContext);
+
+    sc::SetFormulaDirtyContext aFormulaDirtyCxt;
+    mrDoc.SetAllFormulasDirty(aFormulaDirtyCxt);
+
+    if (mrDoc.mpDrawLayer) //  Skip cloning Note caption object
+        // page is already created in ScTable ctor
+        mrDoc.mpDrawLayer->ScCopyPage(static_cast<sal_uInt16>(mnSourceTabNo),
+                                      static_cast<sal_uInt16>(mnTargetTabNo));
+
+    if (mrDoc.pDPCollection)
+        mrDoc.pDPCollection->CopyToTab(mnSourceTabNo, mnTargetTabNo);
+
+    mpTargetTab->SetPageStyle(mpSourceTab->GetPageStyle());
+    mpTargetTab->SetPendingRowHeights(mpSourceTab->IsPendingRowHeights());
+
+    // Copy the custom print range if exists.
+    mpTargetTab->CopyPrintRange(*mpSourceTab);
+
+    // Copy the RTL settings
+    mpTargetTab->SetLayoutRTL(mpSourceTab->IsLayoutRTL());
+    mpTargetTab->SetLoadingRTL(mpSourceTab->IsLoadingRTL());
+
+    // Finally copy the note captions, which need
+    // 1. the updated source ScColumn::nTab members if the target tab is 
before the source tab
+    // 2. row heights and column widths of the destination
+    // 3. RTL settings of the destination
+    mpSourceTab->CopyCaptionsToTable(0, 0, mrDoc.MaxCol(), mrDoc.MaxRow(), 
mpTargetTab,
+                                     true /*bCloneCaption*/);
+}
+
+void sc::TableContentCopier::updateReferencesAfterTabInsertion(
+    sc::RefUpdateInsertTabContext& rContext)
+{
+    mpTargetTab->UpdateInsertTabAbs(mnTargetTabNo);
+    mpTargetTab->AdjustRelativeTabRefs(mnSourceTabNo, mnTargetTabNo, 
sc::TargetTabState::Inserted);
+    mpSourceTab->UpdateInsertTab(rContext);
+    mpSourceTab->UpdateCompile();
+}
+
+void sc::TableContentCopier::recompileTargetFormulas()
+{
+    //  maybe already compiled in Clone, but used names need recompilation
+    mpTargetTab->UpdateCompile(true);
+}
+
+sc::TableContentCopier::~TableContentCopier() { recompileTargetFormulas(); }
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/core/data/documen2.cxx b/sc/source/core/data/documen2.cxx
index 7fd9d40a3025..883c07c1564b 100644
--- a/sc/source/core/data/documen2.cxx
+++ b/sc/source/core/data/documen2.cxx
@@ -19,6 +19,7 @@
 
 #include <scextopt.hxx>
 #include <autonamecache.hxx>
+#include <TableContentCopier.hxx>
 
 #include <o3tl/test_info.hxx>
 #include <osl/thread.h>
@@ -820,113 +821,6 @@ bool ScDocument::MoveTab( SCTAB nOldPos, SCTAB nNewPos, 
ScProgress* pProgress )
     return bValid;
 }
 
-namespace sc
-{
-
-class TableContentCopier
-{
-    ScDocument& mrDoc;
-    const SCTAB mnSourceTabNo;
-    const SCTAB mnTargetTabNo;
-    ScTable* const mpSourceTab;
-    ScTable* const mpTargetTab;
-public:
-    TableContentCopier(ScDocument& rDoc,
-        SCTAB nSourceTabNo, SCTAB nTargetTabNo);
-
-    ~TableContentCopier();
-
-    void performCopy(const ScMarkData* pOnlyMarked, ScCloneFlags nCloneFlags,
-        SCTAB nPreviousSourceTabNo = -1);
-    void updateReferencesAfterTabInsertion(RefUpdateInsertTabContext& 
rContext);
-    void recompileTargetFormulas();
-};
-
-} // namespace sc
-
-sc::TableContentCopier::TableContentCopier(ScDocument& rDoc,
-    SCTAB nSourceTabNo, SCTAB nTargetTabNo)
-    : mrDoc(rDoc)
-    , mnSourceTabNo(nSourceTabNo)
-    , mnTargetTabNo(nTargetTabNo)
-    , mpSourceTab(mrDoc.FetchTable(nSourceTabNo))
-    , mpTargetTab(mrDoc.FetchTable(nTargetTabNo))
-{
-}
-
-void sc::TableContentCopier::performCopy(const ScMarkData* pOnlyMarked,
-    ScCloneFlags nCloneFlags, SCTAB nPreviousSourceTabNo)
-{
-    if (nPreviousSourceTabNo < 0)
-        nPreviousSourceTabNo = mnSourceTabNo;
-
-    const bool bGlobalNamesToLocal = true;
-    const ScRangeName* pNames = mrDoc.GetRangeName(mnSourceTabNo);
-    if (pNames)
-        pNames->CopyUsedNames(mnSourceTabNo, nPreviousSourceTabNo, 
mnTargetTabNo, mrDoc, mrDoc, bGlobalNamesToLocal);
-    mrDoc.GetRangeName()->CopyUsedNames(-1, nPreviousSourceTabNo, 
mnTargetTabNo, mrDoc, mrDoc, bGlobalNamesToLocal);
-
-    sc::CopyToDocContext aCopyDocCxt(mrDoc);
-    if (mrDoc.pDBCollection)
-        mrDoc.pDBCollection->CopyToTable(mnSourceTabNo, mnTargetTabNo);
-    mpSourceTab->CopyToTable(aCopyDocCxt, 0, 0, mrDoc.MaxCol(), 
mrDoc.MaxRow(), InsertDeleteFlags::ALL,
-            (pOnlyMarked != nullptr), mpTargetTab, pOnlyMarked,
-            false /*bAsLink*/, true /*bColRowFlags*/, nCloneFlags, false 
/*bCopyCaptions*/);
-    mpTargetTab->SetTabBgColor(mpSourceTab->GetTabBgColor());
-
-    SCTAB nDz = mnTargetTabNo - mnSourceTabNo;
-    sc::RefUpdateContext aRefContext(mrDoc);
-    aRefContext.meMode = URM_COPY;
-    aRefContext.maRange = ScRange(0, 0, mnTargetTabNo, mrDoc.MaxCol(), 
mrDoc.MaxRow(), mnTargetTabNo);
-    aRefContext.mnTabDelta = nDz;
-    mpTargetTab->UpdateReference(aRefContext);
-
-    sc::SetFormulaDirtyContext aFormulaDirtyCxt;
-    mrDoc.SetAllFormulasDirty(aFormulaDirtyCxt);
-
-    if (mrDoc.mpDrawLayer) //  Skip cloning Note caption object
-        // page is already created in ScTable ctor
-        mrDoc.mpDrawLayer->ScCopyPage( static_cast<sal_uInt16>(mnSourceTabNo), 
static_cast<sal_uInt16>(mnTargetTabNo) );
-
-    if (mrDoc.pDPCollection)
-        mrDoc.pDPCollection->CopyToTab(mnSourceTabNo, mnTargetTabNo);
-
-    mpTargetTab->SetPageStyle( mpSourceTab->GetPageStyle() );
-    mpTargetTab->SetPendingRowHeights( mpSourceTab->IsPendingRowHeights() );
-
-    // Copy the custom print range if exists.
-    mpTargetTab->CopyPrintRange(*mpSourceTab);
-
-    // Copy the RTL settings
-    mpTargetTab->SetLayoutRTL(mpSourceTab->IsLayoutRTL());
-    mpTargetTab->SetLoadingRTL(mpSourceTab->IsLoadingRTL());
-
-    // Finally copy the note captions, which need
-    // 1. the updated source ScColumn::nTab members if the target tab is 
before the source tab
-    // 2. row heights and column widths of the destination
-    // 3. RTL settings of the destination
-    mpSourceTab->CopyCaptionsToTable( 0, 0, mrDoc.MaxCol(), mrDoc.MaxRow(), 
mpTargetTab, true /*bCloneCaption*/);
-}
-
-void 
sc::TableContentCopier::updateReferencesAfterTabInsertion(sc::RefUpdateInsertTabContext&
 rContext)
-{
-    mpTargetTab->UpdateInsertTabAbs(mnTargetTabNo);
-    mpTargetTab->AdjustRelativeTabRefs(mnSourceTabNo, mnTargetTabNo, 
sc::TargetTabState::Inserted);
-    mpSourceTab->UpdateInsertTab(rContext);
-    mpSourceTab->UpdateCompile();
-}
-
-void sc::TableContentCopier::recompileTargetFormulas()
-{
-    //  maybe already compiled in Clone, but used names need recompilation
-    mpTargetTab->UpdateCompile(true);
-}
-
-sc::TableContentCopier::~TableContentCopier()
-{
-    recompileTargetFormulas();
-}
-
 bool ScDocument::OverwriteContent(SCTAB nSourceTabNo, SCTAB nTargetTabNo)
 {
     ScTable* pSourceTable = FetchTable(nSourceTabNo);
@@ -948,7 +842,7 @@ bool ScDocument::OverwriteContent(SCTAB nSourceTabNo, SCTAB 
nTargetTabNo)
         maTabs[nTargetTabNo]->StartListeners(aSLCxt, true);
         return true;
     }
-    return true;
+    return false;
 }
 
 

Reply via email to