Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / oox / excelvbaproject.cxx
blobc77a52d689d6736927c79e73e4ed7bc60d82ceb1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <excelvbaproject.hxx>
22 #include <vector>
23 #include <set>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/container/XEnumeration.hpp>
26 #include <com/sun/star/container/XEnumerationAccess.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 <oox/helper/propertyset.hxx>
31 #include <oox/token/properties.hxx>
33 namespace oox {
34 namespace xls {
36 using namespace ::com::sun::star::container;
37 using namespace ::com::sun::star::frame;
38 using namespace ::com::sun::star::script;
39 using namespace ::com::sun::star::sheet;
40 using namespace ::com::sun::star::uno;
42 ExcelVbaProject::ExcelVbaProject( const Reference< XComponentContext >& rxContext, const Reference< XSpreadsheetDocument >& rxDocument ) :
43 ::oox::ole::VbaProject( rxContext, Reference< XModel >( rxDocument, UNO_QUERY ), "Calc" ),
44 mxDocument( rxDocument )
48 // protected ------------------------------------------------------------------
50 namespace {
52 struct SheetCodeNameInfo
54 PropertySet maSheetProps; /// Property set of the sheet without codename.
55 OUString const maPrefix; /// Prefix for the codename to be generated.
57 explicit SheetCodeNameInfo( const PropertySet& rSheetProps, const OUString& rPrefix ) :
58 maSheetProps( rSheetProps ), maPrefix( rPrefix ) {}
61 } // namespace
63 void ExcelVbaProject::prepareImport()
65 /* Check if the sheets have imported codenames. Generate new unused
66 codenames if not. */
67 if( mxDocument.is() ) try
69 // collect existing codenames (do not use them when creating new codenames)
70 ::std::set< OUString > aUsedCodeNames;
72 // collect sheets without codenames
73 ::std::vector< SheetCodeNameInfo > aCodeNameInfos;
75 // iterate over all imported sheets
76 Reference< XEnumerationAccess > xSheetsEA( mxDocument->getSheets(), UNO_QUERY_THROW );
77 Reference< XEnumeration > xSheetsEnum( xSheetsEA->createEnumeration(), UNO_SET_THROW );
78 // own try/catch for every sheet
79 while( xSheetsEnum->hasMoreElements() ) try
81 PropertySet aSheetProp( xSheetsEnum->nextElement() );
82 OUString aCodeName;
83 aSheetProp.getProperty( aCodeName, PROP_CodeName );
84 if( !aCodeName.isEmpty() )
86 aUsedCodeNames.insert( aCodeName );
88 else
90 // TODO: once we have chart sheets we need a switch/case on sheet type ('SheetNNN' vs. 'ChartNNN')
91 aCodeNameInfos.emplace_back( aSheetProp, "Sheet" );
94 catch( Exception& )
98 // create new codenames if sheets do not have one
99 for (auto & codeName : aCodeNameInfos)
101 // search for an unused codename
102 sal_Int32 nCounter = 1;
103 OUString aCodeName;
106 aCodeName = codeName.maPrefix + OUString::number( nCounter++ );
108 while( aUsedCodeNames.count( aCodeName ) > 0 );
109 aUsedCodeNames.insert( aCodeName );
111 // set codename at sheet
112 codeName.maSheetProps.setProperty( PROP_CodeName, aCodeName );
114 // tell base class to create a dummy module
115 addDummyModule( aCodeName, ModuleType::DOCUMENT );
118 catch( Exception& )
123 } // namespace xls
124 } // namespace oox
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */