nss: upgrade to release 3.73
[LibreOffice.git] / sc / source / filter / oox / excelvbaproject.cxx
blobc3e48ba474c16526041321ff2e2b0ac1cfa2ab9f
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::xls {
35 using namespace ::com::sun::star::container;
36 using namespace ::com::sun::star::frame;
37 using namespace ::com::sun::star::script;
38 using namespace ::com::sun::star::sheet;
39 using namespace ::com::sun::star::uno;
41 ExcelVbaProject::ExcelVbaProject( const Reference< XComponentContext >& rxContext, const Reference< XSpreadsheetDocument >& rxDocument ) :
42 ::oox::ole::VbaProject( rxContext, Reference< XModel >( rxDocument, UNO_QUERY ), "Calc" ),
43 mxDocument( rxDocument )
47 // protected ------------------------------------------------------------------
49 namespace {
51 struct SheetCodeNameInfo
53 PropertySet maSheetProps; /// Property set of the sheet without codename.
54 OUString maPrefix; /// Prefix for the codename to be generated.
56 explicit SheetCodeNameInfo( const PropertySet& rSheetProps, const OUString& rPrefix ) :
57 maSheetProps( rSheetProps ), maPrefix( rPrefix ) {}
60 } // namespace
62 void ExcelVbaProject::prepareImport()
64 /* Check if the sheets have imported codenames. Generate new unused
65 codenames if not. */
66 if( !mxDocument.is() )
67 return;
69 try
71 // collect existing codenames (do not use them when creating new codenames)
72 ::std::set< OUString > aUsedCodeNames;
74 // collect sheets without codenames
75 ::std::vector< SheetCodeNameInfo > aCodeNameInfos;
77 // iterate over all imported sheets
78 Reference< XEnumerationAccess > xSheetsEA( mxDocument->getSheets(), UNO_QUERY_THROW );
79 Reference< XEnumeration > xSheetsEnum( xSheetsEA->createEnumeration(), UNO_SET_THROW );
80 // own try/catch for every sheet
81 while( xSheetsEnum->hasMoreElements() ) try
83 PropertySet aSheetProp( xSheetsEnum->nextElement() );
84 OUString aCodeName;
85 aSheetProp.getProperty( aCodeName, PROP_CodeName );
86 if( !aCodeName.isEmpty() )
88 aUsedCodeNames.insert( aCodeName );
90 else
92 // TODO: once we have chart sheets we need a switch/case on sheet type ('SheetNNN' vs. 'ChartNNN')
93 aCodeNameInfos.emplace_back( aSheetProp, "Sheet" );
96 catch( Exception& )
100 // create new codenames if sheets do not have one
101 for (auto & codeName : aCodeNameInfos)
103 // search for an unused codename
104 sal_Int32 nCounter = 1;
105 OUString aCodeName;
108 aCodeName = codeName.maPrefix + OUString::number( nCounter++ );
110 while( aUsedCodeNames.count( aCodeName ) > 0 );
111 aUsedCodeNames.insert( aCodeName );
113 // set codename at sheet
114 codeName.maSheetProps.setProperty( PROP_CodeName, aCodeName );
116 // tell base class to create a dummy module
117 addDummyModule( aCodeName, ModuleType::DOCUMENT );
120 catch( Exception& )
125 } // namespace oox::xls
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */