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 <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>
38 using namespace ::com::sun::star::container
;
39 using namespace ::com::sun::star::sheet
;
40 using namespace ::com::sun::star::table
;
41 using namespace ::com::sun::star::uno
;
43 ScenarioCellModel::ScenarioCellModel() :
49 ScenarioModel::ScenarioModel() :
56 Scenario::Scenario( const WorkbookHelper
& rHelper
, sal_Int16 nSheet
, bool bIsActive
) :
57 WorkbookHelper( rHelper
),
60 maModel
.mbActive
= bIsActive
;
63 void Scenario::importScenario( const AttributeList
& rAttribs
)
65 maModel
.maName
= rAttribs
.getXString( XML_name
, OUString() );
66 maModel
.maComment
= rAttribs
.getXString( XML_comment
, OUString() );
67 maModel
.maUser
= rAttribs
.getXString( XML_user
, OUString() );
68 maModel
.mbLocked
= rAttribs
.getBool( XML_locked
, false );
69 maModel
.mbHidden
= rAttribs
.getBool( XML_hidden
, false );
72 void Scenario::importInputCells( const AttributeList
& rAttribs
)
74 ScenarioCellModel aModel
;
75 AddressConverter::convertToCellAddressUnchecked( aModel
.maPos
, rAttribs
.getString( XML_r
, OUString() ), mnSheet
);
76 aModel
.maValue
= rAttribs
.getXString( XML_val
, OUString() );
77 aModel
.mnNumFmtId
= rAttribs
.getInteger( XML_numFmtId
, 0 );
78 aModel
.mbDeleted
= rAttribs
.getBool( XML_deleted
, false );
79 maCells
.push_back( aModel
);
82 void Scenario::importScenario( SequenceInputStream
& rStrm
)
84 rStrm
.skip( 2 ); // cell count
85 // two longs instead of flag field
86 maModel
.mbLocked
= rStrm
.readInt32() != 0;
87 maModel
.mbHidden
= rStrm
.readInt32() != 0;
88 rStrm
>> maModel
.maName
>> maModel
.maComment
>> maModel
.maUser
;
91 void Scenario::importInputCells( SequenceInputStream
& rStrm
)
93 // TODO: where is the deleted flag?
94 ScenarioCellModel aModel
;
98 aModel
.mnNumFmtId
= rStrm
.readuInt16();
99 rStrm
>> aModel
.maValue
;
100 AddressConverter::convertToCellAddressUnchecked( aModel
.maPos
, aPos
, mnSheet
);
101 maCells
.push_back( aModel
);
104 void Scenario::finalizeImport()
106 AddressConverter
& rAddrConv
= getAddressConverter();
108 for( const auto& rCell
: maCells
)
109 if( !rCell
.mbDeleted
&& rAddrConv
.checkCellAddress( rCell
.maPos
, true ) )
110 aRanges
.push_back( ScRange(rCell
.maPos
, rCell
.maPos
) );
112 if( aRanges
.empty() || maModel
.maName
.isEmpty() )
117 /* Find an unused name for the scenario (Calc stores scenario data in
118 hidden sheets named after the scenario following the base sheet). */
119 Reference
< XNameAccess
> xSheetsNA( getDocument()->getSheets(), UNO_QUERY_THROW
);
120 OUString aScenName
= ContainerHelper::getUnusedName( xSheetsNA
, maModel
.maName
, '_' );
122 // create the new scenario sheet
123 Reference
< XScenariosSupplier
> xScenariosSupp( getSheetFromDoc( mnSheet
), UNO_QUERY_THROW
);
124 Reference
< XScenarios
> xScenarios( xScenariosSupp
->getScenarios(), UNO_SET_THROW
);
125 xScenarios
->addNewByName( aScenName
, AddressConverter::toApiSequence(aRanges
), maModel
.maComment
);
127 // write scenario cell values
128 Reference
< XSpreadsheet
> xSheet( getSheetFromDoc( aScenName
), UNO_SET_THROW
);
129 for( const auto& rCell
: maCells
)
131 if( !rCell
.mbDeleted
) try
133 // use XCell::setFormula to auto-detect values and strings
134 Reference
< XCell
> xCell( xSheet
->getCellByPosition( rCell
.maPos
.Col(), rCell
.maPos
.Row() ), UNO_SET_THROW
);
135 xCell
->setFormula( rCell
.maValue
);
142 // scenario properties
143 PropertySet
aPropSet( xScenarios
->getByName( aScenName
) );
144 aPropSet
.setProperty( PROP_IsActive
, maModel
.mbActive
);
145 aPropSet
.setProperty( PROP_CopyBack
, false );
146 aPropSet
.setProperty( PROP_CopyStyles
, false );
147 aPropSet
.setProperty( PROP_CopyFormulas
, false );
148 aPropSet
.setProperty( PROP_Protected
, maModel
.mbLocked
);
149 // #112621# do not show/print scenario border
150 aPropSet
.setProperty( PROP_ShowBorder
, false );
151 aPropSet
.setProperty( PROP_PrintBorder
, false );
158 SheetScenariosModel::SheetScenariosModel() :
164 SheetScenarios::SheetScenarios( const WorkbookHelper
& rHelper
, sal_Int16 nSheet
) :
165 WorkbookHelper( rHelper
),
170 void SheetScenarios::importScenarios( const AttributeList
& rAttribs
)
172 maModel
.mnCurrent
= rAttribs
.getInteger( XML_current
, 0 );
173 maModel
.mnShown
= rAttribs
.getInteger( XML_show
, 0 );
176 void SheetScenarios::importScenarios( SequenceInputStream
& rStrm
)
178 maModel
.mnCurrent
= rStrm
.readuInt16();
179 maModel
.mnShown
= rStrm
.readuInt16();
182 Scenario
& SheetScenarios::createScenario()
184 bool bIsActive
= maScenarios
.size() == static_cast<sal_uInt32
>(maModel
.mnShown
);
185 ScenarioVector::value_type xScenario
= std::make_shared
<Scenario
>( *this, mnSheet
, bIsActive
);
186 maScenarios
.push_back( xScenario
);
190 void SheetScenarios::finalizeImport()
192 maScenarios
.forEachMem( &Scenario::finalizeImport
);
195 ScenarioBuffer::ScenarioBuffer( const WorkbookHelper
& rHelper
) :
196 WorkbookHelper( rHelper
)
200 SheetScenarios
& ScenarioBuffer::createSheetScenarios( sal_Int16 nSheet
)
202 SheetScenariosMap::mapped_type
& rxSheetScens
= maSheetScenarios
[ nSheet
];
204 rxSheetScens
= std::make_shared
<SheetScenarios
>( *this, nSheet
);
205 return *rxSheetScens
;
208 void ScenarioBuffer::finalizeImport()
210 maSheetScenarios
.forEachMem( &SheetScenarios::finalizeImport
);
213 } // namespace oox::xls
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */