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