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