use ScInterpreterContext in ScValueIterator

Otherwise GetFormatTable() asserts with fdo#74507-1 (and
mnOpenCLMinimumFormulaGroupSize disabled).

Change-Id: Ic624076bd552a2f8370f942063e8b79ca703d9fc
Reviewed-on: https://gerrit.libreoffice.org/56288
Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
Tested-by: Luboš Luňák <l.lunak@collabora.com>
(cherry picked from commit e7792c4bf270d5a98ec5b8d2be2e25c11a8ecf02)
Reviewed-on: https://gerrit.libreoffice.org/56373
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/sc/inc/dociter.hxx b/sc/inc/dociter.hxx
index ac76f74..18b9d78 100644
--- a/sc/inc/dociter.hxx
+++ b/sc/inc/dociter.hxx
@@ -37,6 +37,7 @@ class ScPatternAttr;
class ScAttrArray;
class ScAttrIterator;
class ScFlatBoolRowSegments;
struct ScInterpreterContext;
class ScMatrix;
struct ScDBQueryParamBase;
struct ScQueryParam;
@@ -51,6 +52,7 @@ class ScValueIterator            // walk through all values in an area
    typedef sc::CellStoreType::const_position_type PositionType;

    ScDocument*     pDoc;
    ScInterpreterContext* pContext;
    const ScAttrArray*  pAttrArray;
    sal_uInt32      nNumFormat;     // for CalcAsShown
    sal_uInt32      nNumFmtIndex;
@@ -91,6 +93,8 @@ public:

    /// Does NOT reset rValue if no value found!
    bool GetNext( double& rValue, FormulaError& rErr );

    void SetInterpreterContext( ScInterpreterContext* context ) { pContext = context; }
};

class ScDBQueryDataIterator
diff --git a/sc/source/core/data/dociter.cxx b/sc/source/core/data/dociter.cxx
index 374a491..959ffd2 100644
--- a/sc/source/core/data/dociter.cxx
+++ b/sc/source/core/data/dociter.cxx
@@ -77,7 +77,7 @@ void decBlock(std::pair<Iter, size_t>& rPos)

void ScAttrArray_IterGetNumberFormat( sal_uInt32& nFormat, const ScAttrArray*& rpArr,
        SCROW& nAttrEndRow, const ScAttrArray* pNewArr, SCROW nRow,
        const ScDocument* pDoc )
        const ScDocument* pDoc, ScInterpreterContext* pContext = nullptr )
{
    if ( rpArr != pNewArr || nAttrEndRow < nRow )
    {
@@ -90,7 +90,7 @@ void ScAttrArray_IterGetNumberFormat( sal_uInt32& nFormat, const ScAttrArray*& r
            nRowEnd = MAXROW;
        }

        nFormat = pPattern->GetNumberFormat( pDoc->GetFormatTable() );
        nFormat = pPattern->GetNumberFormat( pContext ? pContext->GetFormatTable() : pDoc->GetFormatTable() );
        rpArr = pNewArr;
        nAttrEndRow = nRowEnd;
    }
@@ -99,6 +99,7 @@ void ScAttrArray_IterGetNumberFormat( sal_uInt32& nFormat, const ScAttrArray*& r
ScValueIterator::ScValueIterator( ScDocument* pDocument, const ScRange& rRange,
            SubtotalFlags nSubTotalFlags, bool bTextZero )
    : pDoc(pDocument)
    , pContext(nullptr)
    , pAttrArray(nullptr)
    , nNumFormat(0) // Initialized in GetNumberFormat
    , nNumFmtIndex(0)
@@ -205,8 +206,8 @@ bool ScValueIterator::GetThis(double& rValue, FormulaError& rErr)
                if (bCalcAsShown)
                {
                    ScAttrArray_IterGetNumberFormat(nNumFormat, pAttrArray,
                        nAttrEndRow, pCol->pAttrArray.get(), nCurRow, pDoc);
                    rValue = pDoc->RoundValueAsShown(rValue, nNumFormat);
                        nAttrEndRow, pCol->pAttrArray.get(), nCurRow, pDoc, pContext);
                    rValue = pDoc->RoundValueAsShown(rValue, nNumFormat, pContext);
                }
                return true; // Found it!
            }
diff --git a/sc/source/core/tool/interpr6.cxx b/sc/source/core/tool/interpr6.cxx
index c755469..e1769e2 100644
--- a/sc/source/core/tool/interpr6.cxx
+++ b/sc/source/core/tool/interpr6.cxx
@@ -853,6 +853,7 @@ void ScInterpreter::IterateParameters( ScIterFunc eFunc, bool bTextAsZero )
                else
                {
                    ScValueIterator aValIter( pDok, aRange, mnSubTotalFlags, bTextAsZero );
                    aValIter.SetInterpreterContext( &mrContext );
                    FormulaError nErr = FormulaError::NONE;
                    if (aValIter.GetFirst(fVal, nErr))
                    {