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 85 : SheetInfoModel::SheetInfoModel() :
46 : mnBiffHandle( -1 ),
47 : mnSheetId( -1 ),
48 85 : mnState( XML_visible )
49 : {
50 85 : }
51 :
52 44 : WorksheetBuffer::WorksheetBuffer( const WorkbookHelper& rHelper ) :
53 44 : WorkbookHelper( rHelper )
54 : {
55 44 : }
56 :
57 85 : void WorksheetBuffer::importSheet( const AttributeList& rAttribs )
58 : {
59 85 : SheetInfoModel aModel;
60 85 : aModel.maRelId = rAttribs.getString( R_TOKEN( id ), OUString() );
61 85 : aModel.maName = rAttribs.getXString( XML_name, OUString() );
62 85 : aModel.mnSheetId = rAttribs.getInteger( XML_sheetId, -1 );
63 85 : aModel.mnState = rAttribs.getToken( XML_state, XML_visible );
64 85 : insertSheet( aModel );
65 85 : }
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 88 : sal_Int32 WorksheetBuffer::getWorksheetCount() const
83 : {
84 88 : return static_cast< sal_Int32 >( maSheetInfos.size() );
85 : }
86 :
87 85 : OUString WorksheetBuffer::getWorksheetRelId( sal_Int32 nWorksheet ) const
88 : {
89 85 : const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get();
90 85 : return pSheetInfo ? pSheetInfo->maRelId : OUString();
91 : }
92 :
93 175 : sal_Int16 WorksheetBuffer::getCalcSheetIndex( sal_Int32 nWorksheet ) const
94 : {
95 175 : const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get();
96 175 : return pSheetInfo ? pSheetInfo->mnCalcSheet : -1;
97 : }
98 :
99 129 : OUString WorksheetBuffer::getCalcSheetName( sal_Int32 nWorksheet ) const
100 : {
101 129 : const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get();
102 129 : return pSheetInfo ? pSheetInfo->maCalcName : OUString();
103 : }
104 :
105 14 : void WorksheetBuffer::convertSheetNameRef( OUString& sSheetNameRef ) const
106 : {
107 : // convert '#SheetName!A1' to '#SheetName.A1'
108 14 : 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 14 : }
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 3 : OUString WorksheetBuffer::getCalcSheetName( const OUString& rWorksheetName ) const
131 : {
132 3 : if( const SheetInfo* pSheetInfo = maSheetInfosByName.get( rWorksheetName ).get() )
133 : {
134 3 : bool bIsQuoted = pSheetInfo->maName != rWorksheetName;
135 3 : return bIsQuoted ? pSheetInfo->maCalcQuotedName : pSheetInfo->maCalcName;
136 : }
137 0 : return OUString();
138 : }
139 :
140 : // private --------------------------------------------------------------------
141 :
142 : namespace {
143 :
144 170 : OUString lclQuoteName( const OUString& rName )
145 : {
146 170 : OUStringBuffer aBuffer( rName );
147 : // duplicate all quote characters
148 1158 : for( sal_Int32 nPos = aBuffer.getLength() - 1; nPos >= 0; --nPos )
149 988 : if( aBuffer[nPos] == '\'' )
150 0 : aBuffer.insert( nPos, '\'' );
151 : // add outer quotes and return
152 170 : return aBuffer.insert( 0, '\'' ).append( '\'' ).makeStringAndClear();
153 : }
154 :
155 : } // namespace
156 :
157 85 : WorksheetBuffer::SheetInfo::SheetInfo( const SheetInfoModel& rModel, sal_Int16 nCalcSheet, const OUString& rCalcName ) :
158 : SheetInfoModel( rModel ),
159 : maCalcName( rCalcName ),
160 : maCalcQuotedName( lclQuoteName( rCalcName ) ),
161 85 : mnCalcSheet( nCalcSheet )
162 : {
163 85 : }
164 :
165 85 : WorksheetBuffer::IndexNamePair WorksheetBuffer::createSheet( const OUString& rPreferredName, sal_Int32 nSheetPos, bool bVisible )
166 : {
167 : try
168 : {
169 85 : Reference< XSpreadsheets > xSheets( getDocument()->getSheets(), UNO_QUERY_THROW );
170 170 : Reference< XIndexAccess > xSheetsIA( xSheets, UNO_QUERY_THROW );
171 85 : sal_Int16 nCalcSheet = -1;
172 170 : OUString aSheetName = rPreferredName.isEmpty() ? "Sheet" : rPreferredName;
173 170 : PropertySet aPropSet;
174 85 : if( nSheetPos < xSheetsIA->getCount() )
175 : {
176 44 : nCalcSheet = static_cast< sal_Int16 >( nSheetPos );
177 : // existing sheet - try to rename
178 44 : Reference< XNamed > xSheetName( xSheetsIA->getByIndex( nSheetPos ), UNO_QUERY_THROW );
179 44 : if( xSheetName->getName() != aSheetName )
180 : {
181 7 : aSheetName = ContainerHelper::getUnusedName( xSheets, aSheetName, ' ' );
182 7 : xSheetName->setName( aSheetName );
183 : }
184 44 : aPropSet.set( xSheetName );
185 : }
186 : else
187 : {
188 41 : nCalcSheet = static_cast< sal_Int16 >( xSheetsIA->getCount() );
189 : // new sheet - insert with unused name
190 41 : aSheetName = ContainerHelper::getUnusedName( xSheets, aSheetName, ' ' );
191 41 : xSheets->insertNewByName( aSheetName, nCalcSheet );
192 41 : aPropSet.set( xSheetsIA->getByIndex( nCalcSheet ) );
193 : }
194 :
195 : // sheet properties
196 85 : aPropSet.setProperty( PROP_IsVisible, bVisible );
197 :
198 : // return final sheet index if sheet exists
199 170 : 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 85 : void WorksheetBuffer::insertSheet( const SheetInfoModel& rModel )
209 : {
210 85 : sal_Int32 nWorksheet = static_cast< sal_Int32 >( maSheetInfos.size() );
211 85 : IndexNamePair aIndexName = createSheet( rModel.maName, nWorksheet, rModel.mnState == XML_visible );
212 170 : ::boost::shared_ptr< SheetInfo > xSheetInfo( new SheetInfo( rModel, aIndexName.first, aIndexName.second ) );
213 85 : maSheetInfos.push_back( xSheetInfo );
214 85 : maSheetInfosByName[ rModel.maName ] = xSheetInfo;
215 170 : maSheetInfosByName[ lclQuoteName( rModel.maName ) ] = xSheetInfo;
216 85 : }
217 :
218 : } // namespace xls
219 18 : } // namespace oox
220 :
221 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|