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 <comphelper/processfactory.hxx>
21 : #include <comphelper/types.hxx>
22 : #include <vcl/msgbox.hxx>
23 : #include <svx/dataaccessdescriptor.hxx>
24 : #include <sfx2/viewfrm.hxx>
25 :
26 : #include <com/sun/star/sdb/CommandType.hpp>
27 : #include <com/sun/star/sdb/XCompletedExecution.hpp>
28 : #include <com/sun/star/sdbc/XRow.hpp>
29 : #include <com/sun/star/sdbc/XRowSet.hpp>
30 : #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
31 : #include <com/sun/star/sdbcx/XRowLocate.hpp>
32 : #include <com/sun/star/task/InteractionHandler.hpp>
33 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 : #include <com/sun/star/beans/XPropertySet.hpp>
35 : #include <com/sun/star/frame/XDispatchProvider.hpp>
36 : #include <com/sun/star/frame/FrameSearchFlag.hpp>
37 : #include <com/sun/star/view/XSelectionSupplier.hpp>
38 :
39 :
40 : #include "dbdocfun.hxx"
41 : #include "docsh.hxx"
42 : #include "globstr.hrc"
43 : #include "scerrors.hxx"
44 : #include "dbdata.hxx"
45 : #include "markdata.hxx"
46 : #include "undodat.hxx"
47 : #include "progress.hxx"
48 : #include "patattr.hxx"
49 : #include "docpool.hxx"
50 : #include "attrib.hxx"
51 : #include "dbdocutl.hxx"
52 : #include "editable.hxx"
53 : #include "hints.hxx"
54 : #include "miscuno.hxx"
55 : #include "chgtrack.hxx"
56 :
57 : using namespace com::sun::star;
58 :
59 : #define SC_SERVICE_ROWSET "com.sun.star.sdb.RowSet"
60 :
61 : //! move to a header file?
62 : #define SC_DBPROP_DATASOURCENAME "DataSourceName"
63 : #define SC_DBPROP_COMMAND "Command"
64 : #define SC_DBPROP_COMMANDTYPE "CommandType"
65 :
66 0 : void ScDBDocFunc::ShowInBeamer( const ScImportParam& rParam, SfxViewFrame* pFrame )
67 : {
68 : // called after opening the database beamer
69 :
70 0 : if ( !pFrame || !rParam.bImport )
71 0 : return;
72 :
73 0 : uno::Reference<frame::XFrame> xFrame = pFrame->GetFrame().GetFrameInterface();
74 0 : uno::Reference<frame::XDispatchProvider> xDP(xFrame, uno::UNO_QUERY);
75 :
76 0 : uno::Reference<frame::XFrame> xBeamerFrame = xFrame->findFrame(
77 : OUString("_beamer"),
78 0 : frame::FrameSearchFlag::CHILDREN);
79 0 : if (xBeamerFrame.is())
80 : {
81 0 : uno::Reference<frame::XController> xController = xBeamerFrame->getController();
82 0 : uno::Reference<view::XSelectionSupplier> xControllerSelection(xController, uno::UNO_QUERY);
83 0 : if (xControllerSelection.is())
84 : {
85 : sal_Int32 nType = rParam.bSql ? sdb::CommandType::COMMAND :
86 0 : ( (rParam.nType == ScDbQuery) ? sdb::CommandType::QUERY :
87 0 : sdb::CommandType::TABLE );
88 :
89 0 : ::svx::ODataAccessDescriptor aSelection;
90 0 : aSelection.setDataSource(rParam.aDBName);
91 0 : aSelection[svx::daCommand] <<= rParam.aStatement;
92 0 : aSelection[svx::daCommandType] <<= nType;
93 :
94 0 : xControllerSelection->select(uno::makeAny(aSelection.createPropertyValueSequence()));
95 : }
96 : else
97 : {
98 : OSL_FAIL("no selection supplier in the beamer!");
99 0 : }
100 0 : }
101 : }
102 :
103 : // -----------------------------------------------------------------
104 :
105 0 : bool ScDBDocFunc::DoImportUno( const ScAddress& rPos,
106 : const uno::Sequence<beans::PropertyValue>& aArgs )
107 : {
108 0 : svx::ODataAccessDescriptor aDesc( aArgs ); // includes selection and result set
109 :
110 : // create database range
111 0 : ScDBData* pDBData = rDocShell.GetDBData( ScRange(rPos), SC_DB_IMPORT, SC_DBSEL_KEEP );
112 : DBG_ASSERT(pDBData, "can't create DB data");
113 0 : String sTarget = pDBData->GetName();
114 :
115 0 : UpdateImport( sTarget, aDesc );
116 :
117 0 : return true;
118 : }
119 :
120 : // -----------------------------------------------------------------
121 :
122 4 : bool ScDBDocFunc::DoImport( SCTAB nTab, const ScImportParam& rParam,
123 : const svx::ODataAccessDescriptor* pDescriptor, bool bRecord, bool bAddrInsert )
124 : {
125 4 : ScDocument* pDoc = rDocShell.GetDocument();
126 4 : ScChangeTrack *pChangeTrack = NULL;
127 4 : ScRange aChangedRange;
128 :
129 4 : if (bRecord && !pDoc->IsUndoEnabled())
130 0 : bRecord = false;
131 :
132 4 : ScDBData* pDBData = NULL;
133 4 : if ( !bAddrInsert )
134 : {
135 : pDBData = pDoc->GetDBAtArea( nTab, rParam.nCol1, rParam.nRow1,
136 4 : rParam.nCol2, rParam.nRow2 );
137 4 : if (!pDBData)
138 : {
139 : OSL_FAIL( "DoImport: no DBData" );
140 0 : return false;
141 : }
142 : }
143 :
144 4 : Window* pWaitWin = rDocShell.GetActiveDialogParent();
145 4 : if (pWaitWin)
146 4 : pWaitWin->EnterWait();
147 4 : ScDocShellModificator aModificator( rDocShell );
148 :
149 4 : sal_Bool bSuccess = false;
150 4 : sal_Bool bApi = false; //! pass as argument
151 4 : sal_Bool bTruncated = false; // for warning
152 4 : sal_uInt16 nErrStringId = 0;
153 8 : String aErrorMessage;
154 :
155 4 : SCCOL nCol = rParam.nCol1;
156 4 : SCROW nRow = rParam.nRow1;
157 4 : SCCOL nEndCol = nCol; // end of resulting database area
158 4 : SCROW nEndRow = nRow;
159 : long i;
160 :
161 4 : sal_Bool bDoSelection = false;
162 4 : sal_Bool bRealSelection = false; // sal_True if not everything is selected
163 4 : sal_Bool bBookmarkSelection = sal_False;
164 4 : sal_Int32 nListPos = 0;
165 4 : sal_Int32 nRowsRead = 0;
166 4 : sal_Int32 nListCount = 0;
167 :
168 8 : uno::Sequence<uno::Any> aSelection;
169 4 : if ( pDescriptor && pDescriptor->has(svx::daSelection) )
170 : {
171 0 : (*pDescriptor)[svx::daSelection] >>= aSelection;
172 0 : nListCount = aSelection.getLength();
173 0 : if ( nListCount > 0 )
174 : {
175 0 : bDoSelection = true;
176 0 : if ( pDescriptor->has(svx::daBookmarkSelection) )
177 0 : bBookmarkSelection = ScUnoHelpFunctions::GetBoolFromAny( (*pDescriptor)[svx::daBookmarkSelection] );
178 0 : if ( bBookmarkSelection )
179 : {
180 : // From bookmarks, there's no way to detect if all records are selected.
181 : // Rely on base to pass no selection in that case.
182 0 : bRealSelection = true;
183 : }
184 : }
185 : }
186 :
187 8 : uno::Reference<sdbc::XResultSet> xResultSet;
188 4 : if ( pDescriptor && pDescriptor->has(svx::daCursor) )
189 0 : xResultSet.set((*pDescriptor)[svx::daCursor], uno::UNO_QUERY);
190 :
191 : // ImportDoc - also used for Redo
192 4 : ScDocument* pImportDoc = new ScDocument( SCDOCMODE_UNDO );
193 4 : pImportDoc->InitUndo( pDoc, nTab, nTab );
194 :
195 : //
196 : // get data from database into import document
197 : //
198 :
199 : try
200 : {
201 : // progress bar
202 : // only text (title is still needed, for the cancel button)
203 4 : ScProgress aProgress( &rDocShell, ScGlobal::GetRscString(STR_UNDO_IMPORTDATA), 0 );
204 :
205 : uno::Reference<sdbc::XRowSet> xRowSet = uno::Reference<sdbc::XRowSet>(
206 8 : xResultSet, uno::UNO_QUERY );
207 4 : sal_Bool bDispose = false;
208 4 : if ( !xRowSet.is() )
209 : {
210 4 : bDispose = sal_True;
211 8 : xRowSet = uno::Reference<sdbc::XRowSet>(
212 8 : comphelper::getProcessServiceFactory()->createInstance(
213 4 : OUString( SC_SERVICE_ROWSET ) ),
214 4 : uno::UNO_QUERY);
215 4 : uno::Reference<beans::XPropertySet> xRowProp( xRowSet, uno::UNO_QUERY );
216 : OSL_ENSURE( xRowProp.is(), "can't get RowSet" );
217 4 : if ( xRowProp.is() )
218 : {
219 : //
220 : // set source parameters
221 : //
222 :
223 : sal_Int32 nType = rParam.bSql ? sdb::CommandType::COMMAND :
224 1 : ( (rParam.nType == ScDbQuery) ? sdb::CommandType::QUERY :
225 5 : sdb::CommandType::TABLE );
226 4 : uno::Any aAny;
227 :
228 4 : aAny <<= rParam.aDBName;
229 4 : xRowProp->setPropertyValue(
230 4 : OUString(SC_DBPROP_DATASOURCENAME), aAny );
231 :
232 4 : aAny <<= rParam.aStatement;
233 4 : xRowProp->setPropertyValue(
234 4 : OUString(SC_DBPROP_COMMAND), aAny );
235 :
236 4 : aAny <<= nType;
237 4 : xRowProp->setPropertyValue(
238 4 : OUString(SC_DBPROP_COMMANDTYPE), aAny );
239 :
240 8 : uno::Reference<sdb::XCompletedExecution> xExecute( xRowSet, uno::UNO_QUERY );
241 4 : if ( xExecute.is() )
242 : {
243 : uno::Reference<task::XInteractionHandler> xHandler(
244 : task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
245 4 : uno::UNO_QUERY_THROW);
246 4 : xExecute->executeWithCompletion( xHandler );
247 : }
248 : else
249 4 : xRowSet->execute();
250 4 : }
251 : }
252 4 : if ( xRowSet.is() )
253 : {
254 : //
255 : // get column descriptions
256 : //
257 :
258 4 : long nColCount = 0;
259 4 : uno::Reference<sdbc::XResultSetMetaData> xMeta;
260 8 : uno::Reference<sdbc::XResultSetMetaDataSupplier> xMetaSupp( xRowSet, uno::UNO_QUERY );
261 4 : if ( xMetaSupp.is() )
262 4 : xMeta = xMetaSupp->getMetaData();
263 4 : if ( xMeta.is() )
264 4 : nColCount = xMeta->getColumnCount(); // this is the number of real columns
265 :
266 4 : if ( rParam.nCol1 + nColCount - 1 > MAXCOL )
267 : {
268 0 : nColCount = 0;
269 : //! error message
270 : }
271 :
272 8 : uno::Reference<sdbcx::XRowLocate> xLocate;
273 4 : if ( bBookmarkSelection )
274 : {
275 0 : xLocate.set( xRowSet, uno::UNO_QUERY );
276 0 : if ( !xLocate.is() )
277 : {
278 : SAL_WARN( "sc.ui","can't get XRowLocate");
279 0 : bDoSelection = bRealSelection = bBookmarkSelection = sal_False;
280 : }
281 : }
282 :
283 8 : uno::Reference<sdbc::XRow> xRow( xRowSet, uno::UNO_QUERY );
284 4 : if ( nColCount > 0 && xRow.is() )
285 : {
286 4 : nEndCol = (SCCOL)( rParam.nCol1 + nColCount - 1 );
287 :
288 4 : uno::Sequence<sal_Int32> aColTypes( nColCount ); // column types
289 8 : uno::Sequence<sal_Bool> aColCurr( nColCount ); // currency flag is not in types
290 4 : sal_Int32* pTypeArr = aColTypes.getArray();
291 4 : sal_Bool* pCurrArr = aColCurr.getArray();
292 38 : for (i=0; i<nColCount; i++)
293 : {
294 34 : pTypeArr[i] = xMeta->getColumnType( i+1 );
295 34 : pCurrArr[i] = xMeta->isCurrency( i+1 );
296 : }
297 :
298 4 : if ( !bAddrInsert ) // read column names
299 : {
300 4 : nCol = rParam.nCol1;
301 38 : for (i=0; i<nColCount; i++)
302 : {
303 : pImportDoc->SetString( nCol, nRow, nTab,
304 34 : xMeta->getColumnLabel( i+1 ) );
305 34 : ++nCol;
306 : }
307 4 : ++nRow;
308 : }
309 :
310 4 : sal_Bool bEnd = false;
311 4 : if ( !bDoSelection )
312 4 : xRowSet->beforeFirst();
313 4 : sal_uInt16 nInserted = 0;
314 92 : while ( !bEnd )
315 : {
316 : // skip rows that are not selected
317 84 : if ( !bDoSelection )
318 : {
319 84 : if ( (bEnd = !xRowSet->next()) == false )
320 80 : ++nRowsRead;
321 : }
322 : else
323 : {
324 0 : if (nListPos < nListCount)
325 : {
326 0 : if ( bBookmarkSelection )
327 : {
328 0 : bEnd = !xLocate->moveToBookmark(aSelection[nListPos]);
329 : }
330 : else // use record numbers
331 : {
332 0 : sal_Int32 nNextRow = 0;
333 0 : aSelection[nListPos] >>= nNextRow;
334 0 : if ( nRowsRead+1 < nNextRow )
335 0 : bRealSelection = true;
336 0 : bEnd = !xRowSet->absolute(nRowsRead = nNextRow);
337 : }
338 0 : ++nListPos;
339 : }
340 : else
341 : {
342 0 : if ( !bBookmarkSelection && xRowSet->next() )
343 0 : bRealSelection = true; // more data available but not used
344 0 : bEnd = true;
345 : }
346 : }
347 :
348 84 : if ( !bEnd )
349 : {
350 80 : if ( ValidRow(nRow) )
351 : {
352 80 : nCol = rParam.nCol1;
353 760 : for (i=0; i<nColCount; i++)
354 : {
355 : ScDatabaseDocUtil::PutData( pImportDoc, nCol, nRow, nTab,
356 680 : xRow, i+1, pTypeArr[i], pCurrArr[i] );
357 680 : ++nCol;
358 : }
359 80 : nEndRow = nRow;
360 80 : ++nRow;
361 :
362 : // progress bar
363 :
364 80 : ++nInserted;
365 80 : if (!(nInserted & 15))
366 : {
367 4 : String aPict = ScGlobal::GetRscString( STR_PROGRESS_IMPORT );
368 8 : String aText = aPict.GetToken(0,'#');
369 4 : aText += OUString::number( nInserted );
370 4 : aText += aPict.GetToken(1,'#');
371 :
372 4 : if (!aProgress.SetStateText( 0, aText )) // stopped by user?
373 : {
374 0 : bEnd = sal_True;
375 0 : bSuccess = false;
376 0 : nErrStringId = STR_DATABASE_ABORTED;
377 4 : }
378 : }
379 : }
380 : else // past the end of the spreadsheet
381 : {
382 0 : bEnd = sal_True; // don't continue
383 0 : bTruncated = sal_True; // warning flag
384 : }
385 : }
386 : }
387 :
388 8 : bSuccess = sal_True;
389 : }
390 :
391 4 : if ( bDispose )
392 8 : ::comphelper::disposeComponent( xRowSet );
393 4 : }
394 : }
395 0 : catch ( const sdbc::SQLException& rError )
396 : {
397 0 : aErrorMessage = rError.Message;
398 : }
399 0 : catch ( uno::Exception& )
400 : {
401 : OSL_FAIL("Unexpected exception in database");
402 : }
403 :
404 : //
405 : // test for cell protection
406 : //
407 :
408 4 : sal_Bool bKeepFormat = !bAddrInsert && pDBData->IsKeepFmt();
409 4 : sal_Bool bMoveCells = !bAddrInsert && pDBData->IsDoSize();
410 4 : SCCOL nFormulaCols = 0; // columns to be filled with formulas
411 4 : if (bMoveCells && nEndCol == rParam.nCol2)
412 : {
413 : // if column count changes, formulas would become invalid anyway
414 : // -> only set nFormulaCols for unchanged column count
415 :
416 0 : SCCOL nTestCol = rParam.nCol2 + 1; // right of the data
417 0 : SCROW nTestRow = rParam.nRow1 + 1; // below the title row
418 0 : while ( nTestCol <= MAXCOL &&
419 0 : pDoc->GetCellType(ScAddress( nTestCol, nTestRow, nTab )) == CELLTYPE_FORMULA )
420 0 : ++nTestCol, ++nFormulaCols;
421 : }
422 :
423 4 : if (bSuccess)
424 : {
425 : // old and new range editable?
426 4 : ScEditableTester aTester;
427 4 : aTester.TestBlock( pDoc, nTab, rParam.nCol1,rParam.nRow1,rParam.nCol2,rParam.nRow2 );
428 4 : aTester.TestBlock( pDoc, nTab, rParam.nCol1,rParam.nRow1,nEndCol,nEndRow );
429 4 : if ( !aTester.IsEditable() )
430 : {
431 0 : nErrStringId = aTester.GetMessageId();
432 0 : bSuccess = false;
433 : }
434 4 : else if ( (pChangeTrack = pDoc->GetChangeTrack()) != NULL )
435 0 : aChangedRange = ScRange(rParam.nCol1, rParam.nRow1, nTab,
436 0 : nEndCol+nFormulaCols, nEndRow, nTab );
437 : }
438 :
439 4 : if ( bSuccess && bMoveCells )
440 : {
441 : ScRange aOld( rParam.nCol1, rParam.nRow1, nTab,
442 0 : rParam.nCol2+nFormulaCols, rParam.nRow2, nTab );
443 : ScRange aNew( rParam.nCol1, rParam.nRow1, nTab,
444 0 : nEndCol+nFormulaCols, nEndRow, nTab );
445 0 : if (!pDoc->CanFitBlock( aOld, aNew ))
446 : {
447 0 : nErrStringId = STR_MSSG_DOSUBTOTALS_2; // can't insert cells
448 0 : bSuccess = false;
449 : }
450 : }
451 :
452 : //
453 : // copy data from import doc into real document
454 : //
455 :
456 4 : if ( bSuccess )
457 : {
458 4 : if (bKeepFormat)
459 : {
460 : // keep formatting of title and first data row from the document
461 : // CopyToDocument also copies styles, Apply... needs separate calls
462 :
463 0 : SCCOL nMinEndCol = std::min( rParam.nCol2, nEndCol ); // not too much
464 0 : nMinEndCol = sal::static_int_cast<SCCOL>( nMinEndCol + nFormulaCols ); // only if column count unchanged
465 0 : pImportDoc->DeleteAreaTab( 0,0, MAXCOL,MAXROW, nTab, IDF_ATTRIB );
466 : pDoc->CopyToDocument( rParam.nCol1, rParam.nRow1, nTab,
467 : nMinEndCol, rParam.nRow1, nTab,
468 0 : IDF_ATTRIB, false, pImportDoc );
469 :
470 0 : SCROW nDataStartRow = rParam.nRow1+1;
471 0 : for (SCCOL nCopyCol=rParam.nCol1; nCopyCol<=nMinEndCol; nCopyCol++)
472 : {
473 : const ScPatternAttr* pSrcPattern = pDoc->GetPattern(
474 0 : nCopyCol, nDataStartRow, nTab );
475 : pImportDoc->ApplyPatternAreaTab( nCopyCol, nDataStartRow, nCopyCol, nEndRow,
476 0 : nTab, *pSrcPattern );
477 0 : const ScStyleSheet* pStyle = pSrcPattern->GetStyleSheet();
478 0 : if (pStyle)
479 : pImportDoc->ApplyStyleAreaTab( nCopyCol, nDataStartRow, nCopyCol, nEndRow,
480 0 : nTab, *pStyle );
481 : }
482 : }
483 :
484 : // don't set cell protection attribute if table is protected
485 4 : if (pDoc->IsTabProtected(nTab))
486 : {
487 0 : ScPatternAttr aPattern(pImportDoc->GetPool());
488 0 : aPattern.GetItemSet().Put( ScProtectionAttr( false,false,false,false ) );
489 0 : pImportDoc->ApplyPatternAreaTab( 0,0,MAXCOL,MAXROW, nTab, aPattern );
490 : }
491 :
492 : //
493 : // copy old data for undo
494 : //
495 :
496 4 : SCCOL nUndoEndCol = std::max( nEndCol, rParam.nCol2 ); // rParam = old end
497 4 : SCROW nUndoEndRow = std::max( nEndRow, rParam.nRow2 );
498 :
499 4 : ScDocument* pUndoDoc = NULL;
500 4 : ScDBData* pUndoDBData = NULL;
501 4 : if ( bRecord )
502 : {
503 4 : pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
504 4 : pUndoDoc->InitUndo( pDoc, nTab, nTab );
505 :
506 4 : if ( !bAddrInsert )
507 4 : pUndoDBData = new ScDBData( *pDBData );
508 : }
509 :
510 4 : ScMarkData aNewMark;
511 4 : aNewMark.SelectOneTable( nTab );
512 :
513 4 : if (bRecord)
514 : {
515 : // do not touch notes (ScUndoImportData does not support drawing undo)
516 4 : sal_uInt16 nCopyFlags = IDF_ALL & ~IDF_NOTE;
517 :
518 : // nFormulaCols is set only if column count is unchanged
519 : pDoc->CopyToDocument( rParam.nCol1, rParam.nRow1, nTab,
520 : nEndCol+nFormulaCols, nEndRow, nTab,
521 4 : nCopyFlags, false, pUndoDoc );
522 4 : if ( rParam.nCol2 > nEndCol )
523 : pDoc->CopyToDocument( nEndCol+1, rParam.nRow1, nTab,
524 : nUndoEndCol, nUndoEndRow, nTab,
525 3 : nCopyFlags, false, pUndoDoc );
526 4 : if ( rParam.nRow2 > nEndRow )
527 : pDoc->CopyToDocument( rParam.nCol1, nEndRow+1, nTab,
528 : nUndoEndCol+nFormulaCols, nUndoEndRow, nTab,
529 2 : nCopyFlags, false, pUndoDoc );
530 : }
531 :
532 : //
533 : // move new data
534 : //
535 :
536 4 : if (bMoveCells)
537 : {
538 : // clear only the range without the formulas,
539 : // so the formula title and first row are preserved
540 :
541 : ScRange aDelRange( rParam.nCol1, rParam.nRow1, nTab,
542 0 : rParam.nCol2, rParam.nRow2, nTab );
543 0 : pDoc->DeleteAreaTab( aDelRange, IDF_ALL & ~IDF_NOTE ); // ohne die Formeln
544 :
545 : ScRange aOld( rParam.nCol1, rParam.nRow1, nTab,
546 0 : rParam.nCol2+nFormulaCols, rParam.nRow2, nTab );
547 : ScRange aNew( rParam.nCol1, rParam.nRow1, nTab,
548 0 : nEndCol+nFormulaCols, nEndRow, nTab );
549 0 : pDoc->FitBlock( aOld, aNew, false ); // Formeln nicht loeschen
550 : }
551 4 : else if ( nEndCol < rParam.nCol2 ) // DeleteArea calls PutInOrder
552 : pDoc->DeleteArea( nEndCol+1, rParam.nRow1, rParam.nCol2, rParam.nRow2,
553 3 : aNewMark, IDF_CONTENTS & ~IDF_NOTE );
554 :
555 : // CopyToDocument doesn't remove contents
556 4 : pDoc->DeleteAreaTab( rParam.nCol1, rParam.nRow1, nEndCol, nEndRow, nTab, IDF_CONTENTS & ~IDF_NOTE );
557 :
558 : // remove each column from ImportDoc after copying to reduce memory usage
559 4 : sal_Bool bOldAutoCalc = pDoc->GetAutoCalc();
560 4 : pDoc->SetAutoCalc( false ); // outside of the loop
561 38 : for (SCCOL nCopyCol = rParam.nCol1; nCopyCol <= nEndCol; nCopyCol++)
562 : {
563 : pImportDoc->CopyToDocument( nCopyCol, rParam.nRow1, nTab, nCopyCol, nEndRow, nTab,
564 34 : IDF_ALL, false, pDoc );
565 34 : pImportDoc->DeleteAreaTab( nCopyCol, rParam.nRow1, nCopyCol, nEndRow, nTab, IDF_CONTENTS );
566 : }
567 4 : pDoc->SetAutoCalc( bOldAutoCalc );
568 :
569 4 : if (nFormulaCols > 0) // copy formulas
570 : {
571 0 : if (bKeepFormat) // formats for formulas
572 : pImportDoc->CopyToDocument( nEndCol+1, rParam.nRow1, nTab,
573 : nEndCol+nFormulaCols, nEndRow, nTab,
574 0 : IDF_ATTRIB, false, pDoc );
575 : // fill formulas
576 0 : ScMarkData aMark;
577 0 : aMark.SelectOneTable(nTab);
578 :
579 0 : sal_uLong nProgCount = nFormulaCols;
580 0 : nProgCount *= nEndRow-rParam.nRow1-1;
581 : ScProgress aProgress( pDoc->GetDocumentShell(),
582 0 : ScGlobal::GetRscString(STR_FILL_SERIES_PROGRESS), nProgCount );
583 :
584 : pDoc->Fill( nEndCol+1, rParam.nRow1+1, nEndCol+nFormulaCols, rParam.nRow1+1,
585 0 : &aProgress, aMark, nEndRow-rParam.nRow1-1, FILL_TO_BOTTOM, FILL_SIMPLE );
586 : }
587 :
588 : // if new range is smaller, clear old contents
589 :
590 4 : if (!bMoveCells) // move has happened above
591 : {
592 4 : if ( rParam.nCol2 > nEndCol )
593 : pDoc->DeleteArea( nEndCol+1, rParam.nRow1, rParam.nCol2, rParam.nRow2,
594 3 : aNewMark, IDF_CONTENTS );
595 4 : if ( rParam.nRow2 > nEndRow )
596 : pDoc->DeleteArea( rParam.nCol1, nEndRow+1, rParam.nCol2, rParam.nRow2,
597 2 : aNewMark, IDF_CONTENTS );
598 : }
599 :
600 4 : if( !bAddrInsert ) // update database range
601 : {
602 4 : pDBData->SetImportParam( rParam );
603 4 : pDBData->SetHeader( sal_True );
604 4 : pDBData->SetByRow( sal_True );
605 4 : pDBData->SetArea( nTab, rParam.nCol1,rParam.nRow1, nEndCol,nEndRow );
606 4 : pDBData->SetImportSelection( bRealSelection );
607 4 : pDoc->CompileDBFormula();
608 : }
609 :
610 4 : if (bRecord)
611 : {
612 4 : ScDocument* pRedoDoc = pImportDoc;
613 4 : pImportDoc = NULL;
614 :
615 4 : if (nFormulaCols > 0) // include filled formulas for redo
616 : pDoc->CopyToDocument( rParam.nCol1, rParam.nRow1, nTab,
617 : nEndCol+nFormulaCols, nEndRow, nTab,
618 0 : IDF_ALL & ~IDF_NOTE, false, pRedoDoc );
619 :
620 4 : ScDBData* pRedoDBData = pDBData ? new ScDBData( *pDBData ) : NULL;
621 :
622 4 : rDocShell.GetUndoManager()->AddUndoAction(
623 : new ScUndoImportData( &rDocShell, nTab,
624 : rParam, nUndoEndCol, nUndoEndRow,
625 : nFormulaCols,
626 4 : pUndoDoc, pRedoDoc, pUndoDBData, pRedoDBData ) );
627 : }
628 :
629 4 : pDoc->SetDirty();
630 4 : rDocShell.PostPaint(ScRange(0, 0, nTab, MAXCOL, MAXROW, nTab), PAINT_GRID);
631 4 : aModificator.SetDocumentModified();
632 :
633 8 : ScDBRangeRefreshedHint aHint( rParam );
634 4 : pDoc->BroadcastUno( aHint );
635 :
636 4 : if (pWaitWin)
637 4 : pWaitWin->LeaveWait();
638 :
639 4 : if ( bTruncated && !bApi ) // show warning
640 4 : ErrorHandler::HandleError(SCWARN_IMPORT_RANGE_OVERFLOW);
641 : }
642 0 : else if ( !bApi )
643 : {
644 0 : if (pWaitWin)
645 0 : pWaitWin->LeaveWait();
646 :
647 0 : if (!aErrorMessage.Len())
648 : {
649 0 : if (!nErrStringId)
650 0 : nErrStringId = STR_MSSG_IMPORTDATA_0;
651 0 : aErrorMessage = ScGlobal::GetRscString( nErrStringId );
652 : }
653 0 : InfoBox aInfoBox( rDocShell.GetActiveDialogParent(), aErrorMessage );
654 0 : aInfoBox.Execute();
655 : }
656 :
657 4 : delete pImportDoc;
658 :
659 4 : if (bSuccess && pChangeTrack)
660 0 : pChangeTrack->AppendInsert ( aChangedRange );
661 :
662 8 : return bSuccess;
663 93 : }
664 :
665 :
666 :
667 :
668 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|