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>
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() :
50 ScenarioModel::ScenarioModel() :
57 Scenario::Scenario( const WorkbookHelper
& rHelper
, sal_Int16 nSheet
, bool bIsActive
) :
58 WorkbookHelper( rHelper
),
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( aModel
.maPos
, rAttribs
.getString( XML_r
, OUString() ), mnSheet
);
77 aModel
.maValue
= rAttribs
.getXString( XML_val
, OUString() );
78 aModel
.mnNumFmtId
= rAttribs
.getInteger( XML_numFmtId
, 0 );
79 aModel
.mbDeleted
= rAttribs
.getBool( XML_deleted
, false );
80 maCells
.push_back( aModel
);
83 void Scenario::importScenario( SequenceInputStream
& rStrm
)
85 rStrm
.skip( 2 ); // cell count
86 // two longs instead of flag field
87 maModel
.mbLocked
= rStrm
.readInt32() != 0;
88 maModel
.mbHidden
= rStrm
.readInt32() != 0;
89 rStrm
>> maModel
.maName
>> maModel
.maComment
>> maModel
.maUser
;
92 void Scenario::importInputCells( SequenceInputStream
& rStrm
)
94 // TODO: where is the deleted flag?
95 ScenarioCellModel aModel
;
99 aModel
.mnNumFmtId
= rStrm
.readuInt16();
100 rStrm
>> aModel
.maValue
;
101 AddressConverter::convertToCellAddressUnchecked( aModel
.maPos
, aPos
, mnSheet
);
102 maCells
.push_back( aModel
);
105 void Scenario::finalizeImport()
107 AddressConverter
& rAddrConv
= getAddressConverter();
109 for( const auto& rCell
: maCells
)
110 if( !rCell
.mbDeleted
&& rAddrConv
.checkCellAddress( rCell
.maPos
, true ) )
111 aRanges
.push_back( ScRange(rCell
.maPos
, rCell
.maPos
) );
113 if( !aRanges
.empty() && !maModel
.maName
.isEmpty() ) try
115 /* Find an unused name for the scenario (Calc stores scenario data in
116 hidden sheets named after the scenario following the base sheet). */
117 Reference
< XNameAccess
> xSheetsNA( getDocument()->getSheets(), UNO_QUERY_THROW
);
118 OUString aScenName
= ContainerHelper::getUnusedName( xSheetsNA
, maModel
.maName
, '_' );
120 // create the new scenario sheet
121 Reference
< XScenariosSupplier
> xScenariosSupp( getSheetFromDoc( mnSheet
), UNO_QUERY_THROW
);
122 Reference
< XScenarios
> xScenarios( xScenariosSupp
->getScenarios(), UNO_SET_THROW
);
123 xScenarios
->addNewByName( aScenName
, AddressConverter::toApiSequence(aRanges
), maModel
.maComment
);
125 // write scenario cell values
126 Reference
< XSpreadsheet
> xSheet( getSheetFromDoc( aScenName
), UNO_SET_THROW
);
127 for( const auto& rCell
: maCells
)
129 if( !rCell
.mbDeleted
) try
131 // use XCell::setFormula to auto-detect values and strings
132 Reference
< XCell
> xCell( xSheet
->getCellByPosition( rCell
.maPos
.Col(), rCell
.maPos
.Row() ), UNO_SET_THROW
);
133 xCell
->setFormula( rCell
.maValue
);
140 // scenario properties
141 PropertySet
aPropSet( xScenarios
->getByName( aScenName
) );
142 aPropSet
.setProperty( PROP_IsActive
, maModel
.mbActive
);
143 aPropSet
.setProperty( PROP_CopyBack
, false );
144 aPropSet
.setProperty( PROP_CopyStyles
, false );
145 aPropSet
.setProperty( PROP_CopyFormulas
, false );
146 aPropSet
.setProperty( PROP_Protected
, maModel
.mbLocked
);
147 // #112621# do not show/print scenario border
148 aPropSet
.setProperty( PROP_ShowBorder
, false );
149 aPropSet
.setProperty( PROP_PrintBorder
, false );
156 SheetScenariosModel::SheetScenariosModel() :
162 SheetScenarios::SheetScenarios( const WorkbookHelper
& rHelper
, sal_Int16 nSheet
) :
163 WorkbookHelper( rHelper
),
168 void SheetScenarios::importScenarios( const AttributeList
& rAttribs
)
170 maModel
.mnCurrent
= rAttribs
.getInteger( XML_current
, 0 );
171 maModel
.mnShown
= rAttribs
.getInteger( XML_show
, 0 );
174 void SheetScenarios::importScenarios( SequenceInputStream
& rStrm
)
176 maModel
.mnCurrent
= rStrm
.readuInt16();
177 maModel
.mnShown
= rStrm
.readuInt16();
180 Scenario
& SheetScenarios::createScenario()
182 bool bIsActive
= maScenarios
.size() == static_cast<sal_uInt32
>(maModel
.mnShown
);
183 ScenarioVector::value_type
xScenario( new Scenario( *this, mnSheet
, bIsActive
) );
184 maScenarios
.push_back( xScenario
);
188 void SheetScenarios::finalizeImport()
190 maScenarios
.forEachMem( &Scenario::finalizeImport
);
193 ScenarioBuffer::ScenarioBuffer( const WorkbookHelper
& rHelper
) :
194 WorkbookHelper( rHelper
)
198 SheetScenarios
& ScenarioBuffer::createSheetScenarios( sal_Int16 nSheet
)
200 SheetScenariosMap::mapped_type
& rxSheetScens
= maSheetScenarios
[ nSheet
];
202 rxSheetScens
.reset( new SheetScenarios( *this, nSheet
) );
203 return *rxSheetScens
;
206 void ScenarioBuffer::finalizeImport()
208 maSheetScenarios
.forEachMem( &SheetScenarios::finalizeImport
);
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */