sc/source/core/data/documen4.cxx |   23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)

New commits:
commit dfd5081ff3973d5d0f216b06dda6360fa490cc9c
Author:     Eike Rathke <er...@redhat.com>
AuthorDate: Mon May 30 21:38:41 2022 +0200
Commit:     Eike Rathke <er...@redhat.com>
CommitDate: Tue May 31 00:11:20 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

diff --git a/sc/source/core/data/documen4.cxx b/sc/source/core/data/documen4.cxx
index 93e5918a31e6..9aa39d66ca94 100644
--- a/sc/source/core/data/documen4.cxx
+++ b/sc/source/core/data/documen4.cxx
@@ -313,13 +313,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);
@@ -333,26 +331,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.
                 ScTokenArray aTokArr(aArr.CloneValue());
+                aPos = ScAddress(nCol, nRow, nTab);
                 pCell = new ScFormulaCell(*this, aPos, aTokArr, eGram, 
ScMatrixMode::Reference);
                 pTab->SetFormulaCell(nCol, nRow, pCell);
             }

Reply via email to