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
36namespace oox::xls {
37
38using namespace ::com::sun::star::container;
39using namespace ::com::sun::star::sheet;
40using namespace ::com::sun::star::table;
41using 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
56Scenario::Scenario( const WorkbookHelper& rHelper, sal_Int16 nSheet, bool bIsActive ) :
57 WorkbookHelper( rHelper ),
58 mnSheet( nSheet )
59{
60 maModel.mbActive = bIsActive;
61}
62
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{
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
164SheetScenarios::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: */
bool empty() const
Definition: rangelst.hxx:88
void push_back(const ScRange &rRange)
Definition: rangelst.cxx:1137
std::optional< OUString > getXString(sal_Int32 nAttrToken) const
std::optional< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
std::optional< OUString > getString(sal_Int32 nAttrToken) const
std::optional< bool > getBool(sal_Int32 nAttrToken) const
static OUString getUnusedName(const css::uno::Reference< css::container::XNameAccess > &rxNameAccess, const OUString &rSuggestedName, sal_Unicode cSeparator)
bool setProperty(sal_Int32 nPropId, const Type &rValue)
void forEachMem(FuncType pFunc) const
void forEachMem(FuncType pFunc) const
container_type::value_type value_type
Converter for cell addresses and cell ranges for OOXML and BIFF filters.
bool checkCellAddress(const ScAddress &rAddress, bool bTrackOverflow)
Checks the passed cell address if it fits into the spreadsheet limits.
static css::uno::Sequence< css::table::CellRangeAddress > toApiSequence(const ScRangeList &orRanges)
Converts the passed range list to a sequence of cell range addresses.
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.
ScenarioBuffer(const WorkbookHelper &rHelper)
SheetScenarios & createSheetScenarios(sal_Int16 nSheet)
Creates and returns a scenario collection for the passed sheet.
SheetScenariosMap maSheetScenarios
void finalizeImport()
Creates all scenarios in the Calc document.
void finalizeImport()
Creates the scenario in the Calc document.
std::vector< ScenarioCellModel > maCells
ScenarioModel maModel
Scenario cells.
Scenario(const WorkbookHelper &rHelper, sal_Int16 nSheet, bool bIsActive)
void importInputCells(const AttributeList &rAttribs)
Imports a new cell for this scenario from an inputCells element.
sal_Int16 mnSheet
Scenario model data.
void importScenario(const AttributeList &rAttribs)
Imports a scenario definition from a scenario element.
SheetScenariosModel maModel
SheetScenarios(const WorkbookHelper &rHelper, sal_Int16 nSheet)
void finalizeImport()
Creates all scenarios in the Calc sheet.
Scenario & createScenario()
Creates and returns a new scenario in this collection.
void importScenarios(const AttributeList &rAttribs)
Imports sheet scenario settings from a scenarios element.
Helper class to provide access to global workbook data.
css::uno::Reference< css::sheet::XSpreadsheet > getSheetFromDoc(sal_Int32 nSheet) const
Returns a reference to the specified spreadsheet in the document model.
AddressConverter & getAddressConverter() const
Returns the converter for string to cell address/range conversion.
const css::uno::Reference< css::sheet::XSpreadsheetDocument > & getDocument() const
Returns a reference to the source/target spreadsheet document model.
@ Exception
void SvStream & rStrm
sal_Int32 mnNumFmtId
bool mbActive
A 2D cell address struct for binary filters.
OUString maComment
Name of the scenario.
bool mbActive
True = scenario is hidden.
bool mbHidden
True = input cell values locked.
OUString maUser
Comment.
bool mbLocked
Name of user created the scenario.
SheetScenariosModel()
Visible scenario.
sal_Int32 mnShown
Selected scenario.