LibreOffice Module sc (master)  1
scenariobuffer.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 <scenariobuffer.hxx>
21 
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>
31 #include <oox/token/properties.hxx>
32 #include <oox/token/tokens.hxx>
33 #include <addressconverter.hxx>
34 #include <biffhelper.hxx>
35 
36 namespace oox::xls {
37 
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;
42 
44  mnNumFmtId( 0 ),
45  mbDeleted( false )
46 {
47 }
48 
50  mbLocked( false ),
51  mbHidden( false ),
52  mbActive( false )
53 {
54 }
55 
56 Scenario::Scenario( const WorkbookHelper& rHelper, sal_Int16 nSheet, bool bIsActive ) :
57  WorkbookHelper( rHelper ),
58  mnSheet( nSheet )
59 {
60  maModel.mbActive = bIsActive;
61 }
62 
63 void Scenario::importScenario( const AttributeList& rAttribs )
64 {
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 );
70 }
71 
73 {
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 );
80 }
81 
83 {
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;
89 }
90 
92 {
93  // TODO: where is the deleted flag?
94  ScenarioCellModel aModel;
95  BinAddress aPos;
96  rStrm >> aPos;
97  rStrm.skip( 8 );
98  aModel.mnNumFmtId = rStrm.readuInt16();
99  rStrm >> aModel.maValue;
101  maCells.push_back( aModel );
102 }
103 
105 {
106  AddressConverter& rAddrConv = getAddressConverter();
107  ScRangeList aRanges;
108  for( const auto& rCell : maCells )
109  if( !rCell.mbDeleted && rAddrConv.checkCellAddress( rCell.maPos, true ) )
110  aRanges.push_back( ScRange(rCell.maPos, rCell.maPos) );
111 
112  if( aRanges.empty() || maModel.maName.isEmpty() )
113  return;
114 
115  try
116  {
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, '_' );
121 
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 );
126 
127  // write scenario cell values
128  Reference< XSpreadsheet > xSheet( getSheetFromDoc( aScenName ), UNO_SET_THROW );
129  for( const auto& rCell : maCells )
130  {
131  if( !rCell.mbDeleted ) try
132  {
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 );
136  }
137  catch( Exception& )
138  {
139  }
140  }
141 
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 );
152  }
153  catch( Exception& )
154  {
155  }
156 }
157 
159  mnCurrent( 0 ),
160  mnShown( 0 )
161 {
162 }
163 
164 SheetScenarios::SheetScenarios( const WorkbookHelper& rHelper, sal_Int16 nSheet ) :
165  WorkbookHelper( rHelper ),
166  mnSheet( nSheet )
167 {
168 }
169 
171 {
172  maModel.mnCurrent = rAttribs.getInteger( XML_current, 0 );
173  maModel.mnShown = rAttribs.getInteger( XML_show, 0 );
174 }
175 
177 {
178  maModel.mnCurrent = rStrm.readuInt16();
179  maModel.mnShown = rStrm.readuInt16();
180 }
181 
183 {
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 );
187  return *xScenario;
188 }
189 
191 {
193 }
194 
196  WorkbookHelper( rHelper )
197 {
198 }
199 
201 {
202  SheetScenariosMap::mapped_type& rxSheetScens = maSheetScenarios[ nSheet ];
203  if( !rxSheetScens )
204  rxSheetScens = std::make_shared<SheetScenarios>( *this, nSheet );
205  return *rxSheetScens;
206 }
207 
209 {
211 }
212 
213 } // namespace oox::xls
214 
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static css::uno::Sequence< css::table::CellRangeAddress > toApiSequence(const ScRangeList &orRanges)
Converts the passed range list to a sequence of cell range addresses.
Helper class to provide access to global workbook data.
bool checkCellAddress(const ScAddress &rAddress, bool bTrackOverflow)
Checks the passed cell address if it fits into the spreadsheet limits.
virtual void skip(sal_Int32 nBytes, size_t nAtomSize=1) override
OptValue< bool > getBool(sal_Int32 nAttrToken) const
void finalizeImport()
Creates all scenarios in the Calc sheet.
Scenario & createScenario()
Creates and returns a new scenario in this collection.
A 2D cell address struct for binary filters.
OptValue< OUString > getXString(sal_Int32 nAttrToken) const
sal_Int32 mnNumFmtId
OptValue< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
ScenarioModel maModel
Scenario cells.
void finalizeImport()
Creates the scenario in the Calc document.
bool mbLocked
Name of user created the scenario.
OptValue< OUString > getString(sal_Int32 nAttrToken) const
std::vector< ScenarioCellModel > maCells
void forEachMem(FuncType pFunc) const
Scenario(const WorkbookHelper &rHelper, sal_Int16 nSheet, bool bIsActive)
sal_uInt16 readuInt16()
OUString maUser
Comment.
void push_back(const ScRange &rRange)
Definition: rangelst.cxx:1144
bool mbActive
bool empty() const
Definition: rangelst.hxx:89
bool mbHidden
True = input cell values locked.
container_type::value_type value_type
const css::uno::Reference< css::sheet::XSpreadsheetDocument > & getDocument() const
Returns a reference to the source/target spreadsheet document model.
void importScenario(const AttributeList &rAttribs)
Imports a scenario definition from a scenario element.
void importInputCells(const AttributeList &rAttribs)
Imports a new cell for this scenario from an inputCells element.
SheetScenarios & createSheetScenarios(sal_Int16 nSheet)
Creates and returns a scenario collection for the passed sheet.
static OUString getUnusedName(const css::uno::Reference< css::container::XNameAccess > &rxNameAccess, const OUString &rSuggestedName, sal_Unicode cSeparator)
sal_Int32 mnShown
Selected scenario.
ScenarioBuffer(const WorkbookHelper &rHelper)
void importScenarios(const AttributeList &rAttribs)
Imports sheet scenario settings from a scenarios element.
Converter for cell addresses and cell ranges for OOXML and BIFF filters.
void finalizeImport()
Creates all scenarios in the Calc document.
SheetScenariosModel maModel
SheetScenarios(const WorkbookHelper &rHelper, sal_Int16 nSheet)
OUString maComment
Name of the scenario.
bool mbActive
True = scenario is hidden.
SheetScenariosMap maSheetScenarios
css::uno::Reference< css::sheet::XSpreadsheet > getSheetFromDoc(sal_Int32 nSheet) const
Returns a reference to the specified spreadsheet in the document model.
static bool convertToCellAddressUnchecked(ScAddress &orAddress, const OUString &rString, sal_Int16 nSheet)
Converts the passed string to a single cell address, without checking any sheet limits.
SheetScenariosModel()
Visible scenario.
sal_Int16 mnSheet
Scenario model data.
void forEachMem(FuncType pFunc) const
bool setProperty(sal_Int32 nPropId, const Type &rValue)
AddressConverter & getAddressConverter() const
Returns the converter for string to cell address/range conversion.