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