Update git submodules
[LibreOffice.git] / sc / source / filter / oox / scenariobuffer.cxx
blob1fbf64cc1ff4ec302d9c7e6ec29d6f4001450f22
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 <scenariobuffer.hxx>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/sheet/XScenarios.hpp>
24 #include <com/sun/star/sheet/XScenariosSupplier.hpp>
25 #include <com/sun/star/sheet/XSpreadsheet.hpp>
26 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
27 #include <oox/helper/binaryinputstream.hxx>
28 #include <oox/helper/attributelist.hxx>
29 #include <oox/helper/containerhelper.hxx>
30 #include <oox/helper/propertyset.hxx>
31 #include <oox/token/properties.hxx>
32 #include <oox/token/tokens.hxx>
33 #include <addressconverter.hxx>
34 #include <biffhelper.hxx>
35 #include <docuno.hxx>
37 namespace oox::xls {
39 using namespace ::com::sun::star::container;
40 using namespace ::com::sun::star::sheet;
41 using namespace ::com::sun::star::table;
42 using namespace ::com::sun::star::uno;
44 ScenarioCellModel::ScenarioCellModel() :
45 mnNumFmtId( 0 ),
46 mbDeleted( false )
50 ScenarioModel::ScenarioModel() :
51 mbLocked( false ),
52 mbHidden( false ),
53 mbActive( false )
57 Scenario::Scenario( const WorkbookHelper& rHelper, sal_Int16 nSheet, bool bIsActive ) :
58 WorkbookHelper( rHelper ),
59 mnSheet( nSheet )
61 maModel.mbActive = bIsActive;
64 void Scenario::importScenario( const AttributeList& rAttribs )
66 maModel.maName = rAttribs.getXString( XML_name, OUString() );
67 maModel.maComment = rAttribs.getXString( XML_comment, OUString() );
68 maModel.maUser = rAttribs.getXString( XML_user, OUString() );
69 maModel.mbLocked = rAttribs.getBool( XML_locked, false );
70 maModel.mbHidden = rAttribs.getBool( XML_hidden, false );
73 void Scenario::importInputCells( const AttributeList& rAttribs )
75 ScenarioCellModel aModel;
76 AddressConverter::convertToCellAddressUnchecked(
77 aModel.maPos, rAttribs.getString(XML_r, OUString()), mnSheet, getScDocument());
78 aModel.maValue = rAttribs.getXString( XML_val, OUString() );
79 aModel.mnNumFmtId = rAttribs.getInteger( XML_numFmtId, 0 );
80 aModel.mbDeleted = rAttribs.getBool( XML_deleted, false );
81 maCells.push_back( aModel );
84 void Scenario::importScenario( SequenceInputStream& rStrm )
86 rStrm.skip( 2 ); // cell count
87 // two longs instead of flag field
88 maModel.mbLocked = rStrm.readInt32() != 0;
89 maModel.mbHidden = rStrm.readInt32() != 0;
90 rStrm >> maModel.maName >> maModel.maComment >> maModel.maUser;
93 void Scenario::importInputCells( SequenceInputStream& rStrm )
95 // TODO: where is the deleted flag?
96 ScenarioCellModel aModel;
97 BinAddress aPos;
98 rStrm >> aPos;
99 rStrm.skip( 8 );
100 aModel.mnNumFmtId = rStrm.readuInt16();
101 rStrm >> aModel.maValue;
102 AddressConverter::convertToCellAddressUnchecked( aModel.maPos, aPos, mnSheet );
103 maCells.push_back( aModel );
106 void Scenario::finalizeImport()
108 AddressConverter& rAddrConv = getAddressConverter();
109 ScRangeList aRanges;
110 for( const auto& rCell : maCells )
111 if( !rCell.mbDeleted && rAddrConv.checkCellAddress( rCell.maPos, true ) )
112 aRanges.push_back( ScRange(rCell.maPos, rCell.maPos) );
114 if( aRanges.empty() || maModel.maName.isEmpty() )
115 return;
119 /* Find an unused name for the scenario (Calc stores scenario data in
120 hidden sheets named after the scenario following the base sheet). */
121 Reference< XNameAccess > xSheetsNA( getDocument()->getSheets(), UNO_QUERY_THROW );
122 OUString aScenName = ContainerHelper::getUnusedName( xSheetsNA, maModel.maName, '_' );
124 // create the new scenario sheet
125 Reference< XScenariosSupplier > xScenariosSupp( getSheetFromDoc( mnSheet ), UNO_QUERY_THROW );
126 Reference< XScenarios > xScenarios( xScenariosSupp->getScenarios(), UNO_SET_THROW );
127 xScenarios->addNewByName( aScenName, AddressConverter::toApiSequence(aRanges), maModel.maComment );
129 // write scenario cell values
130 Reference< XSpreadsheet > xSheet( getSheetFromDoc( aScenName ), UNO_SET_THROW );
131 for( const auto& rCell : maCells )
133 if( !rCell.mbDeleted ) try
135 // use XCell::setFormula to auto-detect values and strings
136 Reference< XCell > xCell( xSheet->getCellByPosition( rCell.maPos.Col(), rCell.maPos.Row() ), UNO_SET_THROW );
137 xCell->setFormula( rCell.maValue );
139 catch( Exception& )
144 // scenario properties
145 PropertySet aPropSet( xScenarios->getByName( aScenName ) );
146 aPropSet.setProperty( PROP_IsActive, maModel.mbActive );
147 aPropSet.setProperty( PROP_CopyBack, false );
148 aPropSet.setProperty( PROP_CopyStyles, false );
149 aPropSet.setProperty( PROP_CopyFormulas, false );
150 aPropSet.setProperty( PROP_Protected, maModel.mbLocked );
151 // #112621# do not show/print scenario border
152 aPropSet.setProperty( PROP_ShowBorder, false );
153 aPropSet.setProperty( PROP_PrintBorder, false );
155 catch( Exception& )
160 SheetScenariosModel::SheetScenariosModel() :
161 mnCurrent( 0 ),
162 mnShown( 0 )
166 SheetScenarios::SheetScenarios( const WorkbookHelper& rHelper, sal_Int16 nSheet ) :
167 WorkbookHelper( rHelper ),
168 mnSheet( nSheet )
172 void SheetScenarios::importScenarios( const AttributeList& rAttribs )
174 maModel.mnCurrent = rAttribs.getInteger( XML_current, 0 );
175 maModel.mnShown = rAttribs.getInteger( XML_show, 0 );
178 void SheetScenarios::importScenarios( SequenceInputStream& rStrm )
180 maModel.mnCurrent = rStrm.readuInt16();
181 maModel.mnShown = rStrm.readuInt16();
184 Scenario& SheetScenarios::createScenario()
186 bool bIsActive = maScenarios.size() == static_cast<sal_uInt32>(maModel.mnShown);
187 ScenarioVector::value_type xScenario = std::make_shared<Scenario>( *this, mnSheet, bIsActive );
188 maScenarios.push_back( xScenario );
189 return *xScenario;
192 void SheetScenarios::finalizeImport()
194 maScenarios.forEachMem( &Scenario::finalizeImport );
197 ScenarioBuffer::ScenarioBuffer( const WorkbookHelper& rHelper ) :
198 WorkbookHelper( rHelper )
202 SheetScenarios& ScenarioBuffer::createSheetScenarios( sal_Int16 nSheet )
204 SheetScenariosMap::mapped_type& rxSheetScens = maSheetScenarios[ nSheet ];
205 if( !rxSheetScens )
206 rxSheetScens = std::make_shared<SheetScenarios>( *this, nSheet );
207 return *rxSheetScens;
210 void ScenarioBuffer::finalizeImport()
212 maSheetScenarios.forEachMem( &SheetScenarios::finalizeImport );
215 } // namespace oox::xls
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */