diff --git a/sc/CppunitTest_sc_uicalc2.mk b/sc/CppunitTest_sc_uicalc2.mk index 647d4ec7abed..fbc467622f40 100644 --- a/sc/CppunitTest_sc_uicalc2.mk +++ b/sc/CppunitTest_sc_uicalc2.mk @@ -23,6 +23,7 @@ $(eval $(call gb_CppunitTest_use_libraries,sc_uicalc2, \ comphelper \ cppu \ cppuhelper \ + editeng \ i18nlangtag \ sal \ sc \ diff --git a/sc/qa/unit/uicalc/uicalc2.cxx b/sc/qa/unit/uicalc/uicalc2.cxx index 1eaeb5cac4e3..9be6d83840bb 100644 --- a/sc/qa/unit/uicalc/uicalc2.cxx +++ b/sc/qa/unit/uicalc/uicalc2.cxx @@ -8,6 +8,7 @@ */ #include "../helper/qahelper.hxx" +#include #include #include #include @@ -26,8 +27,10 @@ #include #include #include +#include #include #include +#include #include using namespace ::com::sun::star; @@ -1489,6 +1492,83 @@ CPPUNIT_TEST_FIXTURE(ScUiCalcTest2, testTdf156174) CPPUNIT_ASSERT(!pDBs->empty()); } +CPPUNIT_TEST_FIXTURE(ScUiCalcTest2, testTdf154044) +{ + createScDoc(); + ScDocument* pDoc = getScDoc(); + + auto getBackColor = [pDoc](SCCOL c) { + const ScPatternAttr* pattern = pDoc->GetPattern(c, 0, 0); + const SvxBrushItem& brush = pattern->GetItemSet().Get(ATTR_BACKGROUND); + return brush.GetColor(); + }; + + CPPUNIT_ASSERT_EQUAL(INITIALCOLCOUNT, pDoc->GetAllocatedColumnsCount(0)); + for (SCCOL i = 0; i <= pDoc->MaxCol(); ++i) + { + OString msg = "i=" + OString::number(i); + CPPUNIT_ASSERT_EQUAL_MESSAGE(msg.getStr(), COL_AUTO, getBackColor(i)); + } + + // Set the background color of A1:CV1 + auto aColorArg( + comphelper::InitPropertySequence({ { "BackgroundColor", uno::Any(COL_LIGHTBLUE) } })); + goToCell("A1:CV1"); + dispatchCommand(mxComponent, ".uno:BackgroundColor", aColorArg); + + // Partial row range allocates necessary columns + CPPUNIT_ASSERT_EQUAL(SCCOL(100), pDoc->GetAllocatedColumnsCount(0)); + + // Check that settings are applied + for (SCCOL i = 0; i < 100; ++i) + { + OString msg = "i=" + OString::number(i); + CPPUNIT_ASSERT_EQUAL_MESSAGE(msg.getStr(), COL_LIGHTBLUE, getBackColor(i)); + } + + // Undo + SfxUndoManager* pUndoMgr = pDoc->GetUndoManager(); + CPPUNIT_ASSERT(pUndoMgr); + pUndoMgr->Undo(); + + // Check that all the cells have restored the setting + for (SCCOL i = 0; i < 100; ++i) + { + OString msg = "i=" + OString::number(i); + // Without the fix in place, this would fail with + // - Expected: rgba[ffffff00] + // - Actual : rgba[0000ffff] + // - i=1 + CPPUNIT_ASSERT_EQUAL_MESSAGE(msg.getStr(), COL_AUTO, getBackColor(i)); + } + + // Also check the whole row selection case - it is handled specially: columns are not allocated. + // See commit 3db91487e57277f75d64d95d06d4ddcc29f1c4e0 (set properly attributes for cells in + // unallocated Calc columns, 2022-03-04). + goToCell("A1:" + pDoc->MaxColAsString() + "1"); + dispatchCommand(mxComponent, ".uno:BackgroundColor", aColorArg); + + // Check that settings are applied + for (SCCOL i = 0; i <= pDoc->MaxCol(); ++i) + { + OString msg = "i=" + OString::number(i); + CPPUNIT_ASSERT_EQUAL_MESSAGE(msg.getStr(), COL_LIGHTBLUE, getBackColor(i)); + } + + // Undo + pUndoMgr->Undo(); + + // No additional columns have been allocated for whole-row range + CPPUNIT_ASSERT_EQUAL(SCCOL(100), pDoc->GetAllocatedColumnsCount(0)); + + // Check that all the cells have restored the setting + for (SCCOL i = 0; i <= pDoc->MaxCol(); ++i) + { + OString msg = "i=" + OString::number(i); + CPPUNIT_ASSERT_EQUAL_MESSAGE(msg.getStr(), COL_AUTO, getBackColor(i)); + } +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/core/data/table2.cxx b/sc/source/core/data/table2.cxx index ed8db48982b6..ec7f96731ac6 100644 --- a/sc/source/core/data/table2.cxx +++ b/sc/source/core/data/table2.cxx @@ -1319,6 +1319,13 @@ void ScTable::CopyToTable( const bool bToUndoDoc = pDestTab->rDocument.IsUndo(); const bool bFromUndoDoc = rDocument.IsUndo(); + if (bToUndoDoc && (nFlags & InsertDeleteFlags::ATTRIB) && nCol2 >= aCol.size()) + { + // tdf#154044: Copy also the default column data + aDefaultColData.AttrArray().CopyArea(nRow1, nRow2, 0, + pDestTab->aDefaultColData.AttrArray()); + } + if ((bToUndoDoc || bFromUndoDoc) && (nFlags & InsertDeleteFlags::CONTENTS) && mpRangeName) { // Copying formulas may create sheet-local named expressions on the @@ -1344,6 +1351,16 @@ void ScTable::CopyToTable( for (SCCOL i = nCol1; i <= ClampToAllocatedColumns(nCol2); i++) aCol[i].CopyToColumn(rCxt, nRow1, nRow2, bToUndoDoc ? nFlags : nTempFlags, bMarked, pDestTab->CreateColumnIfNotExists(i), pMarkData, bAsLink, bGlobalNamesToLocal); + // tdf#154044: Restore from the default column data + if (bFromUndoDoc && (nFlags & InsertDeleteFlags::ATTRIB) && nCol2 >= aCol.size()) + { + aDefaultColData.AttrArray().CopyArea(nRow1, nRow2, 0, + pDestTab->aDefaultColData.AttrArray()); + SCCOL nMaxSetDefault = pDestTab->ClampToAllocatedColumns(nCol2); + for (SCCOL i = aCol.size(); i <= nMaxSetDefault; i++) + aDefaultColData.AttrArray().CopyArea(nRow1, nRow2, 0, + pDestTab->aCol[i].AttrArray()); + } } if (!bColRowFlags) // Column widths/Row heights/Flags