From c9ffec6cdf1af4c9c1e8c6317a59ad5d63093ac7 Mon Sep 17 00:00:00 2001 From: Eike Rathke Date: Mon, 30 May 2022 21:38:41 +0200 Subject: InsertMatrixFormula: correct references for across sheets array formulas MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Inserting an array/matrix formula across two or more selected/marked sheets generated wrong matrix offset references starting with the second sheet, pointing to the top left of the first array formula. Only the top left cell of the inserted formula on each sheet displayed the correct value. Deleting the array formula on the first sheet then left all matrix offset references on the remaining sheets with #REF! errors and those cells could not be deleted anymore because their original parent cell was gone. Change-Id: If5d53311f9aabdcd7432ff26ab555bb91b0b121d Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135147 Reviewed-by: Eike Rathke Tested-by: Jenkins (cherry picked from commit dfd5081ff3973d5d0f216b06dda6360fa490cc9c) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135154 Reviewed-by: Caolán McNamara --- sc/source/core/data/documen4.cxx | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/sc/source/core/data/documen4.cxx b/sc/source/core/data/documen4.cxx index cf9a2fbd0a83..e893c84acaed 100644 --- a/sc/source/core/data/documen4.cxx +++ b/sc/source/core/data/documen4.cxx @@ -306,13 +306,11 @@ void ScDocument::InsertMatrixFormula(SCCOL nCol1, SCROW nRow1, *pCell, *this, ScAddress(nCol1, nRow1, rTab), ScCloneFlags::StartListening)); } - ScAddress aBasePos(nCol1, nRow1, nTab1); ScSingleRefData aRefData; aRefData.InitFlags(); - aRefData.SetColRel( true ); - aRefData.SetRowRel( true ); - aRefData.SetTabRel( true ); - aRefData.SetAddress(GetSheetLimits(), aBasePos, aBasePos); + aRefData.SetRelCol(0); + aRefData.SetRelRow(0); + aRefData.SetRelTab(0); // 2D matrix, always same sheet ScTokenArray aArr(*this); // consists only of one single reference token. formula::FormulaToken* t = aArr.AddMatrixSingleReference(aRefData); @@ -326,26 +324,21 @@ void ScDocument::InsertMatrixFormula(SCCOL nCol1, SCROW nRow1, if (!pTab) continue; - if (nTab != nTab1) - { - aRefData.SetRelTab(nTab - aBasePos.Tab()); - *t->GetSingleRef() = aRefData; - } - - for (SCCOL nCol : GetWritableColumnsRange(nTab1, nCol1, nCol2)) + for (SCCOL nCol : GetWritableColumnsRange(nTab, nCol1, nCol2)) { + aRefData.SetRelCol(nCol1 - nCol); for (SCROW nRow = nRow1; nRow <= nRow2; ++nRow) { if (nCol == nCol1 && nRow == nRow1) // Skip the base position. continue; - // Token array must be cloned so that each formula cell receives its own copy. - aPos = ScAddress(nCol, nRow, nTab); // Reference in each cell must point to the origin cell relative to the current cell. - aRefData.SetAddress(GetSheetLimits(), aBasePos, aPos); + aRefData.SetRelRow(nRow1 - nRow); *t->GetSingleRef() = aRefData; + // Token array must be cloned so that each formula cell receives its own copy. std::unique_ptr pTokArr(aArr.Clone()); + aPos = ScAddress(nCol, nRow, nTab); pCell = new ScFormulaCell(*this, aPos, *pTokArr, eGram, ScMatrixMode::Reference); pTab->SetFormulaCell(nCol, nRow, pCell); } -- cgit