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 #include "oox/xls/excelvbaproject.hxx"
23
24 #include <list>
25 #include <set>
26 #include <com/sun/star/container/XEnumeration.hpp>
27 #include <com/sun/star/container/XEnumerationAccess.hpp>
28 #include <com/sun/star/document/XEventsSupplier.hpp>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <com/sun/star/script/ModuleType.hpp>
31 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
32 #include <rtl/ustrbuf.hxx>
33 #include "oox/helper/helper.hxx"
34 #include "oox/helper/propertyset.hxx"
35
36 namespace oox {
37 namespace xls {
38
39 // ============================================================================
40
41 using namespace ::com::sun::star::container;
42 using namespace ::com::sun::star::document;
43 using namespace ::com::sun::star::frame;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::script;
46 using namespace ::com::sun::star::sheet;
47 using namespace ::com::sun::star::uno;
48
49 using ::rtl::OUString;
50 using ::rtl::OUStringBuffer;
51
52 // ============================================================================
53
ExcelVbaProject(const Reference<XComponentContext> & rxContext,const Reference<XSpreadsheetDocument> & rxDocument)54 ExcelVbaProject::ExcelVbaProject( const Reference< XComponentContext >& rxContext, const Reference< XSpreadsheetDocument >& rxDocument ) :
55 ::oox::ole::VbaProject( rxContext, Reference< XModel >( rxDocument, UNO_QUERY ), CREATE_OUSTRING( "Calc" ) ),
56 mxDocument( rxDocument )
57 {
58 }
59
60 // protected ------------------------------------------------------------------
61
62 namespace {
63
64 struct SheetCodeNameInfo
65 {
66 PropertySet maSheetProps; // Property set of the sheet without codename.
67 OUString maPrefix; // Prefix for the codename to be generated.
68
SheetCodeNameInfooox::xls::__anon39e8f0830111::SheetCodeNameInfo69 inline explicit SheetCodeNameInfo( PropertySet& rSheetProps, const OUString& rPrefix ) :
70 maSheetProps( rSheetProps ), maPrefix( rPrefix ) {}
71 };
72
73 typedef ::std::set< OUString > CodeNameSet;
74 typedef ::std::list< SheetCodeNameInfo > SheetCodeNameInfoList;
75
76 } // namespace
77
prepareImport()78 void ExcelVbaProject::prepareImport()
79 {
80 /* Check if the sheets have imported codenames. Generate new unused
81 codenames if not. */
82 if( mxDocument.is() ) try
83 {
84 // collect existing codenames (do not use them when creating new codenames)
85 CodeNameSet aUsedCodeNames;
86
87 // collect sheets without codenames
88 SheetCodeNameInfoList aCodeNameInfos;
89
90 // iterate over all imported sheets
91 Reference< XEnumerationAccess > xSheetsEA( mxDocument->getSheets(), UNO_QUERY_THROW );
92 Reference< XEnumeration > xSheetsEnum( xSheetsEA->createEnumeration(), UNO_SET_THROW );
93 // own try/catch for every sheet
94 while( xSheetsEnum->hasMoreElements() ) try
95 {
96 PropertySet aSheetProp( xSheetsEnum->nextElement() );
97 OUString aCodeName;
98 aSheetProp.getProperty( aCodeName, PROP_CodeName );
99 if( aCodeName.getLength() > 0 )
100 {
101 aUsedCodeNames.insert( aCodeName );
102 }
103 else
104 {
105 // TODO: once we have chart sheets we need a switch/case on sheet type ('SheetNNN' vs. 'ChartNNN')
106 aCodeNameInfos.push_back( SheetCodeNameInfo( aSheetProp, CREATE_OUSTRING( "Sheet" ) ) );
107 }
108 }
109 catch( Exception& )
110 {
111 }
112
113 // create new codenames if sheets do not have one
114 for( SheetCodeNameInfoList::iterator aIt = aCodeNameInfos.begin(), aEnd = aCodeNameInfos.end(); aIt != aEnd; ++aIt )
115 {
116 // search for an unused codename
117 sal_Int32 nCounter = 1;
118 OUString aCodeName;
119 do
120 {
121 aCodeName = OUStringBuffer( aIt->maPrefix ).append( nCounter++ ).makeStringAndClear();
122 }
123 while( aUsedCodeNames.count( aCodeName ) > 0 );
124 aUsedCodeNames.insert( aCodeName );
125
126 // set codename at sheet
127 aIt->maSheetProps.setProperty( PROP_CodeName, aCodeName );
128
129 // tell base class to create a dummy module
130 addDummyModule( aCodeName, ModuleType::DOCUMENT );
131 }
132 }
133 catch( Exception& )
134 {
135 }
136 }
137
138 // ============================================================================
139
140 } // namespace xls
141 } // namespace oox
142
143 /* vim: set noet sw=4 ts=4: */
144