1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
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>
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
;
46 ExcelVbaProject::ExcelVbaProject( const Reference
< XComponentContext
>& rxContext
, const Reference
< XSpreadsheetDocument
>& rxDocument
) :
47 ::oox::ole::VbaProject( rxContext
, Reference
< XModel
>( rxDocument
, UNO_QUERY
), "Calc" ),
48 mxDocument( rxDocument
)
52 // protected ------------------------------------------------------------------
56 struct SheetCodeNameInfo
58 PropertySet maSheetProps
; /// Property set of the sheet without codename.
59 OUString maPrefix
; /// Prefix for the codename to be generated.
61 inline explicit SheetCodeNameInfo( PropertySet
& rSheetProps
, const OUString
& rPrefix
) :
62 maSheetProps( rSheetProps
), maPrefix( rPrefix
) {}
65 typedef ::std::set
< OUString
> CodeNameSet
;
66 typedef ::std::list
< SheetCodeNameInfo
> SheetCodeNameInfoList
;
70 void ExcelVbaProject::prepareImport()
72 /* Check if the sheets have imported codenames. Generate new unused
74 if( mxDocument
.is() ) try
76 // collect existing codenames (do not use them when creating new codenames)
77 CodeNameSet aUsedCodeNames
;
79 // collect sheets without codenames
80 SheetCodeNameInfoList aCodeNameInfos
;
82 // iterate over all imported sheets
83 Reference
< XEnumerationAccess
> xSheetsEA( mxDocument
->getSheets(), UNO_QUERY_THROW
);
84 Reference
< XEnumeration
> xSheetsEnum( xSheetsEA
->createEnumeration(), UNO_SET_THROW
);
85 // own try/catch for every sheet
86 while( xSheetsEnum
->hasMoreElements() ) try
88 PropertySet
aSheetProp( xSheetsEnum
->nextElement() );
90 aSheetProp
.getProperty( aCodeName
, PROP_CodeName
);
91 if( !aCodeName
.isEmpty() )
93 aUsedCodeNames
.insert( aCodeName
);
97 // TODO: once we have chart sheets we need a switch/case on sheet type ('SheetNNN' vs. 'ChartNNN')
98 aCodeNameInfos
.push_back( SheetCodeNameInfo( aSheetProp
, "Sheet" ) );
105 // create new codenames if sheets do not have one
106 for( SheetCodeNameInfoList::iterator aIt
= aCodeNameInfos
.begin(), aEnd
= aCodeNameInfos
.end(); aIt
!= aEnd
; ++aIt
)
108 // search for an unused codename
109 sal_Int32 nCounter
= 1;
113 aCodeName
= OUStringBuffer( aIt
->maPrefix
).append( nCounter
++ ).makeStringAndClear();
115 while( aUsedCodeNames
.count( aCodeName
) > 0 );
116 aUsedCodeNames
.insert( aCodeName
);
118 // set codename at sheet
119 aIt
->maSheetProps
.setProperty( PROP_CodeName
, aCodeName
);
121 // tell base class to create a dummy module
122 addDummyModule( aCodeName
, ModuleType::DOCUMENT
);
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */