Resolves: tdf#115046 don't restore a sheet-local DBData from temporary

And don't clear AutoFilter button flags in the new temporary range.

Change-Id: I273de1e04632ac99c267523b2843665d257fd361
diff --git a/sc/inc/dbdata.hxx b/sc/inc/dbdata.hxx
index 2fea51e..061ef51 100644
--- a/sc/inc/dbdata.hxx
+++ b/sc/inc/dbdata.hxx
@@ -122,6 +122,7 @@ public:

    const OUString& GetName() const { return aName; }
    const OUString& GetUpperName() const { return aUpper; }
    SCTAB       GetTab() const                  { return nTable; }
    void        GetArea(SCTAB& rTab, SCCOL& rCol1, SCROW& rRow1, SCCOL& rCol2, SCROW& rRow2) const;
    SC_DLLPUBLIC void GetArea(ScRange& rRange) const;
    void        SetArea(SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2);
diff --git a/sc/source/ui/docshell/docsh5.cxx b/sc/source/ui/docshell/docsh5.cxx
index 6921f0b..d55f0f2 100644
--- a/sc/source/ui/docshell/docsh5.cxx
+++ b/sc/source/ui/docshell/docsh5.cxx
@@ -222,8 +222,10 @@ ScDBData* ScDocShell::GetDBData( const ScRange& rMarked, ScGetDBMode eMode, ScGe
            // other ranges, use the document global temporary anonymous range
            // instead. But, if AutoFilter is to be toggled then do use the
            // sheet-local DB range.
            bool bSheetLocal = true;
            if (eMode != SC_DB_AUTOFILTER && pNoNameData->HasAutoFilter())
            {
                bSheetLocal = false;
                pNoNameData = aDocument.GetAnonymousDBData();
                if (!pNoNameData)
                {
@@ -231,14 +233,24 @@ ScDBData* ScDocShell::GetDBData( const ScRange& rMarked, ScGetDBMode eMode, ScGe
                            nTab, nStartCol, nStartRow, nEndCol, nEndRow, true, bHasHeader);
                    aDocument.SetAnonymousDBData( pNoNameData);
                }
                // ScDocShell::CancelAutoDBRange() would restore the
                // sheet-local anonymous DBData from pOldAutoDBRange, unset so
                // that won't happen with data of a previous sheet-local
                // DBData.
                delete pOldAutoDBRange;
                pOldAutoDBRange = nullptr;
            }

            if ( !pOldAutoDBRange )
            else if (!pOldAutoDBRange)
            {
                // store the old unnamed database range with its settings for undo
                // (store at the first change, get the state before all changes)
                pOldAutoDBRange = new ScDBData( *pNoNameData );
            }
            else if (pOldAutoDBRange->GetTab() != pNoNameData->GetTab())
            {
                // Different sheet-local unnamed DB range than the previous one.
                *pOldAutoDBRange = *pNoNameData;
            }

            SCCOL nOldX1;                                   // take old range away cleanly
            SCROW nOldY1;                                   //! (UNDO ???)
@@ -255,7 +267,10 @@ ScDBData* ScDocShell::GetDBData( const ScRange& rMarked, ScGetDBMode eMode, ScGe
                    nStartCol <= nOldY2 && nOldY1 <= nEndCol)
                bHasHeader = true;

            DBAreaDeleted( nOldTab, nOldX1, nOldY1, nOldX2 );
            // Remove AutoFilter button flags only for sheet-local DB range,
            // not if a temporary is used.
            if (bSheetLocal)
                DBAreaDeleted( nOldTab, nOldX1, nOldY1, nOldX2 );

            pNoNameData->SetSortParam( ScSortParam() );             // reset parameter
            pNoNameData->SetQueryParam( ScQueryParam() );