[Libreoffice-commits] core.git: sc/inc sc/qa sc/source

Noel Grandin (via logerrit) logerrit at kemper.freedesktop.org
Sat Dec 14 05:58:45 UTC 2019


 sc/inc/address.hxx                          |   12 -
 sc/inc/compiler.hxx                         |    1 
 sc/inc/refdata.hxx                          |   27 +--
 sc/inc/reftokenhelper.hxx                   |    6 
 sc/inc/sharedformula.hxx                    |    2 
 sc/inc/table.hxx                            |    2 
 sc/inc/token.hxx                            |   10 -
 sc/qa/unit/mark_test.cxx                    |   12 -
 sc/qa/unit/ucalc.cxx                        |   14 -
 sc/qa/unit/ucalc_formula.cxx                |   18 +-
 sc/qa/unit/ucalc_sharedformula.cxx          |    2 
 sc/source/core/data/colorscale.cxx          |   12 -
 sc/source/core/data/column.cxx              |    2 
 sc/source/core/data/column4.cxx             |    8 -
 sc/source/core/data/conditio.cxx            |    4 
 sc/source/core/data/documen4.cxx            |   12 -
 sc/source/core/data/documentimport.cxx      |    6 
 sc/source/core/data/formulacell.cxx         |  115 ++++++++-------
 sc/source/core/data/formulaiter.cxx         |    8 -
 sc/source/core/data/grouptokenconverter.cxx |    4 
 sc/source/core/data/table7.cxx              |    2 
 sc/source/core/tool/address.cxx             |    2 
 sc/source/core/tool/chartlis.cxx            |    6 
 sc/source/core/tool/compiler.cxx            |  197 ++++++++++++++-------------
 sc/source/core/tool/consoli.cxx             |    4 
 sc/source/core/tool/detfunc.cxx             |    2 
 sc/source/core/tool/doubleref.cxx           |   13 +
 sc/source/core/tool/interpr1.cxx            |   18 +-
 sc/source/core/tool/interpr2.cxx            |   12 -
 sc/source/core/tool/interpr4.cxx            |   22 +--
 sc/source/core/tool/rangenam.cxx            |   24 +--
 sc/source/core/tool/refdata.cxx             |   96 +++----------
 sc/source/core/tool/reftokenhelper.cxx      |   20 +-
 sc/source/core/tool/refupdat.cxx            |    4 
 sc/source/core/tool/sharedformula.cxx       |   12 -
 sc/source/core/tool/token.cxx               |  202 ++++++++++++++--------------
 sc/source/filter/excel/excform8.cxx         |    4 
 sc/source/filter/excel/xeformula.cxx        |    2 
 sc/source/filter/lotus/lotform.cxx          |    3 
 sc/source/ui/app/inputhdl.cxx               |    8 -
 sc/source/ui/docshell/arealink.cxx          |    2 
 sc/source/ui/docshell/dbdocfun.cxx          |    4 
 sc/source/ui/docshell/dbdocimp.cxx          |    2 
 sc/source/ui/docshell/docfunc.cxx           |   10 -
 sc/source/ui/docshell/docsh3.cxx            |    8 -
 sc/source/ui/docshell/docsh4.cxx            |    2 
 sc/source/ui/docshell/impex.cxx             |    8 -
 sc/source/ui/formdlg/formula.cxx            |    2 
 sc/source/ui/miscdlgs/anyrefdg.cxx          |    6 
 sc/source/ui/unoobj/chart2uno.cxx           |   31 ++--
 sc/source/ui/view/gridwin.cxx               |    8 -
 sc/source/ui/view/gridwin4.cxx              |    2 
 sc/source/ui/view/select.cxx                |    4 
 sc/source/ui/view/tabview2.cxx              |   16 +-
 sc/source/ui/view/tabview3.cxx              |   12 -
 sc/source/ui/view/tabview4.cxx              |    8 -
 sc/source/ui/view/tabvwsh3.cxx              |    2 
 sc/source/ui/view/viewdata.cxx              |   32 ++--
 sc/source/ui/view/viewfun2.cxx              |    2 
 sc/source/ui/view/viewfun3.cxx              |    6 
 sc/source/ui/view/viewfun6.cxx              |    8 -
 sc/source/ui/view/viewutil.cxx              |    2 
 62 files changed, 551 insertions(+), 556 deletions(-)

New commits:
commit 5bcdbf03012e9d2754c3eb166bd5a01201406d9b
Author:     Noel Grandin <noel.grandin at collabora.co.uk>
AuthorDate: Fri Dec 13 20:45:33 2019 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Sat Dec 14 06:57:53 2019 +0100

    sc: rowcol: tdf#50916 convert Valid* methods
    
    which means we end up passing around ScDocument*
    __everywhere__
    
    Change-Id: I44d235ca5e9d57519f068b6880ee7d66f3ceb529
    Reviewed-on: https://gerrit.libreoffice.org/83548
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/sc/inc/address.hxx b/sc/inc/address.hxx
index 5a1f7d91f2ab..2109d582f67b 100644
--- a/sc/inc/address.hxx
+++ b/sc/inc/address.hxx
@@ -88,13 +88,13 @@ const SCCOL SC_TABSTART_NONE  = SCCOL_MAX;
 
 const SCROW MAXROW_30         = 8191;
 
-[[nodiscard]] inline bool ValidCol( SCCOL nCol, SCCOL nMaxCol = MAXCOL )
+[[nodiscard]] inline bool ValidCol( SCCOL nCol, SCCOL nMaxCol )
 {
     assert(nMaxCol == MAXCOL); // temporary to debug jumbo sheets work
     return nCol >= 0 && nCol <= nMaxCol;
 }
 
-[[nodiscard]] inline bool ValidRow( SCROW nRow, SCROW nMaxRow = MAXROW)
+[[nodiscard]] inline bool ValidRow( SCROW nRow, SCROW nMaxRow)
 {
     assert(nMaxRow == MAXROW); // temporary to debug jumbo sheets work
     return nRow >= 0 && nRow <= nMaxRow;
@@ -110,25 +110,25 @@ const SCROW MAXROW_30         = 8191;
     return nTab >= 0 && nTab <= nMaxTab;
 }
 
-[[nodiscard]] inline bool ValidColRow( SCCOL nCol, SCROW nRow, SCCOL nMaxCol = MAXCOL, SCROW nMaxRow = MAXROW )
+[[nodiscard]] inline bool ValidColRow( SCCOL nCol, SCROW nRow, SCCOL nMaxCol, SCROW nMaxRow )
 {
     assert(nMaxRow == MAXROW); // temporary to debug jumbo sheets work
     return ValidCol(nCol,nMaxCol) && ValidRow(nRow,nMaxRow);
 }
 
-[[nodiscard]] inline bool ValidColRowTab( SCCOL nCol, SCROW nRow, SCTAB nTab, SCCOL nMaxCol = MAXCOL, SCROW nMaxRow = MAXROW )
+[[nodiscard]] inline bool ValidColRowTab( SCCOL nCol, SCROW nRow, SCTAB nTab, SCCOL nMaxCol, SCROW nMaxRow )
 {
     assert(nMaxRow == MAXROW); // temporary to debug jumbo sheets work
     return ValidCol(nCol,nMaxCol) && ValidRow(nRow,nMaxRow) && ValidTab( nTab);
 }
 
-[[nodiscard]] inline SCCOL SanitizeCol( SCCOL nCol, SCCOL nMaxCol = MAXCOL )
+[[nodiscard]] inline SCCOL SanitizeCol( SCCOL nCol, SCCOL nMaxCol )
 {
     assert(nMaxCol == MAXCOL); // temporary to debug jumbo sheets work
     return nCol < 0 ? 0 : (nCol > nMaxCol ? nMaxCol : nCol);
 }
 
-[[nodiscard]] inline SCROW SanitizeRow( SCROW nRow, SCROW nMaxRow = MAXROW )
+[[nodiscard]] inline SCROW SanitizeRow( SCROW nRow, SCROW nMaxRow )
 {
     assert(nMaxRow == MAXROW); // temporary to debug jumbo sheets work
     return nRow < 0 ? 0 : (nRow > nMaxRow ? nMaxRow : nRow);
diff --git a/sc/inc/compiler.hxx b/sc/inc/compiler.hxx
index 623f94945943..eca794a657a5 100644
--- a/sc/inc/compiler.hxx
+++ b/sc/inc/compiler.hxx
@@ -218,6 +218,7 @@ public:
                 const OUString& rName ) const = 0;
 
         virtual void makeExternalRefStr(
+            ScSheetLimits& rLimits,
             OUStringBuffer& rBuffer, const ScAddress& rPos, sal_uInt16 nFileId, const OUString& rFileName,
             const OUString& rTabName, const ScSingleRefData& rRef ) const = 0;
 
diff --git a/sc/inc/refdata.hxx b/sc/inc/refdata.hxx
index a6e2299054b9..055095f485a2 100644
--- a/sc/inc/refdata.hxx
+++ b/sc/inc/refdata.hxx
@@ -24,6 +24,8 @@
 #include "scdllapi.h"
 #include "calcmacros.hxx"
 
+struct ScSheetLimits;
+
 /// Single reference (one address) into the sheet
 struct SC_DLLPUBLIC ScSingleRefData
 {
@@ -55,9 +57,9 @@ public:
     void InitAddress( const ScAddress& rAdr );
     void InitAddress( SCCOL nCol, SCROW nRow, SCTAB nTab );
     /// InitAddressRel: InitFlags and set address, everything relative to rPos
-    void InitAddressRel( const ScAddress& rAdr, const ScAddress& rPos );
+    void InitAddressRel( const ScDocument* pDoc, const ScAddress& rAdr, const ScAddress& rPos );
     /// InitFlags and set address, relative to rPos if rRef says so.
-    void InitFromRefAddress( const ScRefAddress& rRef, const ScAddress& rPos );
+    void InitFromRefAddress( const ScDocument* pDoc, const ScRefAddress& rRef, const ScAddress& rPos );
     sal_uInt8 FlagValue() const { return mnFlagValue;}
 
     void SetColRel( bool bVal ) { Flags.bColRel = bVal; }
@@ -99,10 +101,9 @@ public:
         loaded. */
     bool ValidExternal(const ScDocument* pDoc) const;
 
-    ScAddress toAbs( const ScAddress& rPos ) const;
+    ScAddress toAbs( ScSheetLimits& rLimits, const ScAddress& rPos ) const;
     ScAddress toAbs( const ScDocument* pDoc, const ScAddress& rPos ) const;
-    void SetAddress( const ScAddress& rAddr, const ScAddress& rPos );
-    void SetAddress( const ScDocument* pDoc, const ScAddress& rAddr, const ScAddress& rPos );
+    void SetAddress( ScSheetLimits& rLimits, const ScAddress& rAddr, const ScAddress& rPos );
     SCROW Row() const;
     SCCOL Col() const;
     SCTAB Tab() const;
@@ -131,10 +132,10 @@ struct ScComplexRefData
             Ref1.InitAddress( rRange.aStart );
             Ref2.InitAddress( rRange.aEnd );
         }
-    void InitRangeRel( const ScRange& rRange, const ScAddress& rPos )
+    void InitRangeRel( const ScDocument* pDoc, const ScRange& rRange, const ScAddress& rPos )
         {
-            Ref1.InitAddressRel( rRange.aStart, rPos );
-            Ref2.InitAddressRel( rRange.aEnd, rPos );
+            Ref1.InitAddressRel( pDoc, rRange.aStart, rPos );
+            Ref2.InitAddressRel( pDoc, rRange.aEnd, rPos );
         }
     void InitRange( SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
                             SCCOL nCol2, SCROW nRow2, SCTAB nTab2 )
@@ -144,7 +145,7 @@ struct ScComplexRefData
         }
 
     /// InitFlags and set range, relative to rPos if rRef1 and rRef2 say so.
-    void InitFromRefAddresses( const ScRefAddress& rRef1, const ScRefAddress& rRef2, const ScAddress& rPos );
+    void InitFromRefAddresses( const ScDocument* pDoc, const ScRefAddress& rRef1, const ScRefAddress& rRef2, const ScAddress& rPos );
 
     bool Valid(const ScDocument* pDoc) const;
 
@@ -167,12 +168,12 @@ struct ScComplexRefData
         return Ref1.Col() == 0 && Ref2.Col() == MAXCOL && !Ref1.IsColRel() && !Ref2.IsColRel();
     }
 
-    SC_DLLPUBLIC ScRange toAbs( const ScAddress& rPos ) const;
+    SC_DLLPUBLIC ScRange toAbs( ScSheetLimits& rLimits, const ScAddress& rPos ) const;
     SC_DLLPUBLIC ScRange toAbs( const ScDocument* pDoc, const ScAddress& rPos ) const;
 
     /** Set a new range, assuming that the ordering of the range matches the
         ordering of the reference data flags already set. */
-    void SetRange( const ScRange& rRange, const ScAddress& rPos );
+    void SetRange( ScSheetLimits& rLimits, const ScRange& rRange, const ScAddress& rPos );
 
     /** Adjust ordering (front-top-left/rear-bottom-right) to a new position. */
     void PutInOrder( const ScAddress& rPos );
@@ -182,8 +183,8 @@ struct ScComplexRefData
     /** Enlarge range if reference passed is not within existing range.
         ScAddress position is used to calculate absolute references from
         relative references. */
-    ScComplexRefData& Extend( const ScSingleRefData & rRef, const ScAddress & rPos );
-    ScComplexRefData& Extend( const ScComplexRefData & rRef, const ScAddress & rPos );
+    ScComplexRefData& Extend( ScSheetLimits& rLimits, const ScSingleRefData & rRef, const ScAddress & rPos );
+    ScComplexRefData& Extend( ScSheetLimits& rLimits, const ScComplexRefData & rRef, const ScAddress & rPos );
 
     /** Increment or decrement end column unless or until sticky.
         @see ScRange::IncEndColSticky()
diff --git a/sc/inc/reftokenhelper.hxx b/sc/inc/reftokenhelper.hxx
index 759d69c5762e..a63d882011d7 100644
--- a/sc/inc/reftokenhelper.hxx
+++ b/sc/inc/reftokenhelper.hxx
@@ -40,9 +40,9 @@ namespace ScRefTokenHelper
         ::std::vector<ScTokenRef>& rRefTokens, const OUString& rRangeStr, ScDocument* pDoc,
         const sal_Unicode cSep, ::formula::FormulaGrammar::Grammar eGrammar, bool bOnly3DRef = false);
 
-    bool getRangeFromToken(ScRange& rRange, const ScTokenRef& pToken, const ScAddress& rPos, bool bExternal = false);
+    bool getRangeFromToken(const ScDocument* pDoc, ScRange& rRange, const ScTokenRef& pToken, const ScAddress& rPos, bool bExternal = false);
 
-    void getRangeListFromTokens(ScRangeList& rRangeList, const ::std::vector<ScTokenRef>& pTokens, const ScAddress& rPos);
+    void getRangeListFromTokens(const ScDocument* pDoc, ScRangeList& rRangeList, const ::std::vector<ScTokenRef>& pTokens, const ScAddress& rPos);
 
     /**
      * Create a double reference token from a range object.
@@ -54,7 +54,7 @@ namespace ScRefTokenHelper
     bool SC_DLLPUBLIC isRef(const ScTokenRef& pToken);
     bool SC_DLLPUBLIC isExternalRef(const ScTokenRef& pToken);
 
-    bool SC_DLLPUBLIC intersects(
+    bool SC_DLLPUBLIC intersects(const ScDocument* pDoc,
         const ::std::vector<ScTokenRef>& rTokens, const ScTokenRef& pToken, const ScAddress& rPos);
 
     void SC_DLLPUBLIC join(const ScDocument* pDoc, ::std::vector<ScTokenRef>& rTokens, const ScTokenRef& pToken, const ScAddress& rPos);
diff --git a/sc/inc/sharedformula.hxx b/sc/inc/sharedformula.hxx
index b8ba5555812d..c61ce62b6b79 100644
--- a/sc/inc/sharedformula.hxx
+++ b/sc/inc/sharedformula.hxx
@@ -85,7 +85,7 @@ public:
      * @return TRUE if there indeed was a split, else FALSE (e.g. split
      *         positions were only top or bottom cells or no formula group).
      */
-    static bool splitFormulaCellGroups(CellStoreType& rCells, std::vector<SCROW>& rBounds);
+    static bool splitFormulaCellGroups(const ScDocument* pDoc, CellStoreType& rCells, std::vector<SCROW>& rBounds);
 
     /**
      * See if two specified adjacent formula cells can be merged, and if they
diff --git a/sc/inc/table.hxx b/sc/inc/table.hxx
index 681167b9c5f6..8feb233480cf 100644
--- a/sc/inc/table.hxx
+++ b/sc/inc/table.hxx
@@ -449,7 +449,7 @@ public:
 
     CellType    GetCellType( const ScAddress& rPos ) const
                     {
-                        if (!ValidColRow(rPos.Col(),rPos.Row()))
+                        if (!GetDoc().ValidColRow(rPos.Col(),rPos.Row()))
                             return CELLTYPE_NONE;
                         if (rPos.Col() >= aCol.size())
                             return CELLTYPE_NONE;
diff --git a/sc/inc/token.hxx b/sc/inc/token.hxx
index ff52adac2031..433a5e33ab74 100644
--- a/sc/inc/token.hxx
+++ b/sc/inc/token.hxx
@@ -60,10 +60,11 @@ formula::FormulaTokenRef extendRangeReference( ScSheetLimits& rLimits, formula::
 class ScSingleRefToken final : public formula::FormulaToken
 {
 private:
+    ScSheetLimits&    mrSheetLimits; // don't use rtl::Reference to avoid ref-counting traffic
     ScSingleRefData   aSingleRef;
 public:
-                                ScSingleRefToken( ScSheetLimits& , const ScSingleRefData& r, OpCode e = ocPush ) :
-                                    FormulaToken( formula::svSingleRef, e ), aSingleRef( r ) {}
+                                ScSingleRefToken( ScSheetLimits& rLimits, const ScSingleRefData& r, OpCode e = ocPush ) :
+                                    FormulaToken( formula::svSingleRef, e ), mrSheetLimits(rLimits), aSingleRef( r ) {}
     virtual const ScSingleRefData*    GetSingleRef() const override;
     virtual ScSingleRefData*      GetSingleRef() override;
     virtual bool                TextEqual( const formula::FormulaToken& rToken ) const override;
@@ -74,10 +75,11 @@ public:
 class ScDoubleRefToken final : public formula::FormulaToken
 {
 private:
+    ScSheetLimits&    mrSheetLimits; // don't use rtl::Reference to avoid ref-counting traffic
     ScComplexRefData  aDoubleRef;
 public:
-                                ScDoubleRefToken( ScSheetLimits&, const ScComplexRefData& r, OpCode e = ocPush  ) :
-                                    FormulaToken( formula::svDoubleRef, e ), aDoubleRef( r ) {}
+                                ScDoubleRefToken( ScSheetLimits& rLimits, const ScComplexRefData& r, OpCode e = ocPush  ) :
+                                    FormulaToken( formula::svDoubleRef, e ), mrSheetLimits(rLimits), aDoubleRef( r ) {}
     virtual const ScSingleRefData*    GetSingleRef() const override;
     virtual ScSingleRefData*      GetSingleRef() override;
     virtual const ScComplexRefData* GetDoubleRef() const override;
diff --git a/sc/qa/unit/mark_test.cxx b/sc/qa/unit/mark_test.cxx
index 6cd63a02de6b..8177d4444170 100644
--- a/sc/qa/unit/mark_test.cxx
+++ b/sc/qa/unit/mark_test.cxx
@@ -165,22 +165,22 @@ void Test::testSimpleMark( const ScRange& rRange, const ScRange& rSelectionCover
     SCCOL nOutCol2 = rRange.aStart.Col() - 1;
 
     CPPUNIT_ASSERT( aMark.IsCellMarked( nMidCol, nMidRow ) );
-    if ( ValidCol( nOutCol1 ) && ValidRow( nOutRow1 ) )
+    if ( ValidCol( nOutCol1, MAXCOL ) && ValidRow( nOutRow1, MAXROW ) )
         CPPUNIT_ASSERT( !aMark.IsCellMarked( nOutCol1, nOutRow1 ) );
 
-    if ( ValidCol( nOutCol2 ) && ValidRow( nOutRow2 ) )
+    if ( ValidCol( nOutCol2, MAXCOL ) && ValidRow( nOutRow2, MAXROW ) )
         CPPUNIT_ASSERT( !aMark.IsCellMarked( nOutCol2, nOutRow2 ) );
 
-    if ( ValidRow( nOutRow1 ) )
+    if ( ValidRow( nOutRow1, MAXROW ) )
         CPPUNIT_ASSERT( !aMark.IsCellMarked( nMidCol, nOutRow1 ) );
 
-    if ( ValidCol( nOutCol1 ) )
+    if ( ValidCol( nOutCol1, MAXCOL ) )
         CPPUNIT_ASSERT( !aMark.IsCellMarked( nOutCol1, nMidRow ) );
 
-    if ( ValidRow( nOutRow2 ) )
+    if ( ValidRow( nOutRow2, MAXROW ) )
         CPPUNIT_ASSERT( !aMark.IsCellMarked( nMidCol, nOutRow2 ) );
 
-    if ( ValidCol( nOutCol2 ) )
+    if ( ValidCol( nOutCol2, MAXCOL ) )
         CPPUNIT_ASSERT( !aMark.IsCellMarked( nOutCol2, nMidRow ) );
 
     if ( rRange.aStart.Row() == 0 && rRange.aEnd.Row() == MAXROW )
diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx
index e8c643f4d5ba..1b62cc1eecb4 100644
--- a/sc/qa/unit/ucalc.cxx
+++ b/sc/qa/unit/ucalc.cxx
@@ -4600,7 +4600,7 @@ void Test::testFormulaPosition()
 
 namespace {
 
-bool hasRange(const std::vector<ScTokenRef>& rRefTokens, const ScRange& rRange, const ScAddress& rPos)
+bool hasRange(const ScDocument* pDoc, const std::vector<ScTokenRef>& rRefTokens, const ScRange& rRange, const ScAddress& rPos)
 {
     for (const ScTokenRef& p : rRefTokens)
     {
@@ -4615,7 +4615,7 @@ bool hasRange(const std::vector<ScTokenRef>& rRefTokens, const ScRange& rRange,
                 if (rRange.aStart != rRange.aEnd)
                     break;
 
-                ScAddress aThis = aData.toAbs(rPos);
+                ScAddress aThis = aData.toAbs(pDoc, rPos);
                 if (aThis == rRange.aStart)
                     return true;
             }
@@ -4623,7 +4623,7 @@ bool hasRange(const std::vector<ScTokenRef>& rRefTokens, const ScRange& rRange,
             case formula::svDoubleRef:
             {
                 ScComplexRefData aData = *p->GetDoubleRef();
-                ScRange aThis = aData.toAbs(rPos);
+                ScRange aThis = aData.toAbs(pDoc, rPos);
                 if (aThis == rRange)
                     return true;
             }
@@ -4656,9 +4656,9 @@ void Test::testJumpToPrecedentsDependents()
         ScRangeList aRange(aC1);
         rDocFunc.DetectiveCollectAllPreds(aRange, aRefTokens);
         CPPUNIT_ASSERT_MESSAGE("A1:A2 should be a precedent of C1.",
-                               hasRange(aRefTokens, ScRange(0, 0, 0, 0, 1, 0), aC1));
+                               hasRange(m_pDoc, aRefTokens, ScRange(0, 0, 0, 0, 1, 0), aC1));
         CPPUNIT_ASSERT_MESSAGE("B3 should be a precedent of C1.",
-                               hasRange(aRefTokens, ScRange(1, 2, 0), aC1));
+                               hasRange(m_pDoc, aRefTokens, ScRange(1, 2, 0), aC1));
     }
 
     {
@@ -4669,7 +4669,7 @@ void Test::testJumpToPrecedentsDependents()
         CPPUNIT_ASSERT_EQUAL_MESSAGE("there should only be one reference token.",
                                static_cast<size_t>(1), aRefTokens.size());
         CPPUNIT_ASSERT_MESSAGE("A1 should be a precedent of C1.",
-                               hasRange(aRefTokens, ScRange(0, 0, 0), aC2));
+                               hasRange(m_pDoc, aRefTokens, ScRange(0, 0, 0), aC2));
     }
 
     {
@@ -4678,7 +4678,7 @@ void Test::testJumpToPrecedentsDependents()
         ScRangeList aRange(aA1);
         rDocFunc.DetectiveCollectAllSuccs(aRange, aRefTokens);
         CPPUNIT_ASSERT_MESSAGE("C1:C2 should be the only dependent of A1.",
-                               aRefTokens.size() == 1 && hasRange(aRefTokens, ScRange(2, 0, 0, 2, 1, 0), aA1));
+                               aRefTokens.size() == 1 && hasRange(m_pDoc, aRefTokens, ScRange(2, 0, 0, 2, 1, 0), aA1));
     }
 
     m_pDoc->DeleteTab(0);
diff --git a/sc/qa/unit/ucalc_formula.cxx b/sc/qa/unit/ucalc_formula.cxx
index 5c93d3083111..6219a65d35c7 100644
--- a/sc/qa/unit/ucalc_formula.cxx
+++ b/sc/qa/unit/ucalc_formula.cxx
@@ -946,6 +946,8 @@ void Test::testFormulaTokenEquality()
 
 void Test::testFormulaRefData()
 {
+    std::unique_ptr<ScDocument> pDoc = std::make_unique<ScDocument>();
+
     ScAddress aAddr(4,5,3), aPos(2,2,2);
     ScSingleRefData aRef;
     aRef.InitAddress(aAddr);
@@ -957,7 +959,7 @@ void Test::testFormulaRefData()
     aRef.SetRowRel(true);
     aRef.SetColRel(true);
     aRef.SetTabRel(true);
-    aRef.SetAddress(aAddr, aPos);
+    aRef.SetAddress(pDoc->GetSheetLimits(), aAddr, aPos);
     CPPUNIT_ASSERT_EQUAL(SCCOL(2), aRef.Col());
     CPPUNIT_ASSERT_EQUAL(SCROW(3), aRef.Row());
     CPPUNIT_ASSERT_EQUAL(SCTAB(1), aRef.Tab());
@@ -969,15 +971,15 @@ void Test::testFormulaRefData()
 
     aRef.InitAddress(ScAddress(6,5,0));
 
-    aDoubleRef.Extend(aRef, ScAddress());
-    ScRange aTest = aDoubleRef.toAbs(ScAddress());
+    aDoubleRef.Extend(pDoc->GetSheetLimits(), aRef, ScAddress());
+    ScRange aTest = aDoubleRef.toAbs(pDoc.get(), ScAddress());
     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong start position of extended range.", ScAddress(2,2,0), aTest.aStart);
     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong end position of extended range.", ScAddress(6,5,0), aTest.aEnd);
 
     ScComplexRefData aDoubleRef2;
-    aDoubleRef2.InitRangeRel(ScRange(1,2,0,8,6,0), ScAddress(5,5,0));
-    aDoubleRef.Extend(aDoubleRef2, ScAddress(5,5,0));
-    aTest = aDoubleRef.toAbs(ScAddress(5,5,0));
+    aDoubleRef2.InitRangeRel(pDoc.get(), ScRange(1,2,0,8,6,0), ScAddress(5,5,0));
+    aDoubleRef.Extend(pDoc->GetSheetLimits(), aDoubleRef2, ScAddress(5,5,0));
+    aTest = aDoubleRef.toAbs(pDoc.get(), ScAddress(5,5,0));
 
     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong start position of extended range.", ScAddress(1,2,0), aTest.aStart);
     CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong end position of extended range.", ScAddress(8,6,0), aTest.aEnd);
@@ -1198,7 +1200,7 @@ void Test::testFormulaCompilerImplicitIntersection2Param()
             CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong type of token(third argument to SUMIF)", svDoubleRef, ppTokens[2]->GetType());
 
             ScComplexRefData aSumRangeData = *ppTokens[2]->GetDoubleRef();
-            ScRange aSumRange = aSumRangeData.toAbs(rCase.aCellAddress);
+            ScRange aSumRange = aSumRangeData.toAbs(m_pDoc, rCase.aCellAddress);
             CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong sum-range in RPN array", rCase.aSumRange, aSumRange);
 
             CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong IsRel type for start column address in sum-range", rCase.bStartColRel, aSumRangeData.Ref1.IsColRel());
@@ -1379,7 +1381,7 @@ void Test::testFormulaCompilerImplicitIntersection1ParamWithChange()
             CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong type of RPN token(argument to COS)", svSingleRef, ppRPNTokens[0]->GetType());
 
             ScSingleRefData aArgAddrRPN = *ppRPNTokens[0]->GetSingleRef();
-            ScAddress aArgAddrActual = aArgAddrRPN.toAbs(rCase.aCellAddress);
+            ScAddress aArgAddrActual = aArgAddrRPN.toAbs(m_pDoc, rCase.aCellAddress);
             CPPUNIT_ASSERT_EQUAL_MESSAGE("Computed implicit intersection singleref is wrong", rCase.aArgAddr, aArgAddrActual);
         }
     }
diff --git a/sc/qa/unit/ucalc_sharedformula.cxx b/sc/qa/unit/ucalc_sharedformula.cxx
index 45e4994481ed..0c3f06d46602 100644
--- a/sc/qa/unit/ucalc_sharedformula.cxx
+++ b/sc/qa/unit/ucalc_sharedformula.cxx
@@ -1048,7 +1048,7 @@ void Test::testSharedFormulasDeleteColumns()
     CPPUNIT_ASSERT_EQUAL(svSingleRef, pToken->GetType());
     const ScSingleRefData* pSRef = pToken->GetSingleRef();
     CPPUNIT_ASSERT(pSRef->IsColDeleted());
-    CPPUNIT_ASSERT_EQUAL(static_cast<SCROW>(0), pSRef->toAbs(ScAddress(1,0,0)).Row());
+    CPPUNIT_ASSERT_EQUAL(static_cast<SCROW>(0), pSRef->toAbs(m_pDoc, ScAddress(1,0,0)).Row());
 
     // The formula string should show #REF! in lieu of the column position (only for Calc A1 syntax).
     sc::CompileFormulaContext aCFCxt(m_pDoc, FormulaGrammar::GRAM_ENGLISH);
diff --git a/sc/source/core/data/colorscale.cxx b/sc/source/core/data/colorscale.cxx
index e282ca78c5a1..2da00e1f03d6 100644
--- a/sc/source/core/data/colorscale.cxx
+++ b/sc/source/core/data/colorscale.cxx
@@ -54,8 +54,8 @@ void ScFormulaListener::startListening(const ScTokenArray* pArr, const ScRange&
         {
             case formula::svSingleRef:
             {
-                ScAddress aCell = t->GetSingleRef()->toAbs(rRange.aStart);
-                ScAddress aCell2 = t->GetSingleRef()->toAbs(rRange.aEnd);
+                ScAddress aCell = t->GetSingleRef()->toAbs(mpDoc, rRange.aStart);
+                ScAddress aCell2 = t->GetSingleRef()->toAbs(mpDoc, rRange.aEnd);
                 ScRange aRange(aCell, aCell2);
                 if (aRange.IsValid())
                     mpDoc->StartListeningArea(aRange, false, this);
@@ -65,10 +65,10 @@ void ScFormulaListener::startListening(const ScTokenArray* pArr, const ScRange&
             {
                 const ScSingleRefData& rRef1 = *t->GetSingleRef();
                 const ScSingleRefData& rRef2 = *t->GetSingleRef2();
-                ScAddress aCell1 = rRef1.toAbs(rRange.aStart);
-                ScAddress aCell2 = rRef2.toAbs(rRange.aStart);
-                ScAddress aCell3 = rRef1.toAbs(rRange.aEnd);
-                ScAddress aCell4 = rRef2.toAbs(rRange.aEnd);
+                ScAddress aCell1 = rRef1.toAbs(mpDoc, rRange.aStart);
+                ScAddress aCell2 = rRef2.toAbs(mpDoc, rRange.aStart);
+                ScAddress aCell3 = rRef1.toAbs(mpDoc, rRange.aEnd);
+                ScAddress aCell4 = rRef2.toAbs(mpDoc, rRange.aEnd);
                 ScRange aRange1(aCell1, aCell3);
                 ScRange aRange2(aCell2, aCell4);
                 aRange1.ExtendTo(aRange2);
diff --git a/sc/source/core/data/column.cxx b/sc/source/core/data/column.cxx
index fcc0e00de4bd..3cfe18891f63 100644
--- a/sc/source/core/data/column.cxx
+++ b/sc/source/core/data/column.cxx
@@ -2501,7 +2501,7 @@ bool ScColumn::UpdateReference( sc::RefUpdateContext& rCxt, ScDocument* pUndoDoc
     }
 
     // Do the actual splitting.
-    const bool bSplit = sc::SharedFormulaUtil::splitFormulaCellGroups(maCells, aBounds);
+    const bool bSplit = sc::SharedFormulaUtil::splitFormulaCellGroups(GetDoc(), maCells, aBounds);
 
     // Collect all formula groups.
     std::vector<sc::FormulaGroupEntry> aGroups = GetFormulaGroupEntries();
diff --git a/sc/source/core/data/column4.cxx b/sc/source/core/data/column4.cxx
index b684a87c84d1..3431f21ae80d 100644
--- a/sc/source/core/data/column4.cxx
+++ b/sc/source/core/data/column4.cxx
@@ -440,7 +440,7 @@ void ScColumn::ConvertFormulaToValue(
         aBounds.push_back(nRow2+1);
 
     // Split formula cell groups at top and bottom boundaries (if applicable).
-    sc::SharedFormulaUtil::splitFormulaCellGroups(maCells, aBounds);
+    sc::SharedFormulaUtil::splitFormulaCellGroups(GetDoc(), maCells, aBounds);
 
     // Parse all formulas within the range and store their results into temporary storage.
     ConvertFormulaToValueHandler aFunc;
@@ -501,7 +501,7 @@ void ScColumn::SwapNonEmpty(
         aBounds.push_back(rRange.aEnd.Row()+1);
 
     // Split formula cell groups at top and bottom boundaries (if applicable).
-    sc::SharedFormulaUtil::splitFormulaCellGroups(maCells, aBounds);
+    sc::SharedFormulaUtil::splitFormulaCellGroups(GetDoc(), maCells, aBounds);
     std::vector<sc::CellValueSpan> aSpans = rValues.getNonEmptySpans(nTab, nCol);
 
     // Detach formula cells within the spans (if any).
@@ -1197,13 +1197,13 @@ void ScColumn::SplitFormulaGroupByRelativeRef( const ScRange& rBoundRange )
     aBounds.push_back(rBoundRange.aStart.Row());
     if (rBoundRange.aEnd.Row() < GetDoc()->MaxRow())
         aBounds.push_back(rBoundRange.aEnd.Row()+1);
-    sc::SharedFormulaUtil::splitFormulaCellGroups(maCells, aBounds);
+    sc::SharedFormulaUtil::splitFormulaCellGroups(GetDoc(), maCells, aBounds);
 
     RelativeRefBoundChecker aFunc(rBoundRange);
     sc::ProcessFormula(
         maCells.begin(), maCells, rBoundRange.aStart.Row(), rBoundRange.aEnd.Row(), aFunc);
     aFunc.swapBounds(aBounds);
-    sc::SharedFormulaUtil::splitFormulaCellGroups(maCells, aBounds);
+    sc::SharedFormulaUtil::splitFormulaCellGroups(GetDoc(), maCells, aBounds);
 }
 
 namespace {
diff --git a/sc/source/core/data/conditio.cxx b/sc/source/core/data/conditio.cxx
index 72110495ba91..6a150a82c60c 100644
--- a/sc/source/core/data/conditio.cxx
+++ b/sc/source/core/data/conditio.cxx
@@ -1336,7 +1336,7 @@ ScAddress ScConditionEntry::GetValidSrcPos() const
             for ( auto t: pFormula->References() )
             {
                 ScSingleRefData& rRef1 = *t->GetSingleRef();
-                ScAddress aAbs = rRef1.toAbs(aSrcPos);
+                ScAddress aAbs = rRef1.toAbs(mpDoc, aSrcPos);
                 if (!rRef1.IsTabDeleted())
                 {
                     if (aAbs.Tab() < nMinTab)
@@ -1347,7 +1347,7 @@ ScAddress ScConditionEntry::GetValidSrcPos() const
                 if ( t->GetType() == svDoubleRef )
                 {
                     ScSingleRefData& rRef2 = t->GetDoubleRef()->Ref2;
-                    aAbs = rRef2.toAbs(aSrcPos);
+                    aAbs = rRef2.toAbs(mpDoc, aSrcPos);
                     if (!rRef2.IsTabDeleted())
                     {
                         if (aAbs.Tab() < nMinTab)
diff --git a/sc/source/core/data/documen4.cxx b/sc/source/core/data/documen4.cxx
index 22c0ecfae717..871d0cba34cc 100644
--- a/sc/source/core/data/documen4.cxx
+++ b/sc/source/core/data/documen4.cxx
@@ -311,7 +311,7 @@ void ScDocument::InsertMatrixFormula(SCCOL nCol1, SCROW nRow1,
     aRefData.SetColRel( true );
     aRefData.SetRowRel( true );
     aRefData.SetTabRel( true );
-    aRefData.SetAddress(aBasePos, aBasePos);
+    aRefData.SetAddress(GetSheetLimits(), aBasePos, aBasePos);
 
     ScTokenArray aArr(this); // consists only of one single reference token.
     formula::FormulaToken* t = aArr.AddMatrixSingleReference(aRefData);
@@ -342,7 +342,7 @@ void ScDocument::InsertMatrixFormula(SCCOL nCol1, SCROW nRow1,
                 // 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(aBasePos, aPos);
+                aRefData.SetAddress(GetSheetLimits(), aBasePos, aPos);
                 *t->GetSingleRef() = aRefData;
                 std::unique_ptr<ScTokenArray> pTokArr(aArr.Clone());
                 pCell = new ScFormulaCell(this, aPos, *pTokArr, eGram, ScMatrixMode::Reference);
@@ -453,7 +453,7 @@ void ScDocument::InsertTableOp(const ScTabOpParam& rParam,  // multiple (repeate
 
 namespace {
 
-bool setCacheTableReferenced(formula::FormulaToken& rToken, ScExternalRefManager& rRefMgr, const ScAddress& rPos)
+bool setCacheTableReferenced(const ScDocument* pDoc, formula::FormulaToken& rToken, ScExternalRefManager& rRefMgr, const ScAddress& rPos)
 {
     switch (rToken.GetType())
     {
@@ -463,7 +463,7 @@ bool setCacheTableReferenced(formula::FormulaToken& rToken, ScExternalRefManager
         case svExternalDoubleRef:
         {
             const ScComplexRefData& rRef = *rToken.GetDoubleRef();
-            ScRange aAbs = rRef.toAbs(rPos);
+            ScRange aAbs = rRef.toAbs(pDoc, rPos);
             size_t nSheets = aAbs.aEnd.Tab() - aAbs.aStart.Tab() + 1;
             return rRefMgr.setCacheTableReferenced(
                     rToken.GetIndex(), rToken.GetString().getString(), nSheets);
@@ -498,7 +498,7 @@ bool ScDocument::MarkUsedExternalReferences( const ScTokenArray& rArr, const ScA
             if (!pRefMgr)
                 pRefMgr = GetExternalRefManager();
 
-            bAllMarked = setCacheTableReferenced(*t, *pRefMgr, rPos);
+            bAllMarked = setCacheTableReferenced(this, *t, *pRefMgr, rPos);
         }
         else if (t->GetType() == svIndex)
         {
@@ -518,7 +518,7 @@ bool ScDocument::MarkUsedExternalReferences( const ScTokenArray& rArr, const ScA
                 if (!pRefMgr)
                     pRefMgr = GetExternalRefManager();
 
-                bAllMarked = setCacheTableReferenced(*t, *pRefMgr, rPos);
+                bAllMarked = setCacheTableReferenced(this, *t, *pRefMgr, rPos);
             }
         }
     }
diff --git a/sc/source/core/data/documentimport.cxx b/sc/source/core/data/documentimport.cxx
index e73a6c3831f1..46d1638fbe48 100644
--- a/sc/source/core/data/documentimport.cxx
+++ b/sc/source/core/data/documentimport.cxx
@@ -407,7 +407,7 @@ void ScDocumentImport::setMatrixCells(
     aRefData.SetColRel(true);
     aRefData.SetRowRel(true);
     aRefData.SetTabRel(true);
-    aRefData.SetAddress(rBasePos, rBasePos);
+    aRefData.SetAddress(mpImpl->mrDoc.GetSheetLimits(), rBasePos, rBasePos);
 
     ScTokenArray aArr(&mpImpl->mrDoc); // consists only of one single reference token.
     formula::FormulaToken* t = aArr.AddMatrixSingleReference(aRefData);
@@ -418,7 +418,7 @@ void ScDocumentImport::setMatrixCells(
         // Token array must be cloned so that each formula cell receives its own copy.
         aPos.SetRow(nRow);
         // Reference in each cell must point to the origin cell relative to the current cell.
-        aRefData.SetAddress(rBasePos, aPos);
+        aRefData.SetAddress(mpImpl->mrDoc.GetSheetLimits(), rBasePos, aPos);
         *t->GetSingleRef() = aRefData;
         std::unique_ptr<ScTokenArray> pTokArr(aArr.Clone());
         pCell = new ScFormulaCell(&mpImpl->mrDoc, aPos, *pTokArr, eGram, ScMatrixMode::Reference);
@@ -438,7 +438,7 @@ void ScDocumentImport::setMatrixCells(
         for (SCROW nRow = rRange.aStart.Row(); nRow <= rRange.aEnd.Row(); ++nRow)
         {
             aPos.SetRow(nRow);
-            aRefData.SetAddress(rBasePos, aPos);
+            aRefData.SetAddress(mpImpl->mrDoc.GetSheetLimits(), rBasePos, aPos);
             *t->GetSingleRef() = aRefData;
             std::unique_ptr<ScTokenArray> pTokArr(aArr.Clone());
             pCell = new ScFormulaCell(&mpImpl->mrDoc, aPos, *pTokArr, eGram, ScMatrixMode::Reference);
diff --git a/sc/source/core/data/formulacell.cxx b/sc/source/core/data/formulacell.cxx
index 693eeb6c678b..5bb89a1ff421 100644
--- a/sc/source/core/data/formulacell.cxx
+++ b/sc/source/core/data/formulacell.cxx
@@ -228,42 +228,44 @@ namespace {
 // Allow for a year's calendar (366).
 const sal_uInt16 MAXRECURSION = 400;
 
-typedef SCCOLROW(*DimensionSelector)(const ScAddress&, const ScSingleRefData&);
+typedef SCCOLROW(*DimensionSelector)(const ScDocument*, const ScAddress&, const ScSingleRefData&);
 
-SCCOLROW lcl_GetCol(const ScAddress& rPos, const ScSingleRefData& rData)
+SCCOLROW lcl_GetCol(const ScDocument* pDoc, const ScAddress& rPos, const ScSingleRefData& rData)
 {
-    return rData.toAbs(rPos).Col();
+    return rData.toAbs(pDoc, rPos).Col();
 }
 
-SCCOLROW lcl_GetRow(const ScAddress& rPos, const ScSingleRefData& rData)
+SCCOLROW lcl_GetRow(const ScDocument* pDoc, const ScAddress& rPos, const ScSingleRefData& rData)
 {
-    return rData.toAbs(rPos).Row();
+    return rData.toAbs(pDoc, rPos).Row();
 }
 
-SCCOLROW lcl_GetTab(const ScAddress& rPos, const ScSingleRefData& rData)
+SCCOLROW lcl_GetTab(const ScDocument* pDoc, const ScAddress& rPos, const ScSingleRefData& rData)
 {
-    return rData.toAbs(rPos).Tab();
+    return rData.toAbs(pDoc, rPos).Tab();
 }
 
 /** Check if both references span the same range in selected dimension.
  */
 bool
 lcl_checkRangeDimension(
+    const ScDocument* pDoc,
     const ScAddress& rPos, const SingleDoubleRefProvider& rRef1, const SingleDoubleRefProvider& rRef2,
     const DimensionSelector aWhich)
 {
-    return aWhich(rPos, rRef1.Ref1) == aWhich(rPos, rRef2.Ref1) &&
-        aWhich(rPos, rRef1.Ref2) == aWhich(rPos, rRef2.Ref2);
+    return aWhich(pDoc, rPos, rRef1.Ref1) == aWhich(pDoc, rPos, rRef2.Ref1) &&
+        aWhich(pDoc, rPos, rRef1.Ref2) == aWhich(pDoc, rPos, rRef2.Ref2);
 }
 
 bool
 lcl_checkRangeDimensions(
+    const ScDocument* pDoc,
     const ScAddress& rPos, const SingleDoubleRefProvider& rRef1, const SingleDoubleRefProvider& rRef2,
     bool& bCol, bool& bRow, bool& bTab)
 {
-    const bool bSameCols(lcl_checkRangeDimension(rPos, rRef1, rRef2, lcl_GetCol));
-    const bool bSameRows(lcl_checkRangeDimension(rPos, rRef1, rRef2, lcl_GetRow));
-    const bool bSameTabs(lcl_checkRangeDimension(rPos, rRef1, rRef2, lcl_GetTab));
+    const bool bSameCols(lcl_checkRangeDimension(pDoc, rPos, rRef1, rRef2, lcl_GetCol));
+    const bool bSameRows(lcl_checkRangeDimension(pDoc, rPos, rRef1, rRef2, lcl_GetRow));
+    const bool bSameTabs(lcl_checkRangeDimension(pDoc, rPos, rRef1, rRef2, lcl_GetTab));
 
     // Test if exactly two dimensions are equal
     if (int(bSameCols) + int(bSameRows) + int(bSameTabs) == 2)
@@ -281,7 +283,7 @@ lcl_checkRangeDimensions(
  */
 bool
 lcl_checkRangeDimensions(
-    const ScAddress& rPos,
+    const ScDocument* pDoc, const ScAddress& rPos,
     const std::vector<formula::FormulaToken*>::const_iterator& rBegin,
     const std::vector<formula::FormulaToken*>::const_iterator& rEnd,
     bool& bCol, bool& bRow, bool& bTab)
@@ -292,7 +294,7 @@ lcl_checkRangeDimensions(
     bool bOk(false);
     {
         const SingleDoubleRefProvider aRefCur(**aCur);
-        bOk = lcl_checkRangeDimensions(rPos, aRef, aRefCur, bCol, bRow, bTab);
+        bOk = lcl_checkRangeDimensions(pDoc, rPos, aRef, aRefCur, bCol, bRow, bTab);
     }
     while (bOk && aCur != rEnd)
     {
@@ -300,7 +302,7 @@ lcl_checkRangeDimensions(
         bool bColTmp(false);
         bool bRowTmp(false);
         bool bTabTmp(false);
-        bOk = lcl_checkRangeDimensions(rPos, aRef, aRefCur, bColTmp, bRowTmp, bTabTmp);
+        bOk = lcl_checkRangeDimensions(pDoc, rPos, aRef, aRefCur, bColTmp, bRowTmp, bTabTmp);
         bOk = bOk && (bCol == bColTmp && bRow == bRowTmp && bTab == bTabTmp);
         ++aCur;
     }
@@ -310,17 +312,18 @@ lcl_checkRangeDimensions(
 
 class LessByReference
 {
+    const ScDocument* mpDoc;
     ScAddress const maPos;
     DimensionSelector const maFunc;
 public:
-    LessByReference(const ScAddress& rPos, const DimensionSelector& rFunc) :
-        maPos(rPos), maFunc(rFunc) {}
+    LessByReference(const ScDocument* pDoc, const ScAddress& rPos, const DimensionSelector& rFunc) :
+        mpDoc(pDoc), maPos(rPos), maFunc(rFunc) {}
 
     bool operator() (const formula::FormulaToken* pRef1, const formula::FormulaToken* pRef2)
     {
         const SingleDoubleRefProvider aRef1(*pRef1);
         const SingleDoubleRefProvider aRef2(*pRef2);
-        return maFunc(maPos, aRef1.Ref1) < maFunc(maPos, aRef2.Ref1);
+        return maFunc(mpDoc, maPos, aRef1.Ref1) < maFunc(mpDoc, maPos, aRef2.Ref1);
     }
 };
 
@@ -331,22 +334,24 @@ public:
  */
 class AdjacentByReference
 {
+    const ScDocument* mpDoc;
     ScAddress const maPos;
     DimensionSelector const maFunc;
 public:
-    AdjacentByReference(const ScAddress& rPos, DimensionSelector aFunc) :
-        maPos(rPos), maFunc(aFunc) {}
+    AdjacentByReference(const ScDocument* pDoc, const ScAddress& rPos, DimensionSelector aFunc) :
+        mpDoc(pDoc), maPos(rPos), maFunc(aFunc) {}
 
     bool operator() (const formula::FormulaToken* p1, const formula::FormulaToken* p2)
     {
         const SingleDoubleRefProvider aRef1(*p1);
         const SingleDoubleRefProvider aRef2(*p2);
-        return maFunc(maPos, aRef2.Ref1) - maFunc(maPos, aRef1.Ref2) == 1;
+        return maFunc(mpDoc, maPos, aRef2.Ref1) - maFunc(mpDoc, maPos, aRef1.Ref2) == 1;
     }
 };
 
 bool
 lcl_checkIfAdjacent(
+    const ScDocument* pDoc,
     const ScAddress& rPos, const std::vector<formula::FormulaToken*>& rReferences, const DimensionSelector aWhich)
 {
     auto aBegin(rReferences.cbegin());
@@ -354,36 +359,38 @@ lcl_checkIfAdjacent(
     auto aBegin1(aBegin);
     ++aBegin1;
     --aEnd;
-    return std::equal(aBegin, aEnd, aBegin1, AdjacentByReference(rPos, aWhich));
+    return std::equal(aBegin, aEnd, aBegin1, AdjacentByReference(pDoc, rPos, aWhich));
 }
 
 void
 lcl_fillRangeFromRefList(
+    const ScDocument* pDoc,
     const ScAddress& aPos, const std::vector<formula::FormulaToken*>& rReferences, ScRange& rRange)
 {
     const ScSingleRefData aStart(
             SingleDoubleRefProvider(*rReferences.front()).Ref1);
-    rRange.aStart = aStart.toAbs(aPos);
+    rRange.aStart = aStart.toAbs(pDoc, aPos);
     const ScSingleRefData aEnd(
             SingleDoubleRefProvider(*rReferences.back()).Ref2);
-    rRange.aEnd = aEnd.toAbs(aPos);
+    rRange.aEnd = aEnd.toAbs(pDoc, aPos);
 }
 
 bool
 lcl_refListFormsOneRange(
+        const ScDocument* pDoc,
         const ScAddress& rPos, std::vector<formula::FormulaToken*>& rReferences,
         ScRange& rRange)
 {
     if (rReferences.size() == 1)
     {
-        lcl_fillRangeFromRefList(rPos, rReferences, rRange);
+        lcl_fillRangeFromRefList(pDoc, rPos, rReferences, rRange);
         return true;
     }
 
     bool bCell(false);
     bool bRow(false);
     bool bTab(false);
-    if (lcl_checkRangeDimensions(rPos, rReferences.begin(), rReferences.end(), bCell, bRow, bTab))
+    if (lcl_checkRangeDimensions(pDoc, rPos, rReferences.begin(), rReferences.end(), bCell, bRow, bTab))
     {
         DimensionSelector aWhich;
         if (bCell)
@@ -405,10 +412,10 @@ lcl_refListFormsOneRange(
         }
 
         // Sort the references by start of range
-        std::sort(rReferences.begin(), rReferences.end(), LessByReference(rPos, aWhich));
-        if (lcl_checkIfAdjacent(rPos, rReferences, aWhich))
+        std::sort(rReferences.begin(), rReferences.end(), LessByReference(pDoc, rPos, aWhich));
+        if (lcl_checkIfAdjacent(pDoc, rPos, rReferences, aWhich))
         {
-            lcl_fillRangeFromRefList(rPos, rReferences, rRange);
+            lcl_fillRangeFromRefList(pDoc, rPos, rReferences, rRange);
             return true;
         }
     }
@@ -982,7 +989,7 @@ void ScFormulaCell::GetFormula( OUStringBuffer& rBuffer,
              * Can we live without in all cases? */
             ScFormulaCell* pCell = nullptr;
             ScSingleRefData& rRef = *p->GetSingleRef();
-            ScAddress aAbs = rRef.toAbs(aPos);
+            ScAddress aAbs = rRef.toAbs(pDocument, aPos);
             if (pDocument->ValidAddress(aAbs))
                 pCell = pDocument->GetFormulaCell(aAbs);
 
@@ -1048,7 +1055,7 @@ OUString ScFormulaCell::GetFormula( sc::CompileFormulaContext& rCxt, const ScInt
              * Can we live without in all cases? */
             ScFormulaCell* pCell = nullptr;
             ScSingleRefData& rRef = *p->GetSingleRef();
-            ScAddress aAbs = rRef.toAbs(aPos);
+            ScAddress aAbs = rRef.toAbs(pDocument, aPos);
             if (pDocument->ValidAddress(aAbs))
                 pCell = pDocument->GetFormulaCell(aAbs);
 
@@ -2814,7 +2821,7 @@ bool ScFormulaCell::GetMatrixOrigin( const ScDocument* pDoc, ScAddress& rPos ) c
             if( t )
             {
                 ScSingleRefData& rRef = *t->GetSingleRef();
-                ScAddress aAbs = rRef.toAbs(aPos);
+                ScAddress aAbs = rRef.toAbs(pDoc, aPos);
                 if (pDoc->ValidAddress(aAbs))
                 {
                     rPos = aAbs;
@@ -2998,8 +3005,8 @@ bool ScFormulaCell::HasOneReference( ScRange& r ) const
     if( p && !aIter.GetNextReferenceRPN() )        // only one!
     {
         SingleDoubleRefProvider aProv( *p );
-        r.aStart = aProv.Ref1.toAbs(aPos);
-        r.aEnd = aProv.Ref2.toAbs(aPos);
+        r.aStart = aProv.Ref1.toAbs(pDocument, aPos);
+        r.aEnd = aProv.Ref2.toAbs(pDocument, aPos);
         return true;
     }
     else
@@ -3053,7 +3060,7 @@ ScFormulaCell::HasRefListExpressibleAsOneReference(ScRange& rRange) const
         if (pFunction && !aIter.GetNextReferenceRPN()
                 && (pFunction->GetParamCount() == aReferences.size()))
         {
-            return lcl_refListFormsOneRange(aPos, aReferences, rRange);
+            return lcl_refListFormsOneRange(pDocument, aPos, aReferences, rRange);
         }
     }
     return false;
@@ -3138,7 +3145,7 @@ bool checkCompileColRowName(
                 ScSingleRefData& rRef = *t->GetSingleRef();
                 if (rCxt.mnRowDelta > 0 && rRef.IsColRel())
                 {   // ColName
-                    ScAddress aAdr = rRef.toAbs(aPos);
+                    ScAddress aAdr = rRef.toAbs(&rDoc, aPos);
                     ScRangePair* pR = pColList->Find( aAdr );
                     if ( pR )
                     {   // defined
@@ -3153,7 +3160,7 @@ bool checkCompileColRowName(
                 }
                 if (rCxt.mnColDelta > 0 && rRef.IsRowRel())
                 {   // RowName
-                    ScAddress aAdr = rRef.toAbs(aPos);
+                    ScAddress aAdr = rRef.toAbs(&rDoc, aPos);
                     ScRangePair* pR = pRowList->Find( aAdr );
                     if ( pR )
                     {   // defined
@@ -3181,7 +3188,7 @@ bool checkCompileColRowName(
             for (; t; t = aIter.GetNextColRowName())
             {
                 const ScSingleRefData& rRef = *t->GetSingleRef();
-                ScAddress aAbs = rRef.toAbs(aPos);
+                ScAddress aAbs = rRef.toAbs(&rDoc, aPos);
                 if (rDoc.ValidAddress(aAbs))
                 {
                     if (rCxt.maRange.In(aAbs))
@@ -3804,11 +3811,11 @@ void ScFormulaCell::UpdateTranspose( const ScRange& rSource, const ScAddress& rD
         {
             SingleDoubleRefModifier aMod(*t);
             ScComplexRefData& rRef = aMod.Ref();
-            ScRange aAbs = rRef.toAbs(aOldPos);
+            ScRange aAbs = rRef.toAbs(pDocument, aOldPos);
             bool bMod = (ScRefUpdate::UpdateTranspose(pDocument, rSource, rDest, aAbs) != UR_NOTHING || bPosChanged);
             if (bMod)
             {
-                rRef.SetRange(aAbs, aPos); // based on the new anchor position.
+                rRef.SetRange(pDocument->GetSheetLimits(), aAbs, aPos); // based on the new anchor position.
                 bRefChanged = true;
             }
         }
@@ -3854,11 +3861,11 @@ void ScFormulaCell::UpdateGrow( const ScRange& rArea, SCCOL nGrowX, SCROW nGrowY
         {
             SingleDoubleRefModifier aMod(*t);
             ScComplexRefData& rRef = aMod.Ref();
-            ScRange aAbs = rRef.toAbs(aPos);
+            ScRange aAbs = rRef.toAbs(pDocument, aPos);
             bool bMod = (ScRefUpdate::UpdateGrow(rArea, nGrowX, nGrowY, aAbs) != UR_NOTHING);
             if (bMod)
             {
-                rRef.SetRange(aAbs, aPos);
+                rRef.SetRange(pDocument->GetSheetLimits(), aAbs, aPos);
                 bRefChanged = true;
             }
         }
@@ -4428,7 +4435,7 @@ struct ScDependantsCalculator
                     ScSingleRefData aRef = *p->GetSingleRef(); // =Sheet1!A1
                     if( aRef.IsDeleted())
                         return false;
-                    ScAddress aRefPos = aRef.toAbs(mrPos);
+                    ScAddress aRefPos = aRef.toAbs(&mrDoc, mrPos);
 
                     if (!mrDoc.TableExists(aRefPos.Tab()))
                         return false; // or true?
@@ -4464,7 +4471,7 @@ struct ScDependantsCalculator
                     ScComplexRefData aRef = *p->GetDoubleRef();
                     if( aRef.IsDeleted())
                         return false;
-                    ScRange aAbs = aRef.toAbs(mrPos);
+                    ScRange aAbs = aRef.toAbs(&mrDoc, mrPos);
 
                     // Multiple sheet
                     if (aRef.Ref1.Tab() != aRef.Ref2.Tab())
@@ -5122,7 +5129,7 @@ bool ScFormulaCell::InterpretInvariantFormulaGroup()
                 case svSingleRef:
                 {
                     ScSingleRefData aRef = *p->GetSingleRef();
-                    ScAddress aRefPos = aRef.toAbs(aPos);
+                    ScAddress aRefPos = aRef.toAbs(pDocument, aPos);
                     formula::FormulaTokenRef pNewToken = pDocument->ResolveStaticReference(aRefPos);
                     if (!pNewToken)
                         return false;
@@ -5133,7 +5140,7 @@ bool ScFormulaCell::InterpretInvariantFormulaGroup()
                 case svDoubleRef:
                 {
                     ScComplexRefData aRef = *p->GetDoubleRef();
-                    ScRange aRefRange = aRef.toAbs(aPos);
+                    ScRange aRefRange = aRef.toAbs(pDocument, aPos);
                     formula::FormulaTokenRef pNewToken = pDocument->ResolveStaticReference(aRefRange);
                     if (!pNewToken)
                         return false;
@@ -5189,8 +5196,8 @@ void startListeningArea(
 {
     const ScSingleRefData& rRef1 = *rToken.GetSingleRef();
     const ScSingleRefData& rRef2 = *rToken.GetSingleRef2();
-    ScAddress aCell1 = rRef1.toAbs(rPos);
-    ScAddress aCell2 = rRef2.toAbs(rPos);
+    ScAddress aCell1 = rRef1.toAbs(&rDoc, rPos);
+    ScAddress aCell2 = rRef2.toAbs(&rDoc, rPos);
     if (aCell1.IsValid() && aCell2.IsValid())
     {
         if (rToken.GetOpCode() == ocColRowNameAuto)
@@ -5236,7 +5243,7 @@ void ScFormulaCell::StartListeningTo( ScDocument* pDoc )
         {
             case svSingleRef:
             {
-                ScAddress aCell =  t->GetSingleRef()->toAbs(aPos);
+                ScAddress aCell =  t->GetSingleRef()->toAbs(pDocument, aPos);
                 if (aCell.IsValid())
                     pDoc->StartListeningCell(aCell, this);
             }
@@ -5279,7 +5286,7 @@ void ScFormulaCell::StartListeningTo( sc::StartListeningContext& rCxt )
         {
             case svSingleRef:
             {
-                ScAddress aCell = t->GetSingleRef()->toAbs(aPos);
+                ScAddress aCell = t->GetSingleRef()->toAbs(pDocument, aPos);
                 if (aCell.IsValid())
                     rDoc.StartListeningCell(rCxt, aCell, *this);
             }
@@ -5301,8 +5308,8 @@ void endListeningArea(
 {
     const ScSingleRefData& rRef1 = *rToken.GetSingleRef();
     const ScSingleRefData& rRef2 = *rToken.GetSingleRef2();
-    ScAddress aCell1 = rRef1.toAbs(rPos);
-    ScAddress aCell2 = rRef2.toAbs(rPos);
+    ScAddress aCell1 = rRef1.toAbs(&rDoc, rPos);
+    ScAddress aCell2 = rRef2.toAbs(&rDoc, rPos);
     if (aCell1.IsValid() && aCell2.IsValid())
     {
         if (rToken.GetOpCode() == ocColRowNameAuto)
@@ -5356,7 +5363,7 @@ void ScFormulaCell::EndListeningTo( ScDocument* pDoc, ScTokenArray* pArr,
         {
             case svSingleRef:
             {
-                ScAddress aCell = t->GetSingleRef()->toAbs(aCellPos);
+                ScAddress aCell = t->GetSingleRef()->toAbs(pDocument, aCellPos);
                 if (aCell.IsValid())
                     pDoc->EndListeningCell(aCell, this);
             }
@@ -5403,7 +5410,7 @@ void ScFormulaCell::EndListeningTo( sc::EndListeningContext& rCxt )
         {
             case svSingleRef:
             {
-                ScAddress aCell = t->GetSingleRef()->toAbs(aCellPos);
+                ScAddress aCell = t->GetSingleRef()->toAbs(pDocument, aCellPos);
                 if (aCell.IsValid())
                     rDoc.EndListeningCell(rCxt, aCell, *this);
             }
diff --git a/sc/source/core/data/formulaiter.cxx b/sc/source/core/data/formulaiter.cxx
index 4f71466888fa..7c27eb133864 100644
--- a/sc/source/core/data/formulaiter.cxx
+++ b/sc/source/core/data/formulaiter.cxx
@@ -36,13 +36,13 @@ ScDetectiveRefIter::ScDetectiveRefIter( const ScDocument* pDoc, ScFormulaCell* p
 static bool lcl_ScDetectiveRefIter_SkipRef( const ScDocument* pDoc, formula::FormulaToken* p, const ScAddress& rPos )
 {
     ScSingleRefData& rRef1 = *p->GetSingleRef();
-    ScAddress aAbs1 = rRef1.toAbs(rPos);
+    ScAddress aAbs1 = rRef1.toAbs(pDoc, rPos);
     if (!pDoc->ValidAddress(aAbs1))
         return true;
     if ( p->GetType() == svDoubleRef || p->GetType() == svExternalDoubleRef )
     {
         ScSingleRefData& rRef2 = p->GetDoubleRef()->Ref2;
-        ScAddress aAbs2 = rRef2.toAbs(rPos);
+        ScAddress aAbs2 = rRef2.toAbs(pDoc, rPos);
         if (!pDoc->ValidAddress(aAbs2))
             return true;
     }
@@ -56,8 +56,8 @@ bool ScDetectiveRefIter::GetNextRef( ScRange& rRange )
     if( p )
     {
         SingleDoubleRefProvider aProv( *p );
-        rRange.aStart = aProv.Ref1.toAbs(aPos);
-        rRange.aEnd = aProv.Ref2.toAbs(aPos);
+        rRange.aStart = aProv.Ref1.toAbs(mpDoc, aPos);
+        rRange.aEnd = aProv.Ref2.toAbs(mpDoc, aPos);
         bRet = true;
     }
 
diff --git a/sc/source/core/data/grouptokenconverter.cxx b/sc/source/core/data/grouptokenconverter.cxx
index 9da4dc476ea2..ee79aedd5004 100644
--- a/sc/source/core/data/grouptokenconverter.cxx
+++ b/sc/source/core/data/grouptokenconverter.cxx
@@ -121,7 +121,7 @@ bool ScGroupTokenConverter::convert( const ScTokenArray& rCode, sc::FormulaLogge
                 ScSingleRefData aRef = *p->GetSingleRef();
                 if( aRef.IsDeleted())
                     return false;
-                ScAddress aRefPos = aRef.toAbs(mrPos);
+                ScAddress aRefPos = aRef.toAbs(&mrDoc, mrPos);
                 if (aRef.IsRowRel())
                 {
                     if (isSelfReferenceRelative(aRefPos, aRef.Row()))
@@ -192,7 +192,7 @@ bool ScGroupTokenConverter::convert( const ScTokenArray& rCode, sc::FormulaLogge
                 ScComplexRefData aRef = *p->GetDoubleRef();
                 if( aRef.IsDeleted())
                     return false;
-                ScRange aAbs = aRef.toAbs(mrPos);
+                ScRange aAbs = aRef.toAbs(&mrDoc, mrPos);
 
                 // Multiple sheets not handled by vector/matrix.
                 if (aRef.Ref1.Tab() != aRef.Ref2.Tab())
diff --git a/sc/source/core/data/table7.cxx b/sc/source/core/data/table7.cxx
index 29dd2ddc9041..38f1d02e3fd7 100644
--- a/sc/source/core/data/table7.cxx
+++ b/sc/source/core/data/table7.cxx
@@ -269,7 +269,7 @@ void ScTable::SplitFormulaGroups( SCCOL nCol, std::vector<SCROW>& rRows )
     if (!IsColValid(nCol))
         return;
 
-    sc::SharedFormulaUtil::splitFormulaCellGroups(aCol[nCol].maCells, rRows);
+    sc::SharedFormulaUtil::splitFormulaCellGroups(&GetDoc(), aCol[nCol].maCells, rRows);
 }
 
 void ScTable::UnshareFormulaCells( SCCOL nCol, std::vector<SCROW>& rRows )
diff --git a/sc/source/core/tool/address.cxx b/sc/source/core/tool/address.cxx
index 930833057634..0ee1eeeb4862 100644
--- a/sc/source/core/tool/address.cxx
+++ b/sc/source/core/tool/address.cxx
@@ -2550,7 +2550,7 @@ bool AlphaToCol( const ScDocument* pDoc, SCCOL& rCol, const OUString& rStr)
         nResult += ScGlobal::ToUpperAlpha(c) - 'A';
         ++nPos;
     }
-    bool bOk = (ValidCol(nResult) && nPos > 0);
+    bool bOk = (pDoc->ValidCol(nResult) && nPos > 0);
     if (bOk)
         rCol = nResult;
     return bOk;
diff --git a/sc/source/core/tool/chartlis.cxx b/sc/source/core/tool/chartlis.cxx
index fab194a2be5d..f5560479e109 100644
--- a/sc/source/core/tool/chartlis.cxx
+++ b/sc/source/core/tool/chartlis.cxx
@@ -186,7 +186,7 @@ void ScChartListener::Update()
 ScRangeListRef ScChartListener::GetRangeList() const
 {
     ScRangeListRef aRLRef(new ScRangeList);
-    ScRefTokenHelper::getRangeListFromTokens(*aRLRef, *mpTokens, ScAddress());
+    ScRefTokenHelper::getRangeListFromTokens(mpDoc, *aRLRef, *mpTokens, ScAddress());
     return aRLRef;
 }
 
@@ -230,7 +230,7 @@ public:
         else
         {
             ScRange aRange;
-            ScRefTokenHelper::getRangeFromToken(aRange, pToken, ScAddress(), bExternal);
+            ScRefTokenHelper::getRangeFromToken(mpDoc, aRange, pToken, ScAddress(), bExternal);
             if (mbStart)
                 startListening(aRange);
             else
@@ -294,7 +294,7 @@ void ScChartListener::UpdateChartIntersecting( const ScRange& rRange )
     ScTokenRef pToken;
     ScRefTokenHelper::getTokenFromRange(mpDoc, pToken, rRange);
 
-    if (ScRefTokenHelper::intersects(*mpTokens, pToken, ScAddress()))
+    if (ScRefTokenHelper::intersects(mpDoc, *mpTokens, pToken, ScAddress()))
     {
         // force update (chart has to be loaded), don't use ScChartListener::Update
         mpDoc->UpdateChart(GetName());
diff --git a/sc/source/core/tool/compiler.cxx b/sc/source/core/tool/compiler.cxx
index 7f1460db8f51..f5d30d2e8f36 100644
--- a/sc/source/core/tool/compiler.cxx
+++ b/sc/source/core/tool/compiler.cxx
@@ -710,8 +710,8 @@ namespace {
 struct Convention_A1 : public ScCompiler::Convention
 {
     explicit Convention_A1( FormulaGrammar::AddressConvention eConv ) : ScCompiler::Convention( eConv ) { }
-    static void MakeColStr( OUStringBuffer& rBuffer, SCCOL nCol );
-    static void MakeRowStr( OUStringBuffer& rBuffer, SCROW nRow );
+    static void MakeColStr( ScSheetLimits& rLimits, OUStringBuffer& rBuffer, SCCOL nCol );
+    static void MakeRowStr( ScSheetLimits& rLimits, OUStringBuffer& rBuffer, SCROW nRow );
 
     ParseResult parseAnyToken( const OUString& rFormula,
                                sal_Int32 nSrcPos,
@@ -741,17 +741,17 @@ struct Convention_A1 : public ScCompiler::Convention
 
 }
 
-void Convention_A1::MakeColStr( OUStringBuffer& rBuffer, SCCOL nCol )
+void Convention_A1::MakeColStr( ScSheetLimits& rLimits, OUStringBuffer& rBuffer, SCCOL nCol )
 {
-    if ( !ValidCol( nCol) )
+    if ( !rLimits.ValidCol(nCol) )
         rBuffer.append(ScResId(STR_NO_REF_TABLE));
     else
         ::ScColToAlpha( rBuffer, nCol);
 }
 
-void Convention_A1::MakeRowStr( OUStringBuffer& rBuffer, SCROW nRow )
+void Convention_A1::MakeRowStr( ScSheetLimits& rLimits, OUStringBuffer& rBuffer, SCROW nRow )
 {
-    if ( !ValidRow(nRow) )
+    if ( !rLimits.ValidRow(nRow) )
         rBuffer.append(ScResId(STR_NO_REF_TABLE));
     else
         rBuffer.append(sal_Int32(nRow + 1));
@@ -781,7 +781,7 @@ struct ConventionOOO_A1 : public Convention_A1
     };
 
     static void MakeOneRefStrImpl(
-        OUStringBuffer& rBuffer,
+        ScSheetLimits& rLimits, OUStringBuffer& rBuffer,
         const OUString& rErrRef, const std::vector<OUString>& rTabNames,
         const ScSingleRefData& rRef, const ScAddress& rAbsRef,
         bool bForceTab, bool bODF, SingletonDisplay eSingletonDisplay )
@@ -815,20 +815,20 @@ struct ConventionOOO_A1 : public Convention_A1
         {
             if (!rRef.IsColRel())
                 rBuffer.append('$');
-            if (!ValidCol(rAbsRef.Col()) || rRef.IsColDeleted())
+            if (!rLimits.ValidCol(rAbsRef.Col()) || rRef.IsColDeleted())
                 rBuffer.append(rErrRef);
             else
-                MakeColStr(rBuffer, rAbsRef.Col());
+                MakeColStr(rLimits, rBuffer, rAbsRef.Col());
         }
 
         if (eSingletonDisplay != SINGLETON_COL)
         {
             if (!rRef.IsRowRel())
                 rBuffer.append('$');
-            if (!ValidRow(rAbsRef.Row()) || rRef.IsRowDeleted())
+            if (!rLimits.ValidRow(rAbsRef.Row()) || rRef.IsRowDeleted())
                 rBuffer.append(rErrRef);
             else
-                MakeRowStr(rBuffer, rAbsRef.Row());
+                MakeRowStr(rLimits, rBuffer, rAbsRef.Row());
         }
     }
 
@@ -836,8 +836,8 @@ struct ConventionOOO_A1 : public Convention_A1
             const ScComplexRefData& rRef, bool bFromRangeName )
     {
         // If any part is error, display as such.
-        if (!ValidCol(rAbs1.Col()) || rRef.Ref1.IsColDeleted() || !ValidRow(rAbs1.Row()) || rRef.Ref1.IsRowDeleted() ||
-            !ValidCol(rAbs2.Col()) || rRef.Ref2.IsColDeleted() || !ValidRow(rAbs2.Row()) || rRef.Ref2.IsRowDeleted())
+        if (!rLimits.ValidCol(rAbs1.Col()) || rRef.Ref1.IsColDeleted() || !rLimits.ValidRow(rAbs1.Row()) || rRef.Ref1.IsRowDeleted() ||
+            !rLimits.ValidCol(rAbs2.Col()) || rRef.Ref2.IsColDeleted() || !rLimits.ValidRow(rAbs2.Row()) || rRef.Ref2.IsRowDeleted())
             return SINGLETON_NONE;
 
         // A:A or $A:$A or A:$A or $A:A
@@ -875,17 +875,17 @@ struct ConventionOOO_A1 : public Convention_A1
     {
         // In case absolute/relative positions weren't separately available:
         // transform relative to absolute!
-        ScAddress aAbs1 = rRef.Ref1.toAbs(rPos), aAbs2;
+        ScAddress aAbs1 = rRef.Ref1.toAbs(rLimits, rPos), aAbs2;
         if( !bSingleRef )
-            aAbs2 = rRef.Ref2.toAbs(rPos);
+            aAbs2 = rRef.Ref2.toAbs(rLimits, rPos);
 
         SingletonDisplay eSingleton = bSingleRef ? SINGLETON_NONE :
             getSingletonDisplay( rLimits, aAbs1, aAbs2, rRef, bFromRangeName);
-        MakeOneRefStrImpl(rBuffer, rErrRef, rTabNames, rRef.Ref1, aAbs1, false, false, eSingleton);
+        MakeOneRefStrImpl(rLimits, rBuffer, rErrRef, rTabNames, rRef.Ref1, aAbs1, false, false, eSingleton);
         if (!bSingleRef)
         {
             rBuffer.append(':');
-            MakeOneRefStrImpl(rBuffer, rErrRef, rTabNames, rRef.Ref2, aAbs2, aAbs1.Tab() != aAbs2.Tab(), false,
+            MakeOneRefStrImpl(rLimits, rBuffer, rErrRef, rTabNames, rRef.Ref2, aAbs2, aAbs1.Tab() != aAbs2.Tab(), false,
                     eSingleton);
         }
     }
@@ -917,10 +917,11 @@ struct ConventionOOO_A1 : public Convention_A1
     }
 
     static bool makeExternalSingleRefStr(
+        ScSheetLimits& rLimits,
         OUStringBuffer& rBuffer, const OUString& rFileName, const OUString& rTabName,
         const ScSingleRefData& rRef, const ScAddress& rPos, bool bDisplayTabName, bool bEncodeUrl )
     {
-        ScAddress aAbsRef = rRef.toAbs(rPos);
+        ScAddress aAbsRef = rRef.toAbs(rLimits, rPos);
         if (bDisplayTabName)
         {
             OUString aFile;
@@ -940,15 +941,16 @@ struct ConventionOOO_A1 : public Convention_A1
 
         if (!rRef.IsColRel())
             rBuffer.append('$');
-        MakeColStr( rBuffer, aAbsRef.Col());
+        MakeColStr( rLimits, rBuffer, aAbsRef.Col());
         if (!rRef.IsRowRel())
             rBuffer.append('$');
-        MakeRowStr( rBuffer, aAbsRef.Row());
+        MakeRowStr( rLimits, rBuffer, aAbsRef.Row());
 
         return true;
     }
 
     static void makeExternalRefStrImpl(
+        ScSheetLimits& rLimits,
         OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
         const OUString& rTabName, const ScSingleRefData& rRef, bool bODF )
     {
@@ -956,24 +958,26 @@ struct ConventionOOO_A1 : public Convention_A1
             rBuffer.append( '[');
 
         bool bEncodeUrl = bODF;
-        makeExternalSingleRefStr(rBuffer, rFileName, rTabName, rRef, rPos, true, bEncodeUrl);
+        makeExternalSingleRefStr(rLimits, rBuffer, rFileName, rTabName, rRef, rPos, true, bEncodeUrl);
         if (bODF)
             rBuffer.append( ']');
     }
 
     virtual void makeExternalRefStr(
+        ScSheetLimits& rLimits,
         OUStringBuffer& rBuffer, const ScAddress& rPos, sal_uInt16 /*nFileId*/, const OUString& rFileName,
         const OUString& rTabName, const ScSingleRefData& rRef ) const override
     {
-        makeExternalRefStrImpl(rBuffer, rPos, rFileName, rTabName, rRef, false);
+        makeExternalRefStrImpl(rLimits, rBuffer, rPos, rFileName, rTabName, rRef, false);
     }
 
     static void makeExternalRefStrImpl(
+        ScSheetLimits& rLimits,
         OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
         const std::vector<OUString>& rTabNames, const OUString& rTabName,
         const ScComplexRefData& rRef, bool bODF )
     {
-        ScRange aAbsRange = rRef.toAbs(rPos);
+        ScRange aAbsRange = rRef.toAbs(rLimits, rPos);
 
         if (bODF)
             rBuffer.append( '[');
@@ -982,7 +986,7 @@ struct ConventionOOO_A1 : public Convention_A1
 
         do
         {
-            if (!makeExternalSingleRefStr(rBuffer, rFileName, rTabName, rRef.Ref1, rPos, true, bEncodeUrl))
+            if (!makeExternalSingleRefStr(rLimits, rBuffer, rFileName, rTabName, rRef.Ref1, rPos, true, bEncodeUrl))
                 break;
 
             rBuffer.append(':');
@@ -1000,7 +1004,7 @@ struct ConventionOOO_A1 : public Convention_A1
             }
             else if (bODF)
                 rBuffer.append( '.');      // need at least the sheet separator in ODF
-            makeExternalSingleRefStr(
+            makeExternalSingleRefStr(rLimits,
                 rBuffer, rFileName, aLastTabName, rRef.Ref2, rPos, bDisplayTabName, bEncodeUrl);
         } while (false);
 
@@ -1009,12 +1013,12 @@ struct ConventionOOO_A1 : public Convention_A1
     }
 
     virtual void makeExternalRefStr(
-        ScSheetLimits&,
+        ScSheetLimits& rLimits,
         OUStringBuffer& rBuffer, const ScAddress& rPos, sal_uInt16 /*nFileId*/, const OUString& rFileName,
         const std::vector<OUString>& rTabNames, const OUString& rTabName,
         const ScComplexRefData& rRef ) const override
     {
-        makeExternalRefStrImpl(rBuffer, rPos, rFileName, rTabNames, rTabName, rRef, false);
+        makeExternalRefStrImpl(rLimits, rBuffer, rPos, rFileName, rTabNames, rTabName, rRef, false);
     }
 };
 
@@ -1035,12 +1039,12 @@ struct ConventionOOO_A1_ODF : public ConventionOOO_A1
         rBuffer.append('[');
         // In case absolute/relative positions weren't separately available:
         // transform relative to absolute!
-        ScAddress aAbs1 = rRef.Ref1.toAbs(rPos), aAbs2;
+        ScAddress aAbs1 = rRef.Ref1.toAbs(rLimits, rPos), aAbs2;
         if( !bSingleRef )
-            aAbs2 = rRef.Ref2.toAbs(rPos);
+            aAbs2 = rRef.Ref2.toAbs(rLimits, rPos);
 
-        if (FormulaGrammar::isODFF(eGram) && (rRef.Ref1.IsDeleted() || !ValidAddress(aAbs1) ||
-                    (!bSingleRef && (rRef.Ref2.IsDeleted() || !ValidAddress(aAbs2)))))
+        if (FormulaGrammar::isODFF(eGram) && (rRef.Ref1.IsDeleted() || !rLimits.ValidAddress(aAbs1) ||
+                    (!bSingleRef && (rRef.Ref2.IsDeleted() || !rLimits.ValidAddress(aAbs2)))))
         {
             rBuffer.append(rErrRef);
             // For ODFF write [#REF!], but not for PODF so apps reading ODF
@@ -1051,11 +1055,11 @@ struct ConventionOOO_A1_ODF : public ConventionOOO_A1
         {
             SingletonDisplay eSingleton = bSingleRef ? SINGLETON_NONE :
                 getSingletonDisplay( rLimits, aAbs1, aAbs2, rRef, bFromRangeName);
-            MakeOneRefStrImpl(rBuffer, rErrRef, rTabNames, rRef.Ref1, aAbs1, false, true, eSingleton);
+            MakeOneRefStrImpl(rLimits, rBuffer, rErrRef, rTabNames, rRef.Ref1, aAbs1, false, true, eSingleton);
             if (!bSingleRef)
             {
                 rBuffer.append(':');
-                MakeOneRefStrImpl(rBuffer, rErrRef, rTabNames, rRef.Ref2, aAbs2, aAbs1.Tab() != aAbs2.Tab(), true,
+                MakeOneRefStrImpl(rLimits, rBuffer, rErrRef, rTabNames, rRef.Ref2, aAbs2, aAbs1.Tab() != aAbs2.Tab(), true,
                         eSingleton);
             }
         }
@@ -1069,19 +1073,20 @@ struct ConventionOOO_A1_ODF : public ConventionOOO_A1
     }
 
     virtual void makeExternalRefStr(
+        ScSheetLimits& rLimits,
         OUStringBuffer& rBuffer, const ScAddress& rPos, sal_uInt16 /*nFileId*/, const OUString& rFileName,
         const OUString& rTabName, const ScSingleRefData& rRef ) const override
     {
-        makeExternalRefStrImpl(rBuffer, rPos, rFileName, rTabName, rRef, true);
+        makeExternalRefStrImpl(rLimits, rBuffer, rPos, rFileName, rTabName, rRef, true);
     }
 
     virtual void makeExternalRefStr(
-        ScSheetLimits& ,
+        ScSheetLimits& rLimits,
         OUStringBuffer& rBuffer, const ScAddress& rPos, sal_uInt16 /*nFileId*/, const OUString& rFileName,
         const std::vector<OUString>& rTabNames,
         const OUString& rTabName, const ScComplexRefData& rRef ) const override
     {
-        makeExternalRefStrImpl(rBuffer, rPos, rFileName, rTabNames, rTabName, rRef, true);
+        makeExternalRefStrImpl(rLimits, rBuffer, rPos, rFileName, rTabNames, rTabName, rRef, true);
     }
 };
 
@@ -1092,10 +1097,11 @@ struct ConventionXL
     }
 
     static void GetTab(
+        ScSheetLimits& rLimits,
         const ScAddress& rPos, const std::vector<OUString>& rTabNames,
         const ScSingleRefData& rRef, OUString& rTabName )
     {
-        ScAddress aAbs = rRef.toAbs(rPos);
+        ScAddress aAbs = rRef.toAbs(rLimits, rPos);
         if (rRef.IsTabDeleted() || static_cast<size_t>(aAbs.Tab()) >= rTabNames.size())
         {
             rTabName = ScResId( STR_NO_REF_TABLE );
@@ -1104,7 +1110,7 @@ struct ConventionXL
         rTabName = rTabNames[aAbs.Tab()];
     }
 
-    static void MakeTabStr( OUStringBuffer& rBuf,
+    static void MakeTabStr( ScSheetLimits& rLimits, OUStringBuffer& rBuf,
                             const ScAddress& rPos,
                             const std::vector<OUString>& rTabNames,
                             const ScComplexRefData& rRef,
@@ -1114,11 +1120,11 @@ struct ConventionXL
         {
             OUString aStartTabName, aEndTabName;
 
-            GetTab(rPos, rTabNames, rRef.Ref1, aStartTabName);
+            GetTab(rLimits, rPos, rTabNames, rRef.Ref1, aStartTabName);
 
             if( !bSingleRef && rRef.Ref2.IsFlag3D() )
             {
-                GetTab(rPos, rTabNames, rRef.Ref2, aEndTabName);
+                GetTab(rLimits, rPos, rTabNames, rRef.Ref2, aEndTabName);
             }
 
             rBuf.append( aStartTabName );
@@ -1260,14 +1266,14 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
     ConventionXL_A1() : Convention_A1( FormulaGrammar::CONV_XL_A1 ) { }
     explicit ConventionXL_A1( FormulaGrammar::AddressConvention eConv ) : Convention_A1( eConv ) { }
 
-    static void makeSingleCellStr( OUStringBuffer& rBuf, const ScSingleRefData& rRef, const ScAddress& rAbs )
+    static void makeSingleCellStr( ScSheetLimits& rLimits, OUStringBuffer& rBuf, const ScSingleRefData& rRef, const ScAddress& rAbs )
     {
         if (!rRef.IsColRel())
             rBuf.append('$');
-        MakeColStr(rBuf, rAbs.Col());
+        MakeColStr(rLimits, rBuf, rAbs.Col());
         if (!rRef.IsRowRel())
             rBuf.append('$');
-        MakeRowStr(rBuf, rAbs.Row());
+        MakeRowStr(rLimits, rBuf, rAbs.Row());
     }
 
     virtual void makeRefStr(
@@ -1284,11 +1290,11 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
 
         // Play fast and loose with invalid refs.  There is not much point in producing
         // Foo!A1:#REF! versus #REF! at this point
-        ScAddress aAbs1 = aRef.Ref1.toAbs(rPos), aAbs2;
+        ScAddress aAbs1 = aRef.Ref1.toAbs(rLimits, rPos), aAbs2;
 
-        MakeTabStr(rBuf, rPos, rTabNames, aRef, bSingleRef);
+        MakeTabStr(rLimits, rBuf, rPos, rTabNames, aRef, bSingleRef);
 
-        if (!ValidAddress(aAbs1))
+        if (!rLimits.ValidAddress(aAbs1))
         {
             rBuf.append(rErrRef);
             return;
@@ -1296,8 +1302,8 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
 
         if( !bSingleRef )
         {
-            aAbs2 = aRef.Ref2.toAbs(rPos);
-            if (!ValidAddress(aAbs2))
+            aAbs2 = aRef.Ref2.toAbs(rLimits, rPos);
+            if (!rLimits.ValidAddress(aAbs2))
             {
                 rBuf.append(rErrRef);
                 return;
@@ -1307,11 +1313,11 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
             {
                 if (!aRef.Ref1.IsRowRel())
                     rBuf.append( '$' );
-                MakeRowStr(rBuf, aAbs1.Row());
+                MakeRowStr(rLimits, rBuf, aAbs1.Row());
                 rBuf.append( ':' );
                 if (!aRef.Ref2.IsRowRel())
                     rBuf.append( '$' );
-                MakeRowStr(rBuf, aAbs2.Row());
+                MakeRowStr(rLimits, rBuf, aAbs2.Row());
                 return;
             }
 
@@ -1319,20 +1325,20 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
             {
                 if (!aRef.Ref1.IsColRel())
                     rBuf.append( '$' );
-                MakeColStr(rBuf, aAbs1.Col());
+                MakeColStr(rLimits, rBuf, aAbs1.Col());
                 rBuf.append( ':' );
                 if (!aRef.Ref2.IsColRel())
                     rBuf.append( '$' );
-                MakeColStr(rBuf, aAbs2.Col());
+                MakeColStr(rLimits, rBuf, aAbs2.Col());
                 return;
             }
         }
 
-        makeSingleCellStr(rBuf, aRef.Ref1, aAbs1);
+        makeSingleCellStr(rLimits, rBuf, aRef.Ref1, aAbs1);
         if (!bSingleRef)
         {
             rBuf.append( ':' );
-            makeSingleCellStr(rBuf, aRef.Ref2, aAbs2);
+            makeSingleCellStr(rLimits, rBuf, aRef.Ref2, aAbs2);
         }
     }
 
@@ -1377,6 +1383,7 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
     }
 
     virtual void makeExternalRefStr(
+        ScSheetLimits& rLimits,
         OUStringBuffer& rBuffer, const ScAddress& rPos, sal_uInt16 /*nFileId*/, const OUString& rFileName,
         const OUString& rTabName, const ScSingleRefData& rRef ) const override
     {
@@ -1390,26 +1397,26 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
         ScRangeStringConverter::AppendTableName(rBuffer, rTabName);
         rBuffer.append('!');
 
-        makeSingleCellStr(rBuffer, rRef, rRef.toAbs(rPos));
+        makeSingleCellStr(rLimits, rBuffer, rRef, rRef.toAbs(rLimits, rPos));
     }
 
     virtual void makeExternalRefStr(
-        ScSheetLimits&,
+        ScSheetLimits& rLimits,
         OUStringBuffer& rBuffer, const ScAddress& rPos, sal_uInt16 /*nFileId*/, const OUString& rFileName,
         const std::vector<OUString>& rTabNames, const OUString& rTabName,
         const ScComplexRefData& rRef ) const override
     {
-        ScRange aAbsRef = rRef.toAbs(rPos);
+        ScRange aAbsRef = rRef.toAbs(rLimits, rPos);
 
         ConventionXL::makeExternalDocStr(rBuffer, rFileName);
         ConventionXL::makeExternalTabNameRange(rBuffer, rTabName, rTabNames, aAbsRef);
         rBuffer.append('!');
 
-        makeSingleCellStr(rBuffer, rRef.Ref1, aAbsRef.aStart);
+        makeSingleCellStr(rLimits, rBuffer, rRef.Ref1, aAbsRef.aStart);
         if (aAbsRef.aStart != aAbsRef.aEnd)
         {
             rBuffer.append(':');
-            makeSingleCellStr(rBuffer, rRef.Ref2, aAbsRef.aEnd);
+            makeSingleCellStr(rLimits, rBuffer, rRef.Ref2, aAbsRef.aEnd);
         }
     }
 };
@@ -1487,6 +1494,7 @@ struct ConventionXL_OOX : public ConventionXL_A1
     }
 
     virtual void makeExternalRefStr(
+        ScSheetLimits& rLimits,
         OUStringBuffer& rBuffer, const ScAddress& rPos, sal_uInt16 nFileId, const OUString& /*rFileName*/,
         const OUString& rTabName, const ScSingleRefData& rRef ) const override
     {
@@ -1509,11 +1517,11 @@ struct ConventionXL_OOX : public ConventionXL_A1
         }
         rBuffer.append('!');
 
-        makeSingleCellStr(rBuffer, rRef, rRef.toAbs(rPos));
+        makeSingleCellStr(rLimits, rBuffer, rRef, rRef.toAbs(rLimits, rPos));
     }
 
     virtual void makeExternalRefStr(
-        ScSheetLimits& ,
+        ScSheetLimits& rLimits,
         OUStringBuffer& rBuffer, const ScAddress& rPos, sal_uInt16 nFileId, const OUString& /*rFileName*/,
         const std::vector<OUString>& rTabNames, const OUString& rTabName,
         const ScComplexRefData& rRef ) const override
@@ -1524,7 +1532,7 @@ struct ConventionXL_OOX : public ConventionXL_A1
         // sheet names as well. The [N] having to be within the quoted sheet
         // name is ugly enough...
 
-        ScRange aAbsRef = rRef.toAbs(rPos);
+        ScRange aAbsRef = rRef.toAbs(rLimits, rPos);
 
         OUStringBuffer aBuf;
         ConventionXL::makeExternalTabNameRange( aBuf, rTabName, rTabNames, aAbsRef);
@@ -1541,11 +1549,11 @@ struct ConventionXL_OOX : public ConventionXL_A1
         }
         rBuffer.append('!');
 
-        makeSingleCellStr(rBuffer, rRef.Ref1, aAbsRef.aStart);
+        makeSingleCellStr(rLimits, rBuffer, rRef.Ref1, aAbsRef.aStart);
         if (aAbsRef.aStart != aAbsRef.aEnd)
         {
             rBuffer.append(':');
-            makeSingleCellStr(rBuffer, rRef.Ref2, aAbsRef.aEnd);
+            makeSingleCellStr(rLimits, rBuffer, rRef.Ref2, aAbsRef.aEnd);
         }
     }
 
@@ -1600,14 +1608,14 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
                      bool bSingleRef,
                      bool /*bFromRangeName*/ ) const override
     {
-        ScRange aAbsRef = rRef.toAbs(rPos);
+        ScRange aAbsRef = rRef.toAbs(rLimits, rPos);
         ScComplexRefData aRef( rRef );
 
-        MakeTabStr(rBuf, rPos, rTabNames, aRef, bSingleRef);
+        MakeTabStr(rLimits, rBuf, rPos, rTabNames, aRef, bSingleRef);
 
         // Play fast and loose with invalid refs.  There is not much point in producing
         // Foo!A1:#REF! versus #REF! at this point
-        if (!ValidCol(aAbsRef.aStart.Col()) || !ValidRow(aAbsRef.aStart.Row()))
+        if (!rLimits.ValidCol(aAbsRef.aStart.Col()) || !rLimits.ValidRow(aAbsRef.aStart.Row()))
         {
             rBuf.append(rErrRef);
             return;
@@ -1615,7 +1623,7 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
 
         if( !bSingleRef )
         {
-            if (!ValidCol(aAbsRef.aEnd.Col()) || !ValidRow(aAbsRef.aEnd.Row()))
+            if (!rLimits.ValidCol(aAbsRef.aEnd.Col()) || !rLimits.ValidRow(aAbsRef.aEnd.Row()))
             {
                 rBuf.append(rErrRef);
                 return;
@@ -1699,6 +1707,7 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
     }
 
     virtual void makeExternalRefStr(
+        ScSheetLimits& rLimits,
         OUStringBuffer& rBuffer, const ScAddress& rPos, sal_uInt16 /*nFileId*/, const OUString& rFileName,
         const OUString& rTabName, const ScSingleRefData& rRef ) const override
     {
@@ -1708,7 +1717,7 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
         // whole file path with [] because the file name can contain any
         // characters.
 
-        ScAddress aAbsRef = rRef.toAbs(rPos);
+        ScAddress aAbsRef = rRef.toAbs(rLimits, rPos);
         ConventionXL::makeExternalDocStr(rBuffer, rFileName);
         ScRangeStringConverter::AppendTableName(rBuffer, rTabName);
         rBuffer.append('!');
@@ -1723,13 +1732,13 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
         const std::vector<OUString>& rTabNames, const OUString& rTabName,
         const ScComplexRefData& rRef ) const override
     {
-        ScRange aAbsRef = rRef.toAbs(rPos);
+        ScRange aAbsRef = rRef.toAbs(rLimits, rPos);
 
         ConventionXL::makeExternalDocStr(rBuffer, rFileName);
         ConventionXL::makeExternalTabNameRange(rBuffer, rTabName, rTabNames, aAbsRef);
         rBuffer.append('!');
 
-        if (!ValidCol(aAbsRef.aEnd.Col()) || !ValidRow(aAbsRef.aEnd.Row()))
+        if (!rLimits.ValidCol(aAbsRef.aEnd.Col()) || !rLimits.ValidRow(aAbsRef.aEnd.Row()))
         {
             rBuffer.append(ScResId(STR_NO_REF_TABLE));
             return;
@@ -3183,7 +3192,7 @@ bool ScCompiler::IsDoubleReference( const OUString& rName, const OUString* pErrR
         if ( !(nFlags & ScRefFlags::TAB2_VALID) )
             aRef.Ref2.SetTabDeleted( true );        // #REF!
         aRef.Ref2.SetFlag3D( ( nFlags & ScRefFlags::TAB2_3D ) != ScRefFlags::ZERO );
-        aRef.SetRange(aRange, aPos);
+        aRef.SetRange(pDoc->GetSheetLimits(), aRange, aPos);
         if (aExtInfo.mbExternal)
         {
             ScExternalRefManager* pRefMgr = pDoc->GetExternalRefManager();
@@ -3249,7 +3258,7 @@ bool ScCompiler::IsSingleReference( const OUString& rName, const OUString* pErrR
                 aRef.SetTabDeleted(true);
             nFlags |= ScRefFlags::VALID;
         }
-        aRef.SetAddress(aAddr, aPos);
+        aRef.SetAddress(pDoc->GetSheetLimits(), aAddr, aPos);
 
         if (aExtInfo.mbExternal)
         {
@@ -3583,7 +3592,7 @@ bool ScCompiler::IsColRowName( const OUString& rName )
                                 aRef.SetColRel( true );     // ColName
                             else
                                 aRef.SetRowRel( true );     // RowName
-                            aRef.SetAddress(aIter.GetPos(), aPos);
+                            aRef.SetAddress(pDoc->GetSheetLimits(), aIter.GetPos(), aPos);
                             bInList = bFound = true;
                         }
                     }
@@ -3765,7 +3774,7 @@ bool ScCompiler::IsColRowName( const OUString& rName )
                 aRef.SetRowRel( true );     // RowName
             else
                 aRef.SetColRel( true );     // ColName
-            aRef.SetAddress(aAdr, aPos);
+            aRef.SetAddress(pDoc->GetSheetLimits(), aAdr, aPos);
         }
     }
     if ( bFound )
@@ -5011,7 +5020,7 @@ void ScCompiler::CreateStringFromExternal( OUStringBuffer& rBuffer, const Formul
             rBuffer.append(pConv->makeExternalNameStr( nFileId, *pFileName, t->GetString().getString()));
         break;
         case svExternalSingleRef:
-            pConv->makeExternalRefStr(
+            pConv->makeExternalRefStr(pDoc->GetSheetLimits(),
                    rBuffer, GetPos(), nFileId, *pFileName, t->GetString().getString(),
                    *t->GetSingleRef());
         break;
@@ -5118,7 +5127,7 @@ void ScCompiler::CreateStringFromSingleRef( OUStringBuffer& rBuffer, const Formu
     aRef.Ref1 = aRef.Ref2 = rRef;
     if ( eOp == ocColRowName )
     {
-        ScAddress aAbs = rRef.toAbs(aPos);
+        ScAddress aAbs = rRef.toAbs(pDoc, aPos);
         if (pDoc->HasStringData(aAbs.Col(), aAbs.Row(), aAbs.Tab()))
         {
             OUString aStr = pDoc->GetString(aAbs, mpInterpreterContext);
@@ -5135,7 +5144,7 @@ void ScCompiler::CreateStringFromSingleRef( OUStringBuffer& rBuffer, const Formu
     else if (pArr && (p = maArrIterator.PeekPrevNoSpaces()) && p->GetOpCode() == ocTableRefOpen)
     {
         OUString aStr;
-        ScAddress aAbs = rRef.toAbs(aPos);
+        ScAddress aAbs = rRef.toAbs(pDoc, aPos);
         const ScDBData* pData = pDoc->GetDBAtCursor( aAbs.Col(), aAbs.Row(), aAbs.Tab(), ScDBDataPortion::AREA);
         SAL_WARN_IF( !pData, "sc.core", "ScCompiler::CreateStringFromSingleRef - TableRef without ScDBData: " <<
                 aAbs.Format( ScRefFlags::VALID | ScRefFlags::TAB_3D, pDoc));
@@ -5323,8 +5332,8 @@ void ScCompiler::fillAddInToken(::std::vector< css::sheet::FormulaOpCodeMapEntry
 bool ScCompiler::HandleColRowName()
 {
     ScSingleRefData& rRef = *mpToken->GetSingleRef();
-    const ScAddress aAbs = rRef.toAbs(aPos);
-    if (!ValidAddress(aAbs))
+    const ScAddress aAbs = rRef.toAbs(pDoc, aPos);
+    if (!pDoc->ValidAddress(aAbs))
     {
         SetError( FormulaError::NoRef );
         return true;
@@ -5497,7 +5506,7 @@ bool ScCompiler::HandleColRowName()
                     aRefData.SetColRel( true );
                 else
                     aRefData.SetRowRel( true );
-                aRefData.SetAddress(aRange.aStart, aPos);
+                aRefData.SetAddress(pDoc->GetSheetLimits(), aRange.aStart, aPos);
                 pNew->AddSingleReference( aRefData );
             }
             else
@@ -5514,7 +5523,7 @@ bool ScCompiler::HandleColRowName()
                     aRefData.Ref1.SetRowRel( true );
                     aRefData.Ref2.SetRowRel( true );
                 }
-                aRefData.SetRange(aRange, aPos);
+                aRefData.SetRange(pDoc->GetSheetLimits(), aRange, aPos);
                 if ( bInList )
                     pNew->AddDoubleReference( aRefData );
                 else
@@ -5543,7 +5552,7 @@ bool ScCompiler::HandleDbData()
         ScRange aRange;
         pDBData->GetArea(aRange);
         aRange.aEnd.SetTab(aRange.aStart.Tab());
-        aRefData.SetRange(aRange, aPos);
+        aRefData.SetRange(pDoc->GetSheetLimits(), aRange, aPos);
         ScTokenArray* pNew = new ScTokenArray(pDoc);
         pNew->AddDoubleReference( aRefData );
         PushTokenArray( pNew, true );
@@ -5760,7 +5769,7 @@ bool ScCompiler::HandleTableRef()
                 {
                     case svSingleRef:
                         {
-                            aColRange.aStart = aColRange.aEnd = mpToken->GetSingleRef()->toAbs( aPos);
+                            aColRange.aStart = aColRange.aEnd = mpToken->GetSingleRef()->toAbs(pDoc, aPos);
                             if (    GetTokenIfOpCode( ocTableRefClose) && (nLevel--) &&
                                     GetTokenIfOpCode( ocRange) &&
                                     GetTokenIfOpCode( ocTableRefOpen) && (++nLevel) &&
@@ -5770,7 +5779,7 @@ bool ScCompiler::HandleTableRef()
                                     aColRange = ScRange( ScAddress::INITIALIZE_INVALID);
                                 else
                                 {
-                                    aColRange.aEnd = mpToken->GetSingleRef()->toAbs( aPos);
+                                    aColRange.aEnd = mpToken->GetSingleRef()->toAbs(pDoc, aPos);
                                     aColRange.PutInOrder();
                                     bCol2Rel = mpToken->GetSingleRef()->IsColRel();
                                     bCol2RelName = mpToken->GetSingleRef()->IsRelName();
@@ -5807,13 +5816,13 @@ bool ScCompiler::HandleTableRef()
                     aRefData.SetFlag3D( true);
                     if (nError != FormulaError::NONE)
                     {
-                        aRefData.SetAddress( aRange.aStart, aRange.aStart);
+                        aRefData.SetAddress( pDoc->GetSheetLimits(), aRange.aStart, aRange.aStart);
                         pTR->SetAreaRefRPN( new ScSingleRefToken(pDoc->GetSheetLimits(), aRefData));   // set reference at TableRef
                         pNew->Add( new FormulaErrorToken( nError));             // set error in RPN
                     }
                     else
                     {
-                        aRefData.SetAddress( aRange.aStart, aPos);
+                        aRefData.SetAddress( pDoc->GetSheetLimits(), aRange.aStart, aPos);
                         pTR->SetAreaRefRPN( pNew->AddSingleReference( aRefData));
                     }
                 }
@@ -5836,13 +5845,13 @@ bool ScCompiler::HandleTableRef()
                     aRefData.Ref1.SetFlag3D( true);
                     if (nError != FormulaError::NONE)
                     {
-                        aRefData.SetRange( aRange, aRange.aStart);
+                        aRefData.SetRange( pDoc->GetSheetLimits(), aRange, aRange.aStart);
                         pTR->SetAreaRefRPN( new ScDoubleRefToken(pDoc->GetSheetLimits(), aRefData));   // set reference at TableRef
                         pNew->Add( new FormulaErrorToken( nError));             // set error in RPN
                     }
                     else
                     {
-                        aRefData.SetRange( aRange, aPos);
+                        aRefData.SetRange( pDoc->GetSheetLimits(), aRange, aPos);
                         pTR->SetAreaRefRPN( pNew->AddDoubleReference( aRefData));
                     }
                 }
@@ -6078,7 +6087,7 @@ void ScCompiler::ReplaceDoubleRefII(FormulaToken** ppDoubleRefTok)
     if (!rRange.Ref1.IsRowRel() && !rRange.Ref2.IsRowRel())
         return;
 
-    ScRange aAbsRange = rRange.toAbs(aPos);
+    ScRange aAbsRange = rRange.toAbs(pDoc, aPos);
     if (aAbsRange.aStart == aAbsRange.aEnd)
         return; // Nothing to do (trivial case).
 
@@ -6092,7 +6101,7 @@ void ScCompiler::ReplaceDoubleRefII(FormulaToken** ppDoubleRefTok)
     aSingleRef.SetColRel(rRange.Ref1.IsColRel());
     aSingleRef.SetRowRel(true);
     aSingleRef.SetTabRel(rRange.Ref1.IsTabRel());
-    aSingleRef.SetAddress(aAddr, aPos);
+    aSingleRef.SetAddress(pDoc->GetSheetLimits(), aAddr, aPos);
 
     // Replace the original doubleref token with computed singleref token
     FormulaToken* pNewSingleRefTok = new ScSingleRefToken(pDoc->GetSheetLimits(), aSingleRef);
@@ -6168,7 +6177,7 @@ static void lcl_GetColRowDeltas(const ScRange& rRange, SCCOL& rXDelta, SCROW& rY
 
 bool ScCompiler::AdjustSumRangeShape(const ScComplexRefData& rBaseRange, ScComplexRefData& rSumRange)
 {
-    ScRange aAbs = rSumRange.toAbs(aPos);
+    ScRange aAbs = rSumRange.toAbs(pDoc, aPos);
 
     // Current sum-range end col/row
     SCCOL nEndCol = aAbs.aEnd.Col();
@@ -6177,7 +6186,7 @@ bool ScCompiler::AdjustSumRangeShape(const ScComplexRefData& rBaseRange, ScCompl
     // Current behaviour is, we will get a #NAME? for the below case, so bail out.
     // Note that sum-range's End[Col,Row] are same as Start[Col,Row] if the original formula
     // has a single-ref as the sum-range.
-    if (!ValidCol(nEndCol) || !ValidRow(nEndRow))
+    if (!pDoc->ValidCol(nEndCol) || !pDoc->ValidRow(nEndRow))
         return false;
 
     SCCOL nXDeltaSum = 0;
@@ -6185,7 +6194,7 @@ bool ScCompiler::AdjustSumRangeShape(const ScComplexRefData& rBaseRange, ScCompl
 
     lcl_GetColRowDeltas(aAbs, nXDeltaSum, nYDeltaSum);
 
-    aAbs = rBaseRange.toAbs(aPos);
+    aAbs = rBaseRange.toAbs(pDoc, aPos);
     SCCOL nXDelta = 0;
     SCROW nYDelta = 0;
 
diff --git a/sc/source/core/tool/consoli.cxx b/sc/source/core/tool/consoli.cxx
index bb8647e5e401..d134a9e45cec 100644
--- a/sc/source/core/tool/consoli.cxx
+++ b/sc/source/core/tool/consoli.cxx
@@ -474,7 +474,7 @@ void ScConsData::OutputToDocument( ScDocument* pDestDoc, SCCOL nCol, SCROW nRow,
                                 {
                                     // insert reference (absolute, 3d)
 
-                                    aSRef.SetAddress(ScAddress(aRef.nCol,aRef.nRow,aRef.nTab), ScAddress());
+                                    aSRef.SetAddress(pDestDoc->GetSheetLimits(), ScAddress(aRef.nCol,aRef.nRow,aRef.nTab), ScAddress());
 
                                     ScTokenArray aRefArr(pDestDoc);
                                     aRefArr.AddSingleReference(aSRef);
@@ -493,7 +493,7 @@ void ScConsData::OutputToDocument( ScDocument* pDestDoc, SCCOL nCol, SCROW nRow,
 
                             ScRange aRange(sal::static_int_cast<SCCOL>(nCol+nArrX), nRow+nArrY, nTab);
                             aRange.aEnd.SetRow(nRow+nArrY+nNeeded-1);
-                            aCRef.SetRange(aRange, aDest);
+                            aCRef.SetRange(pDestDoc->GetSheetLimits(), aRange, aDest);
 
                             ScTokenArray aArr(pDestDoc);
                             aArr.AddOpCode(eOpCode);            // selected function
diff --git a/sc/source/core/tool/detfunc.cxx b/sc/source/core/tool/detfunc.cxx
index 045998d7cdf8..ac1af287d1a5 100644
--- a/sc/source/core/tool/detfunc.cxx
+++ b/sc/source/core/tool/detfunc.cxx
@@ -1400,7 +1400,7 @@ void ScDetectiveFunc::GetAllSuccs(SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW n
         {
             const ScAddress& aPos = aIter.GetPos();
             ScTokenRef pRef(p->Clone());
-            if (ScRefTokenHelper::intersects(aSrcRange, pRef, aPos))
+            if (ScRefTokenHelper::intersects(pDoc, aSrcRange, pRef, aPos))
             {
                 // This address is absolute.
                 pRef = ScRefTokenHelper::createRefToken(pDoc, aPos);
diff --git a/sc/source/core/tool/doubleref.cxx b/sc/source/core/tool/doubleref.cxx
index a2a464b6813f..2a2316a0c173 100644
--- a/sc/source/core/tool/doubleref.cxx
+++ b/sc/source/core/tool/doubleref.cxx
@@ -46,6 +46,7 @@ void lcl_uppercase(OUString& rStr)
 }
 
 bool lcl_createStarQuery(
+    const ScDocument* pDoc,
     svl::SharedStringPool& rPool, ScQueryParamBase* pParam, const ScDBRangeBase* pDBRef, const ScDBRangeBase* pQueryRef)
 {
     // A valid StarQuery must be at least 4 columns wide. To be precise it
@@ -96,7 +97,7 @@ bool lcl_createStarQuery(
             // field name in the 2nd column.
             aCellStr = pQueryRef->getString(1, nRow);
             SCCOL nField = pDBRef->findFieldColumn(aCellStr); // TODO: must be case insensitive comparison.
-            if (ValidCol(nField))
+            if (pDoc->ValidCol(nField))
             {
                 rEntry.nField = nField;
                 bValid = true;
@@ -147,6 +148,7 @@ bool lcl_createStarQuery(
 }
 
 bool lcl_createExcelQuery(
+    const ScDocument* pDoc,
     svl::SharedStringPool& rPool, ScQueryParamBase* pParam, const ScDBRangeBase* pDBRef, const ScDBRangeBase* pQueryRef)
 {
     bool bValid = true;
@@ -158,7 +160,7 @@ bool lcl_createExcelQuery(
     {
         OUString aQueryStr = pQueryRef->getString(nCol, 0);
         SCCOL nField = pDBRef->findFieldColumn(aQueryStr);
-        if (ValidCol(nField))
+        if (pDoc->ValidCol(nField))
             aFields[nCol] = nField;
         else
             bValid = false;
@@ -213,6 +215,7 @@ bool lcl_createExcelQuery(
 }
 
 bool lcl_fillQueryEntries(
+    const ScDocument* pDoc,
     svl::SharedStringPool& rPool, ScQueryParamBase* pParam, const ScDBRangeBase* pDBRef, const ScDBRangeBase* pQueryRef)
 {
     SCSIZE nCount = pParam->GetEntryCount();
@@ -220,10 +223,10 @@ bool lcl_fillQueryEntries(
         pParam->GetEntry(i).Clear();
 
     // Standard QueryTabelle
-    bool bValid = lcl_createStarQuery(rPool, pParam, pDBRef, pQueryRef);
+    bool bValid = lcl_createStarQuery(pDoc, rPool, pParam, pDBRef, pQueryRef);
     // Excel QueryTabelle
     if (!bValid)
-        bValid = lcl_createExcelQuery(rPool, pParam, pDBRef, pQueryRef);
+        bValid = lcl_createExcelQuery(pDoc, rPool, pParam, pDBRef, pQueryRef);
 
     nCount = pParam->GetEntryCount();
     if (bValid)
@@ -257,7 +260,7 @@ bool ScDBRangeBase::fillQueryEntries(ScQueryParamBase* pParam, const ScDBRangeBa
     if (!pDBRef)
         return false;
 
-    return lcl_fillQueryEntries(getDoc()->GetSharedStringPool(), pParam, pDBRef, this);
+    return lcl_fillQueryEntries(getDoc(), getDoc()->GetSharedStringPool(), pParam, pDBRef, this);
 }
 
 void ScDBRangeBase::fillQueryOptions(ScQueryParamBase* pParam)
diff --git a/sc/source/core/tool/interpr1.cxx b/sc/source/core/tool/interpr1.cxx
index 5340d622e9b4..388ed68f14ed 100644
--- a/sc/source/core/tool/interpr1.cxx
+++ b/sc/source/core/tool/interpr1.cxx
@@ -4263,7 +4263,7 @@ void ScInterpreter::ScColumns()
                 OUString aTabName;
                 ScComplexRefData aRef;
                 PopExternalDoubleRef( nFileId, aTabName, aRef);
-                ScRange aAbs = aRef.toAbs(aPos);
+                ScRange aAbs = aRef.toAbs(pDok, aPos);
                 nVal += static_cast<sal_uLong>(aAbs.aEnd.Tab() - aAbs.aStart.Tab() + 1) *
                     static_cast<sal_uLong>(aAbs.aEnd.Col() - aAbs.aStart.Col() + 1);
             }
@@ -4320,7 +4320,7 @@ void ScInterpreter::ScRows()
                 OUString aTabName;
                 ScComplexRefData aRef;
                 PopExternalDoubleRef( nFileId, aTabName, aRef);
-                ScRange aAbs = aRef.toAbs(aPos);
+                ScRange aAbs = aRef.toAbs(pDok, aPos);
                 nVal += static_cast<sal_uLong>(aAbs.aEnd.Tab() - aAbs.aStart.Tab() + 1) *
                     static_cast<sal_uLong>(aAbs.aEnd.Row() - aAbs.aStart.Row() + 1);
             }
@@ -4367,7 +4367,7 @@ void ScInterpreter::ScSheets()
                     OUString aTabName;
                     ScComplexRefData aRef;
                     PopExternalDoubleRef( nFileId, aTabName, aRef);
-                    ScRange aAbs = aRef.toAbs(aPos);
+                    ScRange aAbs = aRef.toAbs(pDok, aPos);
                     nVal += static_cast<sal_uLong>(aAbs.aEnd.Tab() - aAbs.aStart.Tab() + 1);
                 }
                 break;
@@ -4431,7 +4431,7 @@ void ScInterpreter::ScColumn()
                     OUString aTabName;
                     ScSingleRefData aRef;
                     PopExternalSingleRef( nFileId, aTabName, aRef );
-                    ScAddress aAbsRef = aRef.toAbs(aPos);
+                    ScAddress aAbsRef = aRef.toAbs(pDok, aPos);
                     nVal = static_cast<double>( aAbsRef.Col() + 1 );
                 }
                 break;
@@ -4455,7 +4455,7 @@ void ScInterpreter::ScColumn()
                         OUString aTabName;
                         ScComplexRefData aRef;
                         PopExternalDoubleRef( nFileId, aTabName, aRef );
-                        ScRange aAbs = aRef.toAbs(aPos);
+                        ScRange aAbs = aRef.toAbs(pDok, aPos);
                         nCol1 = aAbs.aStart.Col();
                         nCol2 = aAbs.aEnd.Col();
                     }
@@ -4535,7 +4535,7 @@ void ScInterpreter::ScRow()
                     OUString aTabName;
                     ScSingleRefData aRef;
                     PopExternalSingleRef( nFileId, aTabName, aRef );
-                    ScAddress aAbsRef = aRef.toAbs(aPos);
+                    ScAddress aAbsRef = aRef.toAbs(pDok, aPos);
                     nVal = static_cast<double>( aAbsRef.Row() + 1 );
                 }
                 break;
@@ -4558,7 +4558,7 @@ void ScInterpreter::ScRow()
                         OUString aTabName;
                         ScComplexRefData aRef;
                         PopExternalDoubleRef( nFileId, aTabName, aRef );
-                        ScRange aAbs = aRef.toAbs(aPos);
+                        ScRange aAbs = aRef.toAbs(pDok, aPos);
                         nRow1 = aAbs.aStart.Row();
                         nRow2 = aAbs.aEnd.Row();
                     }
@@ -8375,7 +8375,7 @@ void ScInterpreter::ScOffset()
             OUString aTabName;
             ScSingleRefData aRef;
             PopExternalSingleRef(nFileId, aTabName, aRef);
-            ScAddress aAbsRef = aRef.toAbs(aPos);
+            ScAddress aAbsRef = aRef.toAbs(pDok, aPos);
             nCol1 = aAbsRef.Col();
             nRow1 = aAbsRef.Row();
             nTab1 = aAbsRef.Tab();
@@ -8432,7 +8432,7 @@ void ScInterpreter::ScOffset()
             OUString aTabName;
             ScComplexRefData aRef;
             PopExternalDoubleRef(nFileId, aTabName, aRef);
-            ScRange aAbs = aRef.toAbs(aPos);
+            ScRange aAbs = aRef.toAbs(pDok, aPos);
             nCol1 = aAbs.aStart.Col();
             nRow1 = aAbs.aStart.Row();
             nTab1 = aAbs.aStart.Tab();
diff --git a/sc/source/core/tool/interpr2.cxx b/sc/source/core/tool/interpr2.cxx
index 43bf4f75da07..2dc9010e8eed 100644
--- a/sc/source/core/tool/interpr2.cxx
+++ b/sc/source/core/tool/interpr2.cxx
@@ -2465,12 +2465,12 @@ void ScInterpreter::ScIntersect()
         ScRefList* pRefList = xRes->GetRefList();
         for (const auto& rRef1 : *x1->GetRefList())
         {
-            const ScAddress& r11 = rRef1.Ref1.toAbs(aPos);
-            const ScAddress& r12 = rRef1.Ref2.toAbs(aPos);
+            const ScAddress& r11 = rRef1.Ref1.toAbs(pDok, aPos);
+            const ScAddress& r12 = rRef1.Ref2.toAbs(pDok, aPos);
             for (const auto& rRef2 : *x2->GetRefList())
             {
-                const ScAddress& r21 = rRef2.Ref1.toAbs(aPos);
-                const ScAddress& r22 = rRef2.Ref2.toAbs(aPos);
+                const ScAddress& r21 = rRef2.Ref1.toAbs(pDok, aPos);
+                const ScAddress& r22 = rRef2.Ref2.toAbs(pDok, aPos);
                 SCCOL nCol1 = ::std::max( r11.Col(), r21.Col());
                 SCROW nRow1 = ::std::max( r11.Row(), r21.Row());
                 SCTAB nTab1 = ::std::max( r11.Tab(), r21.Tab());
@@ -2516,14 +2516,14 @@ void ScInterpreter::ScIntersect()
                 case svDoubleRef:
                 {
                     {
-                        const ScAddress& r = pt[i]->GetSingleRef()->toAbs(aPos);
+                        const ScAddress& r = pt[i]->GetSingleRef()->toAbs(pDok, aPos);
                         nC1[i] = r.Col();
                         nR1[i] = r.Row();
                         nT1[i] = r.Tab();
                     }
                     if (sv[i] == svDoubleRef)
                     {
-                        const ScAddress& r = pt[i]->GetSingleRef2()->toAbs(aPos);
+                        const ScAddress& r = pt[i]->GetSingleRef2()->toAbs(pDok, aPos);
                         nC2[i] = r.Col();
                         nR2[i] = r.Row();
                         nT2[i] = r.Tab();
diff --git a/sc/source/core/tool/interpr4.cxx b/sc/source/core/tool/interpr4.cxx
index 8a7143fb94ef..1a371f3f3381 100644
--- a/sc/source/core/tool/interpr4.cxx
+++ b/sc/source/core/tool/interpr4.cxx
@@ -878,12 +878,12 @@ void ScInterpreter::SingleRefToVars( const ScSingleRefData & rRef,
     else
         rTab = rRef.Tab();
 
-    if( !ValidCol( rCol) || rRef.IsColDeleted() )
+    if( !pDok->ValidCol( rCol) || rRef.IsColDeleted() )
     {
         SetError( FormulaError::NoRef );
         rCol = 0;
     }
-    if( !ValidRow( rRow) || rRef.IsRowDeleted() )
+    if( !pDok->ValidRow( rRow) || rRef.IsRowDeleted() )
     {
         SetError( FormulaError::NoRef );
         rRow = 0;
@@ -1178,7 +1178,7 @@ void ScInterpreter::PopExternalSingleRef(
         return;
     }
 
-    ScAddress aAddr = rRef.toAbs(aPos);
+    ScAddress aAddr = rRef.toAbs(pDok, aPos);
     ScExternalRefCache::CellFormat aFmt;
     ScExternalRefCache::TokenRef xNew = pRefMgr->getSingleRefToken(
         rFileId, rTabName, aAddr, &aPos, nullptr, &aFmt);
@@ -1279,8 +1279,8 @@ void ScInterpreter::GetExternalDoubleRef(
     }
 
     ScComplexRefData aData(rData);
-    ScRange aRange = aData.toAbs(aPos);
-    if (!ValidColRow(aRange.aStart.Col(), aRange.aStart.Row()) || !ValidColRow(aRange.aEnd.Col(), aRange.aEnd.Row()))
+    ScRange aRange = aData.toAbs(pDok, aPos);
+    if (!pDok->ValidColRow(aRange.aStart.Col(), aRange.aStart.Row()) || !pDok->ValidColRow(aRange.aEnd.Col(), aRange.aEnd.Row()))
     {
         SetError(FormulaError::NoRef);
         return;
@@ -1858,7 +1858,7 @@ void ScInterpreter::PushSingleRef( const ScRefAddress& rRef )
     if (!IfErrorPushError())
     {
         ScSingleRefData aRef;
-        aRef.InitFromRefAddress( rRef, aPos);
+        aRef.InitFromRefAddress( pDok, rRef, aPos);
         PushTempTokenWithoutError( new ScSingleRefToken( pDok->GetSheetLimits(), aRef ) );
     }
 }
@@ -1868,7 +1868,7 @@ void ScInterpreter::PushDoubleRef( const ScRefAddress& rRef1, const ScRefAddress
     if (!IfErrorPushError())
     {
         ScComplexRefData aRef;
-        aRef.InitFromRefAddresses( rRef1, rRef2, aPos);
+        aRef.InitFromRefAddresses( pDok, rRef1, rRef2, aPos);
         PushTempTokenWithoutError( new ScDoubleRefToken( pDok->GetSheetLimits(), aRef ) );
     }
 }
@@ -3665,7 +3665,7 @@ void ScInterpreter::ScDBArea()
         ScRange aRange;
         pDBData->GetArea(aRange);
         aRange.aEnd.SetTab(aRange.aStart.Tab());
-        aRefData.SetRange(aRange, aPos);
+        aRefData.SetRange(pDok->GetSheetLimits(), aRange, aPos);

... etc. - the rest is truncated


More information about the Libreoffice-commits mailing list