sc/inc/dbdata.hxx | 2 + sc/inc/document.hxx | 2 - sc/qa/unit/data/xlsx/tdf145054.xlsx |binary sc/qa/unit/subsequent_filters-test.cxx | 15 ++++++++++ sc/source/core/data/documen2.cxx | 1 sc/source/core/tool/dbdata.cxx | 49 +++++++++++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 1 deletion(-)
New commits: commit 9e12573119080e151020e470efd8082ec5a3d016 Author: Samuel Mehrbrodt <[email protected]> AuthorDate: Thu Jan 27 09:05:35 2022 +0100 Commit: Thorsten Behrens <[email protected]> CommitDate: Thu Jan 27 18:50:15 2022 +0100 Fix copying named DB when it doesn't contain a '_' Follow-up for d9472a5284fde7bb96823655efcb6eb31f405493 Change-Id: Ib97d2c81159ddcf60604558f38e87af59cef86dd Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129023 Tested-by: Jenkins Reviewed-by: Samuel Mehrbrodt <[email protected]> (cherry picked from commit 26d7f3440a0c9dd179ad407d48ce747699d678dd) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129051 Tested-by: Thorsten Behrens <[email protected]> Reviewed-by: Thorsten Behrens <[email protected]> diff --git a/sc/source/core/tool/dbdata.cxx b/sc/source/core/tool/dbdata.cxx index ebe9a237de3d..405473960eca 100644 --- a/sc/source/core/tool/dbdata.cxx +++ b/sc/source/core/tool/dbdata.cxx @@ -1001,7 +1001,9 @@ OUString lcl_IncrementNumberInNamedRange(ScDBCollection::NamedDBs& namedDBs, const OUString& sOldName,bool bIsUpperName) { sal_Int32 lastIndex = sOldName.lastIndexOf('_'); - sal_Int32 nOldNumber = OUString(sOldName.copy(lastIndex)).toInt32(); + sal_Int32 nOldNumber = 0; + if (lastIndex >= 0) + nOldNumber = OUString(sOldName.copy(lastIndex)).toInt32(); OUString sNewName; do { commit df14a02746b7c3791db6f2ab461d73fe22b230d5 Author: Samuel Mehrbrodt <[email protected]> AuthorDate: Thu Dec 16 11:50:01 2021 +0100 Commit: Thorsten Behrens <[email protected]> CommitDate: Thu Jan 27 18:50:00 2022 +0100 tdf#145054 Copy named DBs too when copying sheet Change-Id: I5bf75a7188532776e70c7af64e88371638d76335 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/126916 Tested-by: Jenkins Reviewed-by: Kohei Yoshida <[email protected]> Reviewed-by: Samuel Mehrbrodt <[email protected]> (cherry picked from commit d9472a5284fde7bb96823655efcb6eb31f405493) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129050 Tested-by: Thorsten Behrens <[email protected]> Reviewed-by: Thorsten Behrens <[email protected]> diff --git a/sc/inc/dbdata.hxx b/sc/inc/dbdata.hxx index b000af812045..f7c4f3d810a1 100644 --- a/sc/inc/dbdata.hxx +++ b/sc/inc/dbdata.hxx @@ -257,6 +257,7 @@ public: ScDBData* findByIndex(sal_uInt16 nIndex); ScDBData* findByUpperName(const OUString& rName); iterator findByUpperName2(const OUString& rName); + ScDBData* findByName(const OUString& rName); /** Takes ownership of p and attempts to insert it into the collection. Deletes p if it could not be inserted, i.e. duplicate name. @@ -333,6 +334,7 @@ public: SCCOL nCol2, SCROW nRow2, SCTAB nTab2, SCCOL nDx, SCROW nDy, SCTAB nDz); void UpdateMoveTab( SCTAB nOldPos, SCTAB nNewPos ); + void CopyToTable(SCTAB nOldPos, SCTAB nNewPos); void SetRefreshHandler( const Link<Timer *, void>& rLink ) { aRefreshHandler = rLink; } diff --git a/sc/inc/document.hxx b/sc/inc/document.hxx index 8091f7e2ffb9..f505f06114a9 100644 --- a/sc/inc/document.hxx +++ b/sc/inc/document.hxx @@ -930,7 +930,7 @@ public: SC_DLLPUBLIC bool RenameTab( SCTAB nTab, const OUString& rName, bool bExternalDocument = false ); bool MoveTab( SCTAB nOldPos, SCTAB nNewPos, ScProgress* pProgress = nullptr ); - bool CopyTab( SCTAB nOldPos, SCTAB nNewPos, + SC_DLLPUBLIC bool CopyTab( SCTAB nOldPos, SCTAB nNewPos, const ScMarkData* pOnlyMarked = nullptr ); SC_DLLPUBLIC sal_uLong TransferTab(ScDocument* pSrcDoc, SCTAB nSrcPos, SCTAB nDestPos, bool bInsertNew = true, diff --git a/sc/qa/unit/data/xlsx/tdf145054.xlsx b/sc/qa/unit/data/xlsx/tdf145054.xlsx new file mode 100644 index 000000000000..8360ec7e92be Binary files /dev/null and b/sc/qa/unit/data/xlsx/tdf145054.xlsx differ diff --git a/sc/qa/unit/subsequent_filters-test.cxx b/sc/qa/unit/subsequent_filters-test.cxx index e005b258d979..07257ff38a56 100644 --- a/sc/qa/unit/subsequent_filters-test.cxx +++ b/sc/qa/unit/subsequent_filters-test.cxx @@ -123,6 +123,7 @@ public: void testMatrixODS(); void testMatrixXLS(); void testDoubleThinBorder(); + void testTdf145054(); void testBorderODS(); void testBordersOoo33(); void testBugFixesODS(); @@ -276,6 +277,7 @@ public: CPPUNIT_TEST(testMatrixODS); CPPUNIT_TEST(testMatrixXLS); CPPUNIT_TEST(testDoubleThinBorder); + CPPUNIT_TEST(testTdf145054); CPPUNIT_TEST(testBorderODS); CPPUNIT_TEST(testBordersOoo33); CPPUNIT_TEST(testBugFixesODS); @@ -974,6 +976,19 @@ void ScFiltersTest::testDoubleThinBorder() xDocSh->DoClose(); } +void ScFiltersTest::testTdf145054() +{ + ScDocShellRef xDocSh = loadDoc(u"tdf145054.", FORMAT_XLSX); + CPPUNIT_ASSERT(xDocSh.is()); + ScDocument& rDoc = xDocSh->GetDocument(); + // Copy sheet + rDoc.CopyTab(0, 1); + CPPUNIT_ASSERT_EQUAL(SCTAB(2), rDoc.GetTableCount()); + // Make sure named DB was copied + ScDBData* pDBData = rDoc.GetDBCollection()->getNamedDBs().findByName("__Anonymous_Sheet_DB__1"); + CPPUNIT_ASSERT(pDBData); +} + void ScFiltersTest::testBorderODS() { ScDocShellRef xDocSh = loadDoc("border.", FORMAT_ODS); diff --git a/sc/source/core/data/documen2.cxx b/sc/source/core/data/documen2.cxx index 7271fdef0d43..57b878476918 100644 --- a/sc/source/core/data/documen2.cxx +++ b/sc/source/core/data/documen2.cxx @@ -831,6 +831,7 @@ bool ScDocument::CopyTab( SCTAB nOldPos, SCTAB nNewPos, const ScMarkData* pOnlyM GetRangeName()->CopyUsedNames( -1, nRealOldPos, nNewPos, *this, *this, bGlobalNamesToLocal); sc::CopyToDocContext aCopyDocCxt(*this); + pDBCollection->CopyToTable(nOldPos, nNewPos); maTabs[nOldPos]->CopyToTable(aCopyDocCxt, 0, 0, MaxCol(), MaxRow(), InsertDeleteFlags::ALL, (pOnlyMarked != nullptr), maTabs[nNewPos].get(), pOnlyMarked, false /*bAsLink*/, true /*bColRowFlags*/, bGlobalNamesToLocal, false /*bCopyCaptions*/ ); diff --git a/sc/source/core/tool/dbdata.cxx b/sc/source/core/tool/dbdata.cxx index e56e7fecf516..ebe9a237de3d 100644 --- a/sc/source/core/tool/dbdata.cxx +++ b/sc/source/core/tool/dbdata.cxx @@ -997,6 +997,20 @@ public: } }; +OUString lcl_IncrementNumberInNamedRange(ScDBCollection::NamedDBs& namedDBs, + const OUString& sOldName,bool bIsUpperName) +{ + sal_Int32 lastIndex = sOldName.lastIndexOf('_'); + sal_Int32 nOldNumber = OUString(sOldName.copy(lastIndex)).toInt32(); + OUString sNewName; + do + { + sNewName = sOldName.copy(0, lastIndex + 1) + OUString::number(++nOldNumber); + } while ((bIsUpperName ? namedDBs.findByUpperName(sNewName) : namedDBs.findByName(sNewName)) + != nullptr); + return sNewName; +} + class FindByCursor { SCCOL const mnCol; @@ -1048,6 +1062,17 @@ public: } }; +class FindByName +{ + const OUString& mrName; +public: + explicit FindByName(const OUString& rName) : mrName(rName) {} + bool operator() (std::unique_ptr<ScDBData> const& p) const + { + return p->GetName() == mrName; + } +}; + class FindByPointer { const ScDBData* mpDBData; @@ -1154,6 +1179,12 @@ auto ScDBCollection::NamedDBs::findByUpperName2(const OUString& rName) -> iterat m_DBs.begin(), m_DBs.end(), FindByUpperName(rName)); } +ScDBData* ScDBCollection::NamedDBs::findByName(const OUString& rName) +{ + DBsType::iterator itr = find_if(m_DBs.begin(), m_DBs.end(), FindByName(rName)); + return itr == m_DBs.end() ? nullptr : itr->get(); +} + bool ScDBCollection::NamedDBs::insert(std::unique_ptr<ScDBData> pData) { auto p = pData.get(); @@ -1465,6 +1496,22 @@ void ScDBCollection::UpdateMoveTab( SCTAB nOldPos, SCTAB nNewPos ) for_each(maAnonDBs.begin(), maAnonDBs.end(), func); } +void ScDBCollection::CopyToTable(SCTAB nOldPos, SCTAB nNewPos) +{ + for (const auto& rxNamedDB : maNamedDBs) + { + if (rxNamedDB->GetTab() != nOldPos) + return; + + OUString newName + = lcl_IncrementNumberInNamedRange(getNamedDBs(), rxNamedDB->GetName(), false); + std::unique_ptr<ScDBData> pDataCopy = std::make_unique<ScDBData>(newName, *rxNamedDB); + pDataCopy->UpdateMoveTab(nOldPos, nNewPos); + pDataCopy->SetIndex(0); + getNamedDBs().insert(std::move(pDataCopy)); + } +} + ScDBData* ScDBCollection::GetDBNearCursor(SCCOL nCol, SCROW nRow, SCTAB nTab ) { ScDBData* pNearData = nullptr;
