diff --git a/sc/qa/unit/ucalc_formula2.cxx b/sc/qa/unit/ucalc_formula2.cxx index cf95687ede53..76950c2a399e 100644 --- a/sc/qa/unit/ucalc_formula2.cxx +++ b/sc/qa/unit/ucalc_formula2.cxx @@ -2273,6 +2273,44 @@ CPPUNIT_TEST_FIXTURE(TestFormula2, testMatrixArithmeticMismatchedExtent) m_pDoc->DeleteTab(0); } +CPPUNIT_TEST_FIXTURE(TestFormula2, testMatrixConcatMismatchedExtent) +{ + // String concatenation of two two-dimensional ranges of unequal + // length extends the result to the longer range. Where the two + // ranges overlap the cells join together. The rows past the shorter + // operand have no partner to join and stay empty. + sc::AutoCalcSwitch aACSwitch(*m_pDoc, true); + m_pDoc->InsertTab(0, u"Test"_ustr); + + // First operand A1:B3, three rows by two columns. + m_pDoc->SetString(ScAddress(0, 0, 0), u"a"_ustr); + m_pDoc->SetString(ScAddress(1, 0, 0), u"b"_ustr); + m_pDoc->SetString(ScAddress(0, 1, 0), u"c"_ustr); + m_pDoc->SetString(ScAddress(1, 1, 0), u"d"_ustr); + m_pDoc->SetString(ScAddress(0, 2, 0), u"e"_ustr); + m_pDoc->SetString(ScAddress(1, 2, 0), u"f"_ustr); + + // Second operand D1:E2, two rows by two columns, one row shorter. + m_pDoc->SetString(ScAddress(3, 0, 0), u"1"_ustr); + m_pDoc->SetString(ScAddress(4, 0, 0), u"2"_ustr); + m_pDoc->SetString(ScAddress(3, 1, 0), u"3"_ustr); + m_pDoc->SetString(ScAddress(4, 1, 0), u"4"_ustr); + + ScMarkData aMark(m_pDoc->GetSheetLimits()); + aMark.SelectOneTable(0); + m_pDoc->InsertMatrixFormula(6, 0, 7, 2, aMark, u"=A1:B3&D1:E2"_ustr); + + CPPUNIT_ASSERT_EQUAL(u"a1"_ustr, m_pDoc->GetString(ScAddress(6, 0, 0))); + CPPUNIT_ASSERT_EQUAL(u"b2"_ustr, m_pDoc->GetString(ScAddress(7, 0, 0))); + CPPUNIT_ASSERT_EQUAL(u"c3"_ustr, m_pDoc->GetString(ScAddress(6, 1, 0))); + CPPUNIT_ASSERT_EQUAL(u"d4"_ustr, m_pDoc->GetString(ScAddress(7, 1, 0))); + // The third row is past the second operand, so those cells stay empty. + CPPUNIT_ASSERT_EQUAL(OUString(), m_pDoc->GetString(ScAddress(6, 2, 0))); + CPPUNIT_ASSERT_EQUAL(OUString(), m_pDoc->GetString(ScAddress(7, 2, 0))); + + m_pDoc->DeleteTab(0); +} + CPPUNIT_TEST_FIXTURE(TestFormula2, testMatrixOp) { m_pDoc->InsertTab(0, u"Test"_ustr); diff --git a/sc/source/core/tool/scmatrix.cxx b/sc/source/core/tool/scmatrix.cxx index 71f5e0fb9b4b..339e0a7f21fe 100644 --- a/sc/source/core/tool/scmatrix.cxx +++ b/sc/source/core/tool/scmatrix.cxx @@ -2888,7 +2888,7 @@ void ScMatrixImpl::MatConcat(SCSIZE nMaxCol, SCSIZE nMaxRow, const ScMatrixRef& else xMat1->ExecuteOperation( std::pair(0, 0), - std::pair(nMaxRow - 1, nMaxCol - 1), + std::pair(std::min(nR1, nMaxRow) - 1, std::min(nC1, nMaxCol) - 1), std::move(aDoubleFunc), std::move(aBoolFunc), std::move(aStringFunc), std::move(aEmptyFunc)); std::vector aSharedString(nMaxCol*nMaxRow); @@ -2952,7 +2952,7 @@ void ScMatrixImpl::MatConcat(SCSIZE nMaxCol, SCSIZE nMaxRow, const ScMatrixRef& else xMat2->ExecuteOperation( std::pair(0, 0), - std::pair(nMaxRow - 1, nMaxCol - 1), + std::pair(std::min(nR2, nMaxRow) - 1, std::min(nC2, nMaxCol) - 1), std::move(aDoubleFunc2), std::move(aBoolFunc2), std::move(aStringFunc2), std::move(aEmptyFunc2)); aString.clear();