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