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