sc/source/core/data/documen4.cxx | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-)
New commits: commit 68a595755c82817ee8672aec2c57cd90e3190534 Author: Eike Rathke <er...@redhat.com> AuthorDate: Mon May 30 21:38:41 2022 +0200 Commit: Caolán McNamara <caol...@redhat.com> CommitDate: Sun Jun 5 17:09:41 2022 +0200 InsertMatrixFormula: correct references for across sheets array formulas 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 <er...@redhat.com> Tested-by: Jenkins (cherry picked from commit dfd5081ff3973d5d0f216b06dda6360fa490cc9c) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135154 Reviewed-by: Caolán McNamara <caol...@redhat.com> diff --git a/sc/source/core/data/documen4.cxx b/sc/source/core/data/documen4.cxx index a8e786b68ebf..014350a7a94c 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 : GetColumnsRange(nTab1, nCol1, nCol2)) + for (SCCOL nCol : GetColumnsRange(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<ScTokenArray> pTokArr(aArr.Clone()); + aPos = ScAddress(nCol, nRow, nTab); pCell = new ScFormulaCell(*this, aPos, *pTokArr, eGram, ScMatrixMode::Reference); pTab->SetFormulaCell(nCol, nRow, pCell); }