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