xref: /AOO41X/main/oox/source/xls/excelvbaproject.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "oox/xls/excelvbaproject.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <list>
31*cdf0e10cSrcweir #include <set>
32*cdf0e10cSrcweir #include <com/sun/star/container/XEnumeration.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/document/XEventsSupplier.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/script/ModuleType.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
38*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
39*cdf0e10cSrcweir #include "oox/helper/helper.hxx"
40*cdf0e10cSrcweir #include "oox/helper/propertyset.hxx"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir namespace oox {
43*cdf0e10cSrcweir namespace xls {
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir // ============================================================================
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir using namespace ::com::sun::star::container;
48*cdf0e10cSrcweir using namespace ::com::sun::star::document;
49*cdf0e10cSrcweir using namespace ::com::sun::star::frame;
50*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
51*cdf0e10cSrcweir using namespace ::com::sun::star::script;
52*cdf0e10cSrcweir using namespace ::com::sun::star::sheet;
53*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir using ::rtl::OUString;
56*cdf0e10cSrcweir using ::rtl::OUStringBuffer;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir // ============================================================================
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir ExcelVbaProject::ExcelVbaProject( const Reference< XComponentContext >& rxContext, const Reference< XSpreadsheetDocument >& rxDocument ) :
61*cdf0e10cSrcweir     ::oox::ole::VbaProject( rxContext, Reference< XModel >( rxDocument, UNO_QUERY ), CREATE_OUSTRING( "Calc" ) ),
62*cdf0e10cSrcweir     mxDocument( rxDocument )
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir // protected ------------------------------------------------------------------
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir namespace {
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir struct SheetCodeNameInfo
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir     PropertySet         maSheetProps;       /// Property set of the sheet without codename.
73*cdf0e10cSrcweir     OUString            maPrefix;           /// Prefix for the codename to be generated.
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     inline explicit     SheetCodeNameInfo( PropertySet& rSheetProps, const OUString& rPrefix ) :
76*cdf0e10cSrcweir                             maSheetProps( rSheetProps ), maPrefix( rPrefix ) {}
77*cdf0e10cSrcweir };
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir typedef ::std::set< OUString >              CodeNameSet;
80*cdf0e10cSrcweir typedef ::std::list< SheetCodeNameInfo >    SheetCodeNameInfoList;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir } // namespace
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir void ExcelVbaProject::prepareImport()
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir     /*  Check if the sheets have imported codenames. Generate new unused
87*cdf0e10cSrcweir         codenames if not. */
88*cdf0e10cSrcweir     if( mxDocument.is() ) try
89*cdf0e10cSrcweir     {
90*cdf0e10cSrcweir         // collect existing codenames (do not use them when creating new codenames)
91*cdf0e10cSrcweir         CodeNameSet aUsedCodeNames;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir         // collect sheets without codenames
94*cdf0e10cSrcweir         SheetCodeNameInfoList aCodeNameInfos;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir         // iterate over all imported sheets
97*cdf0e10cSrcweir         Reference< XEnumerationAccess > xSheetsEA( mxDocument->getSheets(), UNO_QUERY_THROW );
98*cdf0e10cSrcweir         Reference< XEnumeration > xSheetsEnum( xSheetsEA->createEnumeration(), UNO_SET_THROW );
99*cdf0e10cSrcweir         // own try/catch for every sheet
100*cdf0e10cSrcweir         while( xSheetsEnum->hasMoreElements() ) try
101*cdf0e10cSrcweir         {
102*cdf0e10cSrcweir             PropertySet aSheetProp( xSheetsEnum->nextElement() );
103*cdf0e10cSrcweir             OUString aCodeName;
104*cdf0e10cSrcweir             aSheetProp.getProperty( aCodeName, PROP_CodeName );
105*cdf0e10cSrcweir             if( aCodeName.getLength() > 0 )
106*cdf0e10cSrcweir             {
107*cdf0e10cSrcweir                 aUsedCodeNames.insert( aCodeName );
108*cdf0e10cSrcweir             }
109*cdf0e10cSrcweir             else
110*cdf0e10cSrcweir             {
111*cdf0e10cSrcweir                 // TODO: once we have chart sheets we need a switch/case on sheet type ('SheetNNN' vs. 'ChartNNN')
112*cdf0e10cSrcweir                 aCodeNameInfos.push_back( SheetCodeNameInfo( aSheetProp, CREATE_OUSTRING( "Sheet" ) ) );
113*cdf0e10cSrcweir             }
114*cdf0e10cSrcweir         }
115*cdf0e10cSrcweir         catch( Exception& )
116*cdf0e10cSrcweir         {
117*cdf0e10cSrcweir         }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir         // create new codenames if sheets do not have one
120*cdf0e10cSrcweir         for( SheetCodeNameInfoList::iterator aIt = aCodeNameInfos.begin(), aEnd = aCodeNameInfos.end(); aIt != aEnd; ++aIt )
121*cdf0e10cSrcweir         {
122*cdf0e10cSrcweir             // search for an unused codename
123*cdf0e10cSrcweir             sal_Int32 nCounter = 1;
124*cdf0e10cSrcweir             OUString aCodeName;
125*cdf0e10cSrcweir             do
126*cdf0e10cSrcweir             {
127*cdf0e10cSrcweir                 aCodeName = OUStringBuffer( aIt->maPrefix ).append( nCounter++ ).makeStringAndClear();
128*cdf0e10cSrcweir             }
129*cdf0e10cSrcweir             while( aUsedCodeNames.count( aCodeName ) > 0 );
130*cdf0e10cSrcweir             aUsedCodeNames.insert( aCodeName );
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir             // set codename at sheet
133*cdf0e10cSrcweir             aIt->maSheetProps.setProperty( PROP_CodeName, aCodeName );
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir             // tell base class to create a dummy module
136*cdf0e10cSrcweir             addDummyModule( aCodeName, ModuleType::DOCUMENT );
137*cdf0e10cSrcweir         }
138*cdf0e10cSrcweir     }
139*cdf0e10cSrcweir     catch( Exception& )
140*cdf0e10cSrcweir     {
141*cdf0e10cSrcweir     }
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir // ============================================================================
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir } // namespace xls
147*cdf0e10cSrcweir } // namespace oox
148