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 <utility>
23#include <vector>
24#include <set>
25#include <com/sun/star/beans/XPropertySet.hpp>
26#include <com/sun/star/container/XEnumeration.hpp>
27#include <com/sun/star/container/XEnumerationAccess.hpp>
28#include <com/sun/star/frame/XModel.hpp>
29#include <com/sun/star/script/ModuleType.hpp>
30#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
32#include <oox/token/properties.hxx>
33
34namespace oox::xls {
35
36using namespace ::com::sun::star::container;
37using namespace ::com::sun::star::frame;
38using namespace ::com::sun::star::script;
39using namespace ::com::sun::star::sheet;
40using namespace ::com::sun::star::uno;
41
42ExcelVbaProject::ExcelVbaProject( const Reference< XComponentContext >& rxContext, const Reference< XSpreadsheetDocument >& rxDocument ) :
43 ::oox::ole::VbaProject( rxContext, Reference< XModel >( rxDocument, UNO_QUERY ), u"Calc" ),
44 mxDocument( rxDocument )
45{
46}
47
48// protected ------------------------------------------------------------------
49
50namespace {
51
52struct SheetCodeNameInfo
53{
54 PropertySet maSheetProps;
55 OUString maPrefix;
56
57 explicit SheetCodeNameInfo( PropertySet aSheetProps, OUString aPrefix ) :
58 maSheetProps(std::move( aSheetProps )), maPrefix(std::move( aPrefix )) {}
59};
60
61} // namespace
62
64{
65 /* Check if the sheets have imported codenames. Generate new unused
66 codenames if not. */
67 if( !mxDocument.is() )
68 return;
69
70 try
71 {
72 // collect existing codenames (do not use them when creating new codenames)
73 ::std::set< OUString > aUsedCodeNames;
74
75 // collect sheets without codenames
76 ::std::vector< SheetCodeNameInfo > aCodeNameInfos;
77
78 // iterate over all imported sheets
79 Reference< XEnumerationAccess > xSheetsEA( mxDocument->getSheets(), UNO_QUERY_THROW );
80 Reference< XEnumeration > xSheetsEnum( xSheetsEA->createEnumeration(), UNO_SET_THROW );
81 // own try/catch for every sheet
82 while( xSheetsEnum->hasMoreElements() ) try
83 {
84 PropertySet aSheetProp( xSheetsEnum->nextElement() );
85 OUString aCodeName;
86 aSheetProp.getProperty( aCodeName, PROP_CodeName );
87 if( !aCodeName.isEmpty() )
88 {
89 aUsedCodeNames.insert( aCodeName );
90 }
91 else
92 {
93 // TODO: once we have chart sheets we need a switch/case on sheet type ('SheetNNN' vs. 'ChartNNN')
94 aCodeNameInfos.emplace_back( aSheetProp, "Sheet" );
95 }
96 }
97 catch( Exception& )
98 {
99 }
100
101 // create new codenames if sheets do not have one
102 for (auto & codeName : aCodeNameInfos)
103 {
104 // search for an unused codename
105 sal_Int32 nCounter = 1;
106 OUString aCodeName;
107 do
108 {
109 aCodeName = codeName.maPrefix + OUString::number( nCounter++ );
110 }
111 while( aUsedCodeNames.count( aCodeName ) > 0 );
112 aUsedCodeNames.insert( aCodeName );
113
114 // set codename at sheet
115 codeName.maSheetProps.setProperty( PROP_CodeName, aCodeName );
116
117 // tell base class to create a dummy module
118 addDummyModule( aCodeName, ModuleType::DOCUMENT );
119 }
120 }
121 catch( Exception& )
122 {
123 }
124}
125
126} // namespace oox::xls
127
128/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool getProperty(Type &orValue, sal_Int32 nPropId) const
void addDummyModule(const OUString &rName, sal_Int32 nType)
ExcelVbaProject(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::sheet::XSpreadsheetDocument > &rxDocument)
virtual void prepareImport() override
Adds dummy modules for sheets without imported code name.
css::uno::Reference< css::sheet::XSpreadsheetDocument > mxDocument
float u
PropertySet maSheetProps
OUString maPrefix
Property set of the sheet without codename.
@ Exception
Reference