sc/qa/unit/ucalc.cxx             |   63 +++++++++++++++++++++++++++++++++++++++
 sc/source/core/data/documen2.cxx |   30 ++++++++++++++++++
 2 files changed, 93 insertions(+)

New commits:
commit c1af1b9c555480305862336954ceefe14865e27d
Author:     Andreas Heinisch <andreas.heini...@yahoo.de>
AuthorDate: Sun Mar 5 17:11:27 2023 +0100
Commit:     Andreas Heinisch <andreas.heini...@yahoo.de>
CommitDate: Wed Mar 8 17:48:11 2023 +0000

    tdf#66613 - Transfer tab: preserve print ranges and col/row repetitions
    
    Change-Id: Ib0e6f9942c2bae1f1fdfa2d0ead0868e9b790b1a
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148284
    Tested-by: Jenkins
    Tested-by: Andreas Heinisch <andreas.heini...@yahoo.de>
    Reviewed-by: Andreas Heinisch <andreas.heini...@yahoo.de>

diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx
index b9217b2b5e6e..7f50f33cb3dc 100644
--- a/sc/qa/unit/ucalc.cxx
+++ b/sc/qa/unit/ucalc.cxx
@@ -387,6 +387,69 @@ CPPUNIT_TEST_FIXTURE(Test, testColumnIterator) // 
tdf#118620
     m_pDoc->DeleteTab(0);
 }
 
+CPPUNIT_TEST_FIXTURE(Test, testTdf66613)
+{
+    // Create different print ranges and col/row repetitions for two tabs
+    const SCTAB nFirstTab = 0;
+    CPPUNIT_ASSERT(m_pDoc->InsertTab(nFirstTab, "FirstPrintRange"));
+    ScRange aFirstPrintRange(0, 0, nFirstTab, 2, 2, nFirstTab);
+    m_pDoc->AddPrintRange(nFirstTab, aFirstPrintRange);
+    ScRange aFirstRepeatColRange(0, 0, nFirstTab, 0, 0, nFirstTab);
+    m_pDoc->SetRepeatColRange(nFirstTab, aFirstRepeatColRange);
+    ScRange aFirstRepeatRowRange(1, 1, nFirstTab, 1, 1, nFirstTab);
+    m_pDoc->SetRepeatRowRange(nFirstTab, aFirstRepeatRowRange);
+
+    const SCTAB nSecondTab = 1;
+    CPPUNIT_ASSERT(m_pDoc->InsertTab(nSecondTab, "SecondPrintRange"));
+    ScRange aSecondPrintRange(0, 0, nSecondTab, 3, 3, nSecondTab);
+    m_pDoc->AddPrintRange(nSecondTab, aSecondPrintRange);
+    ScRange aSecondRepeatColRange(1, 1, nSecondTab, 1, 1, nSecondTab);
+    m_pDoc->SetRepeatColRange(nSecondTab, aSecondRepeatColRange);
+    ScRange aSecondRepeatRowRange(2, 2, nSecondTab, 2, 2, nSecondTab);
+    m_pDoc->SetRepeatRowRange(nSecondTab, aSecondRepeatRowRange);
+
+    // Transfer generated tabs to a new document with different order
+    ScDocument aScDocument;
+    aScDocument.TransferTab(*m_pDoc, nSecondTab, nFirstTab);
+    aScDocument.TransferTab(*m_pDoc, nFirstTab, nSecondTab);
+
+    // Check the number of print ranges in both documents
+    CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(1), 
m_pDoc->GetPrintRangeCount(nFirstTab));
+    CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(1), 
m_pDoc->GetPrintRangeCount(nSecondTab));
+    CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(1), 
aScDocument.GetPrintRangeCount(nFirstTab));
+    CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(1), 
aScDocument.GetPrintRangeCount(nSecondTab));
+
+    // Check the print ranges and col/row repetitions in both documents
+    CPPUNIT_ASSERT_EQUAL(aFirstPrintRange, *m_pDoc->GetPrintRange(nFirstTab, 
0));
+    CPPUNIT_ASSERT_EQUAL(aFirstRepeatColRange, 
*m_pDoc->GetRepeatColRange(nFirstTab));
+    CPPUNIT_ASSERT_EQUAL(aFirstRepeatRowRange, 
*m_pDoc->GetRepeatRowRange(nFirstTab));
+    CPPUNIT_ASSERT_EQUAL(aSecondPrintRange, *m_pDoc->GetPrintRange(nSecondTab, 
0));
+    CPPUNIT_ASSERT_EQUAL(aSecondRepeatColRange, 
*m_pDoc->GetRepeatColRange(nSecondTab));
+    CPPUNIT_ASSERT_EQUAL(aSecondRepeatRowRange, 
*m_pDoc->GetRepeatRowRange(nSecondTab));
+
+    // Tabs have to be adjusted since the order of the tabs is inverted in the 
new document
+    std::vector<ScRange*> aScRanges
+        = { &aFirstPrintRange,  &aFirstRepeatColRange,  &aFirstRepeatRowRange,
+            &aSecondPrintRange, &aSecondRepeatColRange, &aSecondRepeatRowRange 
};
+    for (size_t i = 0; i < aScRanges.size(); i++)
+    {
+        const SCTAB nTab = i >= 3 ? nFirstTab : nSecondTab;
+        aScRanges[i]->aStart.SetTab(nTab);
+        aScRanges[i]->aEnd.SetTab(nTab);
+    }
+
+    // Without the fix in place, no print ranges and col/row repetitions would 
be present
+    CPPUNIT_ASSERT_EQUAL(aFirstPrintRange, 
*aScDocument.GetPrintRange(nSecondTab, 0));
+    CPPUNIT_ASSERT_EQUAL(aFirstRepeatColRange, 
*aScDocument.GetRepeatColRange(nSecondTab));
+    CPPUNIT_ASSERT_EQUAL(aFirstRepeatRowRange, 
*aScDocument.GetRepeatRowRange(nSecondTab));
+    CPPUNIT_ASSERT_EQUAL(aSecondPrintRange, 
*aScDocument.GetPrintRange(nFirstTab, 0));
+    CPPUNIT_ASSERT_EQUAL(aSecondRepeatColRange, 
*aScDocument.GetRepeatColRange(nFirstTab));
+    CPPUNIT_ASSERT_EQUAL(aSecondRepeatRowRange, 
*aScDocument.GetRepeatRowRange(nFirstTab));
+
+    m_pDoc->DeleteTab(nFirstTab);
+    m_pDoc->DeleteTab(nSecondTab);
+}
+
 CPPUNIT_TEST_FIXTURE(Test, testTdf90698)
 {
     CPPUNIT_ASSERT(m_pDoc->InsertTab (0, "Test"));
diff --git a/sc/source/core/data/documen2.cxx b/sc/source/core/data/documen2.cxx
index e21da4be599b..a7ea162177a9 100644
--- a/sc/source/core/data/documen2.cxx
+++ b/sc/source/core/data/documen2.cxx
@@ -1023,6 +1023,36 @@ sal_uLong ScDocument::TransferTab( ScDocument& rSrcDoc, 
SCTAB nSrcPos,
         maTabs[nDestPos]->SetTabNo(nDestPos);
         
maTabs[nDestPos]->SetTabBgColor(rSrcDoc.maTabs[nSrcPos]->GetTabBgColor());
 
+        // tdf#66613 - copy existing print ranges and col/row repetitions
+        if (auto aRepeatColRange = 
rSrcDoc.maTabs[nSrcPos]->GetRepeatColRange())
+        {
+            aRepeatColRange->aStart.SetTab(nDestPos);
+            aRepeatColRange->aEnd.SetTab(nDestPos);
+            maTabs[nDestPos]->SetRepeatColRange(aRepeatColRange);
+        }
+
+        if (auto aRepeatRowRange = 
rSrcDoc.maTabs[nSrcPos]->GetRepeatRowRange())
+        {
+            aRepeatRowRange->aStart.SetTab(nDestPos);
+            aRepeatRowRange->aEnd.SetTab(nDestPos);
+            maTabs[nDestPos]->SetRepeatRowRange(aRepeatRowRange);
+        }
+
+        if (rSrcDoc.IsPrintEntireSheet(nSrcPos))
+            maTabs[nDestPos]->SetPrintEntireSheet();
+        else
+        {
+            const auto nPrintRangeCount = 
rSrcDoc.maTabs[nSrcPos]->GetPrintRangeCount();
+            for (auto nPos = 0; nPos < nPrintRangeCount; nPos++)
+            {
+                // Adjust the tab for the print range at the new position
+                ScRange 
aSrcPrintRange(*rSrcDoc.maTabs[nSrcPos]->GetPrintRange(nPos));
+                aSrcPrintRange.aStart.SetTab(nDestPos);
+                aSrcPrintRange.aEnd.SetTab(nDestPos);
+                maTabs[nDestPos]->AddPrintRange(aSrcPrintRange);
+            }
+        }
+
         if ( !bResultsOnly )
         {
             sc::RefUpdateContext aRefCxt(*this);

Reply via email to