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