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