xref: /trunk/main/sc/source/ui/vba/vbaquerytable.cxx (revision 7a7771fb4d20b857459c676939e2cc2f4dec9635)
1*80d08768SPedro Giffuni /**************************************************************
2*80d08768SPedro Giffuni  *
3*80d08768SPedro Giffuni  * Licensed to the Apache Software Foundation (ASF) under one
4*80d08768SPedro Giffuni  * or more contributor license agreements.  See the NOTICE file
5*80d08768SPedro Giffuni  * distributed with this work for additional information
6*80d08768SPedro Giffuni  * regarding copyright ownership.  The ASF licenses this file
7*80d08768SPedro Giffuni  * to you under the Apache License, Version 2.0 (the
8*80d08768SPedro Giffuni  * "License"); you may not use this file except in compliance
9*80d08768SPedro Giffuni  * with the License.  You may obtain a copy of the License at
10*80d08768SPedro Giffuni  *
11*80d08768SPedro Giffuni  *   http://www.apache.org/licenses/LICENSE-2.0
12*80d08768SPedro Giffuni  *
13*80d08768SPedro Giffuni  * Unless required by applicable law or agreed to in writing,
14*80d08768SPedro Giffuni  * software distributed under the License is distributed on an
15*80d08768SPedro Giffuni  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*80d08768SPedro Giffuni  * KIND, either express or implied.  See the License for the
17*80d08768SPedro Giffuni  * specific language governing permissions and limitations
18*80d08768SPedro Giffuni  * under the License.
19*80d08768SPedro Giffuni  *
20*80d08768SPedro Giffuni  *************************************************************/
21*80d08768SPedro Giffuni 
22*80d08768SPedro Giffuni #include "vbaquerytable.hxx"
23*80d08768SPedro Giffuni #include "helperdecl.hxx"
24*80d08768SPedro Giffuni #include "document.hxx"
25*80d08768SPedro Giffuni #include "docsh.hxx"
26*80d08768SPedro Giffuni #include "address.hxx"
27*80d08768SPedro Giffuni #include <tools/rtti.hxx>
28*80d08768SPedro Giffuni #include <sfx2/linkmgr.hxx>
29*80d08768SPedro Giffuni #include <sfx2/lnkbase.hxx>
30*80d08768SPedro Giffuni #include "arealink.hxx"
31*80d08768SPedro Giffuni #include "vbarange.hxx"
32*80d08768SPedro Giffuni 
33*80d08768SPedro Giffuni using namespace com::sun::star;
34*80d08768SPedro Giffuni using namespace com::sun::star::uno;
35*80d08768SPedro Giffuni 
ScVbaQueryTable(const css::uno::Sequence<css::uno::Any> &,const css::uno::Reference<css::uno::XComponentContext> & xContext)36*80d08768SPedro Giffuni ScVbaQueryTable::ScVbaQueryTable( const css::uno::Sequence< css::uno::Any >& /*args*/,
37*80d08768SPedro Giffuni         const css::uno::Reference< css::uno::XComponentContext >& xContext )
38*80d08768SPedro Giffuni :   ScVbaQueryTableImpl_BASE( xContext ),
39*80d08768SPedro Giffuni     m_pDocument( NULL ),
40*80d08768SPedro Giffuni     m_pParent( NULL )
41*80d08768SPedro Giffuni {
42*80d08768SPedro Giffuni }
43*80d08768SPedro Giffuni 
ScVbaQueryTable(const css::uno::Reference<ov::XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext,ScDocument * pDocument,ScVbaRange * pParent)44*80d08768SPedro Giffuni ScVbaQueryTable::ScVbaQueryTable( const css::uno::Reference< ov::XHelperInterface >& xParent,
45*80d08768SPedro Giffuni             const css::uno::Reference< css::uno::XComponentContext >& xContext,
46*80d08768SPedro Giffuni             ScDocument* pDocument,
47*80d08768SPedro Giffuni             ScVbaRange* pParent
48*80d08768SPedro Giffuni             )
49*80d08768SPedro Giffuni :   ScVbaQueryTableImpl_BASE( xParent, xContext ),
50*80d08768SPedro Giffuni     m_pDocument( pDocument ),
51*80d08768SPedro Giffuni     m_pParent( pParent )
52*80d08768SPedro Giffuni {
53*80d08768SPedro Giffuni }
54*80d08768SPedro Giffuni 
~ScVbaQueryTable()55*80d08768SPedro Giffuni ScVbaQueryTable::~ScVbaQueryTable()
56*80d08768SPedro Giffuni {
57*80d08768SPedro Giffuni }
58*80d08768SPedro Giffuni 
59*80d08768SPedro Giffuni sal_Bool SAL_CALL
Refresh(const css::uno::Any & aBackgroundQuery)60*80d08768SPedro Giffuni ScVbaQueryTable::Refresh( const css::uno::Any& aBackgroundQuery ) throw ( css::uno::RuntimeException )
61*80d08768SPedro Giffuni {
62*80d08768SPedro Giffuni     if ( !m_pParent || !m_pDocument )
63*80d08768SPedro Giffuni         return sal_False;
64*80d08768SPedro Giffuni 
65*80d08768SPedro Giffuni     // Get parent Info
66*80d08768SPedro Giffuni     sal_Int32 nRow = m_pParent->getRow();
67*80d08768SPedro Giffuni     sal_Int32 nClm = m_pParent->getColumn();
68*80d08768SPedro Giffuni     sal_Int16 nTab = m_pParent->getWorksheet()->getIndex() - 1; // The vba index begins from 1.
69*80d08768SPedro Giffuni     ScAddress crrRngAddr( nClm, nRow, nTab );
70*80d08768SPedro Giffuni 
71*80d08768SPedro Giffuni     // Get link info
72*80d08768SPedro Giffuni     sfx2::LinkManager* pLinkMng = m_pDocument->GetLinkManager();
73*80d08768SPedro Giffuni     const sfx2::SvBaseLinks& rLinks = pLinkMng->GetLinks();
74*80d08768SPedro Giffuni     sal_uInt16 nCount = rLinks.Count();
75*80d08768SPedro Giffuni 
76*80d08768SPedro Giffuni     bool bBackground = false;
77*80d08768SPedro Giffuni     if ( aBackgroundQuery.hasValue() )
78*80d08768SPedro Giffuni         aBackgroundQuery >>= bBackground;
79*80d08768SPedro Giffuni 
80*80d08768SPedro Giffuni     bool bFoundLink = false;
81*80d08768SPedro Giffuni     for ( sal_uInt16 i = 0; i < nCount; ++i )
82*80d08768SPedro Giffuni     {
83*80d08768SPedro Giffuni         sfx2::SvBaseLink* pBase = *rLinks[i];
84*80d08768SPedro Giffuni         if ( pBase->ISA( ScAreaLink ) )
85*80d08768SPedro Giffuni         {
86*80d08768SPedro Giffuni             ScAreaLink* pAreaLink = static_cast< ScAreaLink* >( pBase );
87*80d08768SPedro Giffuni             const ScRange& destRange = pAreaLink->GetDestArea();
88*80d08768SPedro Giffuni             if ( destRange.In( crrRngAddr ) )
89*80d08768SPedro Giffuni             {
90*80d08768SPedro Giffuni                 pBase->Update();
91*80d08768SPedro Giffuni                 bFoundLink = true;
92*80d08768SPedro Giffuni             }
93*80d08768SPedro Giffuni         }
94*80d08768SPedro Giffuni     }
95*80d08768SPedro Giffuni 
96*80d08768SPedro Giffuni     return bFoundLink ? sal_True : sal_False;
97*80d08768SPedro Giffuni }
98*80d08768SPedro Giffuni 
99*80d08768SPedro Giffuni void SAL_CALL
Delete()100*80d08768SPedro Giffuni ScVbaQueryTable::Delete() throw ( css::uno::RuntimeException )
101*80d08768SPedro Giffuni {
102*80d08768SPedro Giffuni     if ( !m_pParent || !m_pDocument )
103*80d08768SPedro Giffuni         return;
104*80d08768SPedro Giffuni 
105*80d08768SPedro Giffuni     sal_Int32 nRow = m_pParent->getRow();
106*80d08768SPedro Giffuni     sal_Int32 nClm = m_pParent->getColumn();
107*80d08768SPedro Giffuni     sal_Int16 nTab = m_pParent->getWorksheet()->getIndex() - 1;
108*80d08768SPedro Giffuni     ScAddress crrRngAddr( nClm, nRow, nTab );
109*80d08768SPedro Giffuni 
110*80d08768SPedro Giffuni     sfx2::LinkManager* pLinkMng = m_pDocument->GetLinkManager();
111*80d08768SPedro Giffuni     const sfx2::SvBaseLinks& rLinks = pLinkMng->GetLinks();
112*80d08768SPedro Giffuni     sal_uInt16 nCount = rLinks.Count();
113*80d08768SPedro Giffuni 
114*80d08768SPedro Giffuni     for ( sal_uInt16 i = 0; i < nCount; ++i )
115*80d08768SPedro Giffuni     {
116*80d08768SPedro Giffuni         sfx2::SvBaseLink* pBase = *rLinks[i];
117*80d08768SPedro Giffuni         if ( pBase->ISA( ScAreaLink ) )
118*80d08768SPedro Giffuni         {
119*80d08768SPedro Giffuni             ScAreaLink* pAreaLink = static_cast< ScAreaLink* >( pBase );
120*80d08768SPedro Giffuni             const ScRange& destRange = pAreaLink->GetDestArea();
121*80d08768SPedro Giffuni             if ( destRange.In( crrRngAddr ) )
122*80d08768SPedro Giffuni             {
123*80d08768SPedro Giffuni                 pLinkMng->Remove( pBase );
124*80d08768SPedro Giffuni                 break;
125*80d08768SPedro Giffuni             }
126*80d08768SPedro Giffuni         }
127*80d08768SPedro Giffuni     }
128*80d08768SPedro Giffuni }
129*80d08768SPedro Giffuni 
getServiceImplName()130*80d08768SPedro Giffuni rtl::OUString& ScVbaQueryTable::getServiceImplName()
131*80d08768SPedro Giffuni {
132*80d08768SPedro Giffuni     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaQueryTable") );
133*80d08768SPedro Giffuni     return sImplName;
134*80d08768SPedro Giffuni }
135*80d08768SPedro Giffuni 
getServiceNames()136*80d08768SPedro Giffuni css::uno::Sequence< rtl::OUString > ScVbaQueryTable::getServiceNames()
137*80d08768SPedro Giffuni {
138*80d08768SPedro Giffuni     static css::uno::Sequence< rtl::OUString > aServiceNames;
139*80d08768SPedro Giffuni     if ( aServiceNames.getLength() == 0 )
140*80d08768SPedro Giffuni     {
141*80d08768SPedro Giffuni         aServiceNames.realloc( 1 );
142*80d08768SPedro Giffuni         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.QueryTable") );
143*80d08768SPedro Giffuni     }
144*80d08768SPedro Giffuni     return aServiceNames;
145*80d08768SPedro Giffuni }
146