Issue 128494 - Match function error when sheets are re-ordered
Summary: Match function error when sheets are re-ordered
Status: CONFIRMED
Alias: None
Product: Calc
Classification: Application
Component: programming (show other issues)
Version: 4.1.11
Hardware: All All
: P5 (lowest) Normal (vote)
Target Milestone: ---
Assignee: AOO issues mailing list
QA Contact:
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-11-04 01:15 UTC by RonBlackwell
Modified: 2023-04-28 10:30 UTC (History)
4 users (show)

See Also:
Issue Type: DEFECT
Latest Confirmation in: 4.1.14
Developer Difficulty: ---


Attachments
Shows the incorrect match function when sheets are re-ordered (16.19 KB, application/vnd.oasis.opendocument.spreadsheet)
2021-11-04 01:15 UTC, RonBlackwell
no flags Details
Spreadsheet showing how to create match function problems (16.47 KB, application/vnd.oasis.opendocument.spreadsheet)
2021-11-08 22:04 UTC, RonBlackwell
no flags Details
Functions INDEX, COUNTIF, etc. operate correctly but MATCH does not. (17.55 KB, application/vnd.oasis.opendocument.spreadsheet)
2023-03-31 15:21 UTC, RonBlackwell
no flags Details

Note You need to log in before you can comment on or make changes to this issue.
Description RonBlackwell 2021-11-04 01:15:37 UTC
Created attachment 87070 [details]
Shows the incorrect match function when sheets are re-ordered

When sheets are re-ordered, the match function should search the range specified in the formula not one previously associated with the tab. See the attached spreadsheet for a simple example.
Comment 1 RonBlackwell 2021-11-08 22:04:54 UTC
Created attachment 87072 [details]
Spreadsheet showing how to create match function problems

Follow the instructions in the spreadsheet to create the problems.  Don't save the spreadsheet because you may not be able to fix the errors which show up.
Comment 2 RonBlackwell 2021-11-09 21:18:06 UTC
Attachment 87072 [details] clearly shows that the match function does not search the correct range when sheets are moved. The value returned in cells C2 of Sheet1, Sheet2 and Sheet3 should always be 1, 2 and 3, respectively, no matter how the sheets are -rearranged.
Comment 3 RonBlackwell 2023-03-09 22:13:57 UTC
This problem still occurs on 4.1.14.  It seems Calc does not associate ranges - e.g. A2:A4 - with the correct sheet when sheets are re-ordered.
Comment 4 jeffouille 2023-03-11 08:45:44 UTC
Hello,

I confirm with AOO 4.1.13, but sound like a dump of https://bz.apache.org/ooo/show_bug.cgi?id=127479

Ctrl + Shift + F9 give correct value after move sheet.
Comment 5 RonBlackwell 2023-03-12 15:58:15 UTC
This problem report is a follow-on to 127479. It shows that the problem occurs without any use of the INDIRECT function.  If after moving sheet 3 between sheet 1 and sheet 2, you copy sheet2.b2 and paste it as unformatted text it into both sheet2.C2 and sheet2.C3 the results still do not match.  Other functions - e.g. COUNTIF - do not appear to have this problemso it may just be a problem with the match function not re-calculating properly.
Comment 6 RonBlackwell 2023-03-31 15:21:55 UTC
Created attachment 87182 [details]
Functions INDEX, COUNTIF, etc. operate correctly but MATCH does not.

Maybe someone could compare the code for the MATCH function with the code for the INDEX function and figure out how to fix the MATCH function. This problem has been around for a long time now.
Comment 7 Keith N. McKenna 2023-04-03 01:18:51 UTC
Set as confirmed by comment #4
Comment 8 Peter 2023-04-28 10:23:29 UTC
code in question
main/sc/source/core/tool/interpr1.cxx

line:4349

if I check 4377
        else if (GetStackType() == svMatrix)
        {
            pMatSrc = PopMatrix();
            if (!pMatSrc)
            {
                PushIllegalParameter();
                return;
            }
        }

looking at Index (8347) in comparison
case svMatrix:
                {
                    if (nArea != 1)
                        SetError(errIllegalArgument);
                    sal_uInt16 nOldSp = sp;
                    ScMatrixRef pMat = GetMatrix();
                    if (pMat)
                    {
                        SCSIZE nC, nR;
                        pMat->GetDimensions(nC, nR);
                        // Access one element of a vector independent of col/row
                        // orientation?
                        bool bVector = ((nCol == 0 || nRow == 0) && (nC == 1 || nR == 1));
                        SCSIZE nElement = ::std::max( static_cast<SCSIZE>(nCol),
                                static_cast<SCSIZE>(nRow));
                        if (nC == 0 || nR == 0 ||
                                (!bVector && (static_cast<SCSIZE>(nCol) > nC ||
                                              static_cast<SCSIZE>(nRow) > nR)) ||
                                (bVector && nElement > nC * nR))
                            PushIllegalArgument();
                        else if (nCol == 0 && nRow == 0)
                            sp = nOldSp;
                        else if (bVector)
                        {
                            --nElement;
                            if (pMat->IsString( nElement))
                                PushString( pMat->GetString( nElement));
                            else
                                PushDouble( pMat->GetDouble( nElement));
                        }
                        else if (nCol == 0)
                        {
                            ScMatrixRef pResMat = GetNewMat(nC, 1);
                            if (pResMat)
                            {
                                SCSIZE nRowMinus1 = static_cast<SCSIZE>(nRow - 1);
                                for (SCSIZE i = 0; i < nC; i++)
                                    if (!pMat->IsString(i, nRowMinus1))
                                        pResMat->PutDouble(pMat->GetDouble(i,
                                                    nRowMinus1), i, 0);
                                    else
                                        pResMat->PutString(pMat->GetString(i,
                                                    nRowMinus1), i, 0);
                                PushMatrix(pResMat);
                            }
                            else
                                PushIllegalArgument();
                        }
                        else if (nRow == 0)
                        {
                            ScMatrixRef pResMat = GetNewMat(1, nR);
                            if (pResMat)
                            {
                                SCSIZE nColMinus1 = static_cast<SCSIZE>(nCol - 1);
                                for (SCSIZE i = 0; i < nR; i++)
                                    if (!pMat->IsString(nColMinus1, i))
                                        pResMat->PutDouble(pMat->GetDouble(nColMinus1,
                                                    i), i);
                                    else
                                        pResMat->PutString(pMat->GetString(nColMinus1,
                                                    i), i);
                                PushMatrix(pResMat);
                            }
                            else
                                PushIllegalArgument();
                        }
                        else
                        {
                            if (!pMat->IsString( static_cast<SCSIZE>(nCol-1),
                                        static_cast<SCSIZE>(nRow-1)))
                                PushDouble( pMat->GetDouble(
                                            static_cast<SCSIZE>(nCol-1),
                                            static_cast<SCSIZE>(nRow-1)));
                            else
                                PushString( pMat->GetString(
                                            static_cast<SCSIZE>(nCol-1),
                                            static_cast<SCSIZE>(nRow-1)));
                        }
                    }
                }
                break;

ohh my...
Comment 9 Peter 2023-04-28 10:30:19 UTC
I check a fix... maybe this is easy to fix