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