xref: /aoo42x/main/oox/source/xls/excelvbaproject.cxx (revision ca5ec200)
1*ca5ec200SAndrew Rist /**************************************************************
2*ca5ec200SAndrew Rist  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/xls/excelvbaproject.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <list>
27cdf0e10cSrcweir #include <set>
28cdf0e10cSrcweir #include <com/sun/star/container/XEnumeration.hpp>
29cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
30cdf0e10cSrcweir #include <com/sun/star/document/XEventsSupplier.hpp>
31cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
32cdf0e10cSrcweir #include <com/sun/star/script/ModuleType.hpp>
33cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
34cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
35cdf0e10cSrcweir #include "oox/helper/helper.hxx"
36cdf0e10cSrcweir #include "oox/helper/propertyset.hxx"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir namespace oox {
39cdf0e10cSrcweir namespace xls {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir // ============================================================================
42cdf0e10cSrcweir 
43cdf0e10cSrcweir using namespace ::com::sun::star::container;
44cdf0e10cSrcweir using namespace ::com::sun::star::document;
45cdf0e10cSrcweir using namespace ::com::sun::star::frame;
46cdf0e10cSrcweir using namespace ::com::sun::star::lang;
47cdf0e10cSrcweir using namespace ::com::sun::star::script;
48cdf0e10cSrcweir using namespace ::com::sun::star::sheet;
49cdf0e10cSrcweir using namespace ::com::sun::star::uno;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir using ::rtl::OUString;
52cdf0e10cSrcweir using ::rtl::OUStringBuffer;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir // ============================================================================
55cdf0e10cSrcweir 
ExcelVbaProject(const Reference<XComponentContext> & rxContext,const Reference<XSpreadsheetDocument> & rxDocument)56cdf0e10cSrcweir ExcelVbaProject::ExcelVbaProject( const Reference< XComponentContext >& rxContext, const Reference< XSpreadsheetDocument >& rxDocument ) :
57cdf0e10cSrcweir     ::oox::ole::VbaProject( rxContext, Reference< XModel >( rxDocument, UNO_QUERY ), CREATE_OUSTRING( "Calc" ) ),
58cdf0e10cSrcweir     mxDocument( rxDocument )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir }
61cdf0e10cSrcweir 
62cdf0e10cSrcweir // protected ------------------------------------------------------------------
63cdf0e10cSrcweir 
64cdf0e10cSrcweir namespace {
65cdf0e10cSrcweir 
66cdf0e10cSrcweir struct SheetCodeNameInfo
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     PropertySet         maSheetProps;       /// Property set of the sheet without codename.
69cdf0e10cSrcweir     OUString            maPrefix;           /// Prefix for the codename to be generated.
70cdf0e10cSrcweir 
SheetCodeNameInfooox::xls::__anonccd851c40111::SheetCodeNameInfo71cdf0e10cSrcweir     inline explicit     SheetCodeNameInfo( PropertySet& rSheetProps, const OUString& rPrefix ) :
72cdf0e10cSrcweir                             maSheetProps( rSheetProps ), maPrefix( rPrefix ) {}
73cdf0e10cSrcweir };
74cdf0e10cSrcweir 
75cdf0e10cSrcweir typedef ::std::set< OUString >              CodeNameSet;
76cdf0e10cSrcweir typedef ::std::list< SheetCodeNameInfo >    SheetCodeNameInfoList;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir } // namespace
79cdf0e10cSrcweir 
prepareImport()80cdf0e10cSrcweir void ExcelVbaProject::prepareImport()
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     /*  Check if the sheets have imported codenames. Generate new unused
83cdf0e10cSrcweir         codenames if not. */
84cdf0e10cSrcweir     if( mxDocument.is() ) try
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         // collect existing codenames (do not use them when creating new codenames)
87cdf0e10cSrcweir         CodeNameSet aUsedCodeNames;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         // collect sheets without codenames
90cdf0e10cSrcweir         SheetCodeNameInfoList aCodeNameInfos;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         // iterate over all imported sheets
93cdf0e10cSrcweir         Reference< XEnumerationAccess > xSheetsEA( mxDocument->getSheets(), UNO_QUERY_THROW );
94cdf0e10cSrcweir         Reference< XEnumeration > xSheetsEnum( xSheetsEA->createEnumeration(), UNO_SET_THROW );
95cdf0e10cSrcweir         // own try/catch for every sheet
96cdf0e10cSrcweir         while( xSheetsEnum->hasMoreElements() ) try
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir             PropertySet aSheetProp( xSheetsEnum->nextElement() );
99cdf0e10cSrcweir             OUString aCodeName;
100cdf0e10cSrcweir             aSheetProp.getProperty( aCodeName, PROP_CodeName );
101cdf0e10cSrcweir             if( aCodeName.getLength() > 0 )
102cdf0e10cSrcweir             {
103cdf0e10cSrcweir                 aUsedCodeNames.insert( aCodeName );
104cdf0e10cSrcweir             }
105cdf0e10cSrcweir             else
106cdf0e10cSrcweir             {
107cdf0e10cSrcweir                 // TODO: once we have chart sheets we need a switch/case on sheet type ('SheetNNN' vs. 'ChartNNN')
108cdf0e10cSrcweir                 aCodeNameInfos.push_back( SheetCodeNameInfo( aSheetProp, CREATE_OUSTRING( "Sheet" ) ) );
109cdf0e10cSrcweir             }
110cdf0e10cSrcweir         }
111cdf0e10cSrcweir         catch( Exception& )
112cdf0e10cSrcweir         {
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir         // create new codenames if sheets do not have one
116cdf0e10cSrcweir         for( SheetCodeNameInfoList::iterator aIt = aCodeNameInfos.begin(), aEnd = aCodeNameInfos.end(); aIt != aEnd; ++aIt )
117cdf0e10cSrcweir         {
118cdf0e10cSrcweir             // search for an unused codename
119cdf0e10cSrcweir             sal_Int32 nCounter = 1;
120cdf0e10cSrcweir             OUString aCodeName;
121cdf0e10cSrcweir             do
122cdf0e10cSrcweir             {
123cdf0e10cSrcweir                 aCodeName = OUStringBuffer( aIt->maPrefix ).append( nCounter++ ).makeStringAndClear();
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir             while( aUsedCodeNames.count( aCodeName ) > 0 );
126cdf0e10cSrcweir             aUsedCodeNames.insert( aCodeName );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir             // set codename at sheet
129cdf0e10cSrcweir             aIt->maSheetProps.setProperty( PROP_CodeName, aCodeName );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir             // tell base class to create a dummy module
132cdf0e10cSrcweir             addDummyModule( aCodeName, ModuleType::DOCUMENT );
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir     catch( Exception& )
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir     }
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir // ============================================================================
141cdf0e10cSrcweir 
142cdf0e10cSrcweir } // namespace xls
143cdf0e10cSrcweir } // namespace oox
144