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