LibreOffice Module sc (master)  1
excelvbaproject.cxx
Go to the documentation of this file.
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 <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>
31 #include <oox/token/properties.hxx>
32 
33 namespace oox::xls {
34 
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;
40 
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 )
44 {
45 }
46 
47 // protected ------------------------------------------------------------------
48 
49 namespace {
50 
51 struct SheetCodeNameInfo
52 {
53  PropertySet maSheetProps;
54  OUString maPrefix;
55 
56  explicit SheetCodeNameInfo( const PropertySet& rSheetProps, const OUString& rPrefix ) :
57  maSheetProps( rSheetProps ), maPrefix( rPrefix ) {}
58 };
59 
60 } // namespace
61 
63 {
64  /* Check if the sheets have imported codenames. Generate new unused
65  codenames if not. */
66  if( !mxDocument.is() )
67  return;
68 
69  try
70  {
71  // collect existing codenames (do not use them when creating new codenames)
72  ::std::set< OUString > aUsedCodeNames;
73 
74  // collect sheets without codenames
75  ::std::vector< SheetCodeNameInfo > aCodeNameInfos;
76 
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
82  {
83  PropertySet aSheetProp( xSheetsEnum->nextElement() );
84  OUString aCodeName;
85  aSheetProp.getProperty( aCodeName, PROP_CodeName );
86  if( !aCodeName.isEmpty() )
87  {
88  aUsedCodeNames.insert( aCodeName );
89  }
90  else
91  {
92  // TODO: once we have chart sheets we need a switch/case on sheet type ('SheetNNN' vs. 'ChartNNN')
93  aCodeNameInfos.emplace_back( aSheetProp, "Sheet" );
94  }
95  }
96  catch( Exception& )
97  {
98  }
99 
100  // create new codenames if sheets do not have one
101  for (auto & codeName : aCodeNameInfos)
102  {
103  // search for an unused codename
104  sal_Int32 nCounter = 1;
105  OUString aCodeName;
106  do
107  {
108  aCodeName = codeName.maPrefix + OUString::number( nCounter++ );
109  }
110  while( aUsedCodeNames.count( aCodeName ) > 0 );
111  aUsedCodeNames.insert( aCodeName );
112 
113  // set codename at sheet
114  codeName.maSheetProps.setProperty( PROP_CodeName, aCodeName );
115 
116  // tell base class to create a dummy module
117  addDummyModule( aCodeName, ModuleType::DOCUMENT );
118  }
119  }
120  catch( Exception& )
121  {
122  }
123 }
124 
125 } // namespace oox::xls
126 
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool getProperty(Type &orValue, sal_Int32 nPropId) const
ExcelVbaProject(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::sheet::XSpreadsheetDocument > &rxDocument)
Reference
void addDummyModule(const OUString &rName, sal_Int32 nType)
PropertySet maSheetProps
virtual void prepareImport() override
Adds dummy modules for sheets without imported code name.
css::uno::Reference< css::sheet::XSpreadsheetDocument > mxDocument
OUString maPrefix
Property set of the sheet without codename.