Line data Source code
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 "worksheetbuffer.hxx"
21 :
22 : #include <com/sun/star/container/XIndexAccess.hpp>
23 : #include <com/sun/star/container/XNameAccess.hpp>
24 : #include <com/sun/star/container/XNamed.hpp>
25 : #include <com/sun/star/sheet/XExternalSheetName.hpp>
26 : #include <com/sun/star/sheet/XSheetLinkable.hpp>
27 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
28 : #include <rtl/ustrbuf.hxx>
29 : #include <oox/core/filterbase.hxx>
30 : #include <oox/helper/attributelist.hxx>
31 : #include <oox/helper/containerhelper.hxx>
32 : #include <oox/helper/propertyset.hxx>
33 : #include <oox/token/properties.hxx>
34 : #include "biffinputstream.hxx"
35 : #include "excelhandlers.hxx"
36 :
37 : namespace oox {
38 : namespace xls {
39 :
40 : using namespace ::com::sun::star::container;
41 : using namespace ::com::sun::star::sheet;
42 : using namespace ::com::sun::star::uno;
43 :
44 260 : SheetInfoModel::SheetInfoModel() :
45 : mnBiffHandle( -1 ),
46 : mnSheetId( -1 ),
47 260 : mnState( XML_visible )
48 : {
49 260 : }
50 :
51 128 : WorksheetBuffer::WorksheetBuffer( const WorkbookHelper& rHelper ) :
52 128 : WorkbookHelper( rHelper )
53 : {
54 128 : }
55 :
56 260 : void WorksheetBuffer::importSheet( const AttributeList& rAttribs )
57 : {
58 260 : SheetInfoModel aModel;
59 260 : aModel.maRelId = rAttribs.getString( R_TOKEN( id ), OUString() );
60 260 : aModel.maName = rAttribs.getXString( XML_name, OUString() );
61 260 : aModel.mnSheetId = rAttribs.getInteger( XML_sheetId, -1 );
62 260 : aModel.mnState = rAttribs.getToken( XML_state, XML_visible );
63 260 : insertSheet( aModel );
64 260 : }
65 :
66 0 : void WorksheetBuffer::importSheet( SequenceInputStream& rStrm )
67 : {
68 : sal_Int32 nState;
69 0 : SheetInfoModel aModel;
70 0 : rStrm >> nState >> aModel.mnSheetId >> aModel.maRelId >> aModel.maName;
71 : static const sal_Int32 spnStates[] = { XML_visible, XML_hidden, XML_veryHidden };
72 0 : aModel.mnState = STATIC_ARRAY_SELECT( spnStates, nState, XML_visible );
73 0 : insertSheet( aModel );
74 0 : }
75 :
76 0 : sal_Int16 WorksheetBuffer::insertEmptySheet( const OUString& rPreferredName, bool bVisible )
77 : {
78 0 : return createSheet( rPreferredName, SAL_MAX_INT32, bVisible ).first;
79 : }
80 :
81 256 : sal_Int32 WorksheetBuffer::getWorksheetCount() const
82 : {
83 256 : return static_cast< sal_Int32 >( maSheetInfos.size() );
84 : }
85 :
86 260 : OUString WorksheetBuffer::getWorksheetRelId( sal_Int32 nWorksheet ) const
87 : {
88 260 : const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get();
89 260 : return pSheetInfo ? pSheetInfo->maRelId : OUString();
90 : }
91 :
92 530 : sal_Int16 WorksheetBuffer::getCalcSheetIndex( sal_Int32 nWorksheet ) const
93 : {
94 530 : const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get();
95 530 : return pSheetInfo ? pSheetInfo->mnCalcSheet : -1;
96 : }
97 :
98 388 : OUString WorksheetBuffer::getCalcSheetName( sal_Int32 nWorksheet ) const
99 : {
100 388 : const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get();
101 388 : return pSheetInfo ? pSheetInfo->maCalcName : OUString();
102 : }
103 :
104 46 : void WorksheetBuffer::convertSheetNameRef( OUString& sSheetNameRef ) const
105 : {
106 : // convert '#SheetName!A1' to '#SheetName.A1'
107 46 : if( sSheetNameRef.startsWith("#") )
108 : {
109 0 : sal_Int32 nSepPos = sSheetNameRef.lastIndexOf( '!' );
110 0 : if( nSepPos > 0 )
111 : {
112 : // replace the exclamation mark with a period
113 0 : sSheetNameRef = sSheetNameRef.replaceAt( nSepPos, 1, OUString( '.' ) );
114 : // #i66592# convert sheet names that have been renamed on import
115 0 : OUString aSheetName = sSheetNameRef.copy( 1, nSepPos - 1 );
116 0 : OUString aCalcName = getCalcSheetName( aSheetName );
117 0 : if( !aCalcName.isEmpty() )
118 0 : sSheetNameRef = sSheetNameRef.replaceAt( 1, nSepPos - 1, aCalcName );
119 : }
120 : }
121 46 : }
122 :
123 8 : sal_Int16 WorksheetBuffer::getCalcSheetIndex( const OUString& rWorksheetName ) const
124 : {
125 8 : const SheetInfo* pSheetInfo = maSheetInfosByName.get( rWorksheetName ).get();
126 8 : return pSheetInfo ? pSheetInfo->mnCalcSheet : -1;
127 : }
128 :
129 6 : OUString WorksheetBuffer::getCalcSheetName( const OUString& rWorksheetName ) const
130 : {
131 6 : if( const SheetInfo* pSheetInfo = maSheetInfosByName.get( rWorksheetName ).get() )
132 : {
133 6 : bool bIsQuoted = pSheetInfo->maName != rWorksheetName;
134 6 : return bIsQuoted ? pSheetInfo->maCalcQuotedName : pSheetInfo->maCalcName;
135 : }
136 0 : return OUString();
137 : }
138 :
139 : // private --------------------------------------------------------------------
140 :
141 : namespace {
142 :
143 520 : OUString lclQuoteName( const OUString& rName )
144 : {
145 520 : OUStringBuffer aBuffer( rName );
146 : // duplicate all quote characters
147 3532 : for( sal_Int32 nPos = aBuffer.getLength() - 1; nPos >= 0; --nPos )
148 3012 : if( aBuffer[nPos] == '\'' )
149 0 : aBuffer.insert( nPos, '\'' );
150 : // add outer quotes and return
151 520 : return aBuffer.insert( 0, '\'' ).append( '\'' ).makeStringAndClear();
152 : }
153 :
154 : } // namespace
155 :
156 260 : WorksheetBuffer::SheetInfo::SheetInfo( const SheetInfoModel& rModel, sal_Int16 nCalcSheet, const OUString& rCalcName ) :
157 : SheetInfoModel( rModel ),
158 : maCalcName( rCalcName ),
159 : maCalcQuotedName( lclQuoteName( rCalcName ) ),
160 260 : mnCalcSheet( nCalcSheet )
161 : {
162 260 : }
163 :
164 260 : WorksheetBuffer::IndexNamePair WorksheetBuffer::createSheet( const OUString& rPreferredName, sal_Int32 nSheetPos, bool bVisible )
165 : {
166 : try
167 : {
168 260 : Reference< XSpreadsheets > xSheets( getDocument()->getSheets(), UNO_QUERY_THROW );
169 520 : Reference< XIndexAccess > xSheetsIA( xSheets, UNO_QUERY_THROW );
170 260 : sal_Int16 nCalcSheet = -1;
171 520 : OUString aSheetName = rPreferredName.isEmpty() ? "Sheet" : rPreferredName;
172 520 : PropertySet aPropSet;
173 260 : if( nSheetPos < xSheetsIA->getCount() )
174 : {
175 128 : nCalcSheet = static_cast< sal_Int16 >( nSheetPos );
176 : // existing sheet - try to rename
177 128 : Reference< XNamed > xSheetName( xSheetsIA->getByIndex( nSheetPos ), UNO_QUERY_THROW );
178 128 : if( xSheetName->getName() != aSheetName )
179 : {
180 20 : aSheetName = ContainerHelper::getUnusedName( xSheets, aSheetName, ' ' );
181 20 : xSheetName->setName( aSheetName );
182 : }
183 128 : aPropSet.set( xSheetName );
184 : }
185 : else
186 : {
187 132 : nCalcSheet = static_cast< sal_Int16 >( xSheetsIA->getCount() );
188 : // new sheet - insert with unused name
189 132 : aSheetName = ContainerHelper::getUnusedName( xSheets, aSheetName, ' ' );
190 132 : xSheets->insertNewByName( aSheetName, nCalcSheet );
191 132 : aPropSet.set( xSheetsIA->getByIndex( nCalcSheet ) );
192 : }
193 :
194 : // sheet properties
195 260 : aPropSet.setProperty( PROP_IsVisible, bVisible );
196 :
197 : // return final sheet index if sheet exists
198 520 : return IndexNamePair( nCalcSheet, aSheetName );
199 : }
200 0 : catch( Exception& )
201 : {
202 : OSL_FAIL( "WorksheetBuffer::createSheet - cannot insert or rename worksheet" );
203 : }
204 0 : return IndexNamePair( -1, OUString() );
205 : }
206 :
207 260 : void WorksheetBuffer::insertSheet( const SheetInfoModel& rModel )
208 : {
209 260 : sal_Int32 nWorksheet = static_cast< sal_Int32 >( maSheetInfos.size() );
210 260 : IndexNamePair aIndexName = createSheet( rModel.maName, nWorksheet, rModel.mnState == XML_visible );
211 520 : ::boost::shared_ptr< SheetInfo > xSheetInfo( new SheetInfo( rModel, aIndexName.first, aIndexName.second ) );
212 260 : maSheetInfos.push_back( xSheetInfo );
213 260 : maSheetInfosByName[ rModel.maName ] = xSheetInfo;
214 520 : maSheetInfosByName[ lclQuoteName( rModel.maName ) ] = xSheetInfo;
215 260 : }
216 :
217 : } // namespace xls
218 48 : } // namespace oox
219 :
220 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|