From b7ea66f19aa02570e5517b44be169eb164562496 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Caol=C3=A1n=20McNamara?= Date: Wed, 14 Jul 2021 20:36:56 +0100 Subject: [PATCH] Related: rhbz#1980800 allow convert to csv to write each sheet to a separate file --- ...ow-convert-to-csv-to-write-each-shee.patch | 307 ++++++++++++++++++ libreoffice.spec | 7 +- 2 files changed, 313 insertions(+), 1 deletion(-) create mode 100644 0001-rhbz-1980800-allow-convert-to-csv-to-write-each-shee.patch diff --git a/0001-rhbz-1980800-allow-convert-to-csv-to-write-each-shee.patch b/0001-rhbz-1980800-allow-convert-to-csv-to-write-each-shee.patch new file mode 100644 index 0000000..67ff1f9 --- /dev/null +++ b/0001-rhbz-1980800-allow-convert-to-csv-to-write-each-shee.patch @@ -0,0 +1,307 @@ +From c4961f30f3ab5ea1efd1fc143fac0a22d6a4a2a9 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Caol=C3=A1n=20McNamara?= +Date: Tue, 13 Jul 2021 12:38:07 +0100 +Subject: [PATCH] rhbz#1980800 allow --convert-to csv to write each sheet to a + separate file + +Related: tdf#135762 except only currently implemented for command line use + +sample usage: +soffice --convert-to csv:"Text - txt - csv (StarCalc)":44,34,UTF8,1,,0,false,true,false,false,false,-1 sample.ods +where the new (11th!) final token ("-1") enables writing each sheet to a +new file based on the suggested target name so output in this example +is files sample-Sheet1.csv and sample-Sheet2.csv + +Only -1 for 'all sheets' vs 0 for existing 'current sheet only' (which +is always sheet 0 from the command line) are currently options but the +token could be expanded in the future to select specific sheets to +export. + +Change-Id: Ib99a120f1a2c8d1008a7a3c59a6b39f572fb346e +Reviewed-on: https://gerrit.libreoffice.org/c/core/+/118850 +Tested-by: Jenkins +Reviewed-by: Eike Rathke +(cherry picked from commit b8903bc106dad036acb3d117e5c4fc955697fe02) +--- + desktop/source/app/dispatchwatcher.cxx | 26 +++++-- + sc/source/ui/dbgui/imoptdlg.cxx | 7 +- + sc/source/ui/docshell/docsh.cxx | 97 +++++++++++++++++++------- + sc/source/ui/inc/docsh.hxx | 2 +- + sc/source/ui/inc/imoptdlg.hxx | 6 +- + 5 files changed, 106 insertions(+), 32 deletions(-) + +diff --git a/desktop/source/app/dispatchwatcher.cxx b/desktop/source/app/dispatchwatcher.cxx +index 50b92ecb7834..0660ccf3f83e 100644 +--- a/desktop/source/app/dispatchwatcher.cxx ++++ b/desktop/source/app/dispatchwatcher.cxx +@@ -30,6 +30,7 @@ + #include "officeipcthread.hxx" + #include + #include ++#include + #include + #include + #include +@@ -598,6 +599,8 @@ bool DispatchWatcher::executeDispatchRequests( const std::vector " << aTargetURL8; ++ std::cout << "convert " << aSource8; ++ if (!bMultiFileTarget) ++ std::cout << " -> " << aTargetURL8; + std::cout << " using filter : " << OUStringToOString(aFilter, osl_getThreadTextEncoding()) << std::endl; +- if (FStatHelper::IsDocument(aOutFile)) ++ if (!bMultiFileTarget && FStatHelper::IsDocument(aOutFile)) + std::cout << "Overwriting: " << OUStringToOString(aTempName, osl_getThreadTextEncoding()) << std::endl ; + } + try +diff --git a/sc/source/ui/dbgui/imoptdlg.cxx b/sc/source/ui/dbgui/imoptdlg.cxx +index 071f1b0257bc..d8c4fd810ea3 100644 +--- a/sc/source/ui/dbgui/imoptdlg.cxx ++++ b/sc/source/ui/dbgui/imoptdlg.cxx +@@ -43,6 +43,7 @@ ScImportOptions::ScImportOptions( const OUString& rStr ) + bSaveNumberAsSuch = true; + bSaveFormulas = false; + bRemoveSpace = false; ++ bNewFilePerSheet = false; + sal_Int32 nTokenCount = comphelper::string::getTokenCount(rStr, ','); + if ( nTokenCount < 3 ) + return; +@@ -77,6 +78,8 @@ ScImportOptions::ScImportOptions( const OUString& rStr ) + bSaveFormulas = rStr.getToken(0, ',', nIdx) == "true"; + if ( nTokenCount >= 11 ) + bRemoveSpace = rStr.getToken(0, ',', nIdx) == "true"; ++ if ( nTokenCount >= 12 ) ++ bNewFilePerSheet = rStr.getToken(0, ',', nIdx) == "-1"; + } + } + +@@ -99,7 +102,9 @@ OUString ScImportOptions::BuildString() const + "," + + OUString::boolean( bSaveFormulas ) + // "save formulas": not in ScAsciiOptions + "," + +- OUString::boolean( bRemoveSpace ); // same as "Remove space" in ScAsciiOptions ++ OUString::boolean( bRemoveSpace ) + // same as "Remove space" in ScAsciiOptions ++ "," + ++ std::u16string_view(bNewFilePerSheet ? u"-1" : u"0") ; // Only available for command line --convert-to + + return aResult; + } +diff --git a/sc/source/ui/docshell/docsh.cxx b/sc/source/ui/docshell/docsh.cxx +index 91020db0b2e0..7cee6bb7f32e 100644 +--- a/sc/source/ui/docshell/docsh.cxx ++++ b/sc/source/ui/docshell/docsh.cxx +@@ -44,6 +44,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -119,6 +120,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -1925,7 +1927,7 @@ void escapeTextSep(sal_Int32 nPos, const StrT& rStrDelim, StrT& rStr) + + } + +-void ScDocShell::AsciiSave( SvStream& rStream, const ScImportOptions& rAsciiOpt ) ++void ScDocShell::AsciiSave( SvStream& rStream, const ScImportOptions& rAsciiOpt, SCTAB nTab ) + { + sal_Unicode cDelim = rAsciiOpt.nFieldSepCode; + sal_Unicode cStrDelim = rAsciiOpt.nTextSepCode; +@@ -1971,7 +1973,6 @@ void ScDocShell::AsciiSave( SvStream& rStream, const ScImportOptions& rAsciiOpt + + SCCOL nStartCol = 0; + SCROW nStartRow = 0; +- SCTAB nTab = GetSaveTab(); + SCCOL nEndCol; + SCROW nEndRow; + m_aDocument.GetCellArea( nTab, nEndCol, nEndRow ); +@@ -2389,35 +2390,81 @@ bool ScDocShell::ConvertTo( SfxMedium &rMed ) + } + else if (aFltName == pFilterAscii) + { +- SvStream* pStream = rMed.GetOutStream(); +- if (pStream) ++ OUString sItStr; ++ SfxItemSet* pSet = rMed.GetItemSet(); ++ const SfxPoolItem* pItem; ++ if ( pSet && SfxItemState::SET == ++ pSet->GetItemState( SID_FILE_FILTEROPTIONS, true, &pItem ) ) + { +- OUString sItStr; +- SfxItemSet* pSet = rMed.GetItemSet(); +- const SfxPoolItem* pItem; +- if ( pSet && SfxItemState::SET == +- pSet->GetItemState( SID_FILE_FILTEROPTIONS, true, &pItem ) ) +- { +- sItStr = static_cast(pItem)->GetValue(); +- } ++ sItStr = static_cast(pItem)->GetValue(); ++ } + +- if ( sItStr.isEmpty() ) +- { +- // default for ascii export (from API without options): +- // ISO8859-1/MS_1252 encoding, comma, double quotes ++ if ( sItStr.isEmpty() ) ++ { ++ // default for ascii export (from API without options): ++ // ISO8859-1/MS_1252 encoding, comma, double quotes + +- ScImportOptions aDefOptions( ',', '"', RTL_TEXTENCODING_MS_1252 ); +- sItStr = aDefOptions.BuildString(); +- } ++ ScImportOptions aDefOptions( ',', '"', RTL_TEXTENCODING_MS_1252 ); ++ sItStr = aDefOptions.BuildString(); ++ } + +- weld::WaitObject aWait( GetActiveDialogParent() ); +- ScImportOptions aOptions( sItStr ); +- AsciiSave( *pStream, aOptions ); ++ weld::WaitObject aWait( GetActiveDialogParent() ); ++ ScImportOptions aOptions( sItStr ); ++ ++ if (aOptions.bNewFilePerSheet) ++ { + bRet = true; + +- if (m_aDocument.GetTableCount() > 1) +- if (!rMed.GetError()) +- rMed.SetError(SCWARN_EXPORT_ASCII); ++ INetURLObject aURLObject(rMed.GetURLObject()); ++ OUString sExt = aURLObject.CutExtension(); ++ OUString sBaseName = aURLObject.GetLastName(); ++ aURLObject.CutLastName(); ++ ++ for (SCTAB i = 0, nCount = m_aDocument.GetTableCount(); i < nCount; ++i) ++ { ++ OUString sTabName; ++ if (!m_aDocument.GetName(i, sTabName)) ++ sTabName = OUString::number(i); ++ INetURLObject aSheetURLObject(aURLObject); ++ OUString sFileName = sBaseName + "-" + sTabName; ++ if (!sExt.isEmpty()) ++ sFileName = sFileName + "." + sExt; ++ aSheetURLObject.Append(sFileName); ++ ++ // log similar to DispatchWatcher::executeDispatchRequests ++ OUString aOutFile = aSheetURLObject.GetMainURL(INetURLObject::DecodeMechanism::NONE); ++ OUString aDisplayedName; ++ if (osl::FileBase::E_None != osl::FileBase::getSystemPathFromFileURL(aOutFile, aDisplayedName)) ++ aDisplayedName = aOutFile; ++ std::cout << "Writing sheet " << OUStringToOString(sTabName, osl_getThreadTextEncoding()) << " -> " ++ << OUStringToOString(aDisplayedName, osl_getThreadTextEncoding()) ++ << std::endl; ++ ++ if (FStatHelper::IsDocument(aOutFile)) ++ std::cout << "Overwriting: " << OUStringToOString(aDisplayedName, osl_getThreadTextEncoding()) << std::endl ; ++ ++ std::unique_ptr xStm = ::utl::UcbStreamHelper::CreateStream(aOutFile, StreamMode::TRUNC | StreamMode::WRITE); ++ if (!xStm) ++ { ++ SetError(SCERR_IMPORT_UNKNOWN); ++ bRet = false; ++ break; ++ } ++ AsciiSave(*xStm, aOptions, i); ++ } ++ } ++ else ++ { ++ SvStream* pStream = rMed.GetOutStream(); ++ if (pStream) ++ { ++ AsciiSave(*pStream, aOptions, GetSaveTab()); ++ bRet = true; ++ ++ if (m_aDocument.GetTableCount() > 1) ++ if (!rMed.GetError()) ++ rMed.SetError(SCWARN_EXPORT_ASCII); ++ } + } + } + else if (aFltName == pFilterDBase) +diff --git a/sc/source/ui/inc/docsh.hxx b/sc/source/ui/inc/docsh.hxx +index 41c0b30a42a8..3aa5f6caf311 100644 +--- a/sc/source/ui/inc/docsh.hxx ++++ b/sc/source/ui/inc/docsh.hxx +@@ -227,7 +227,7 @@ public: + + ScDrawLayer* MakeDrawLayer(); + +- void AsciiSave( SvStream& rStream, const ScImportOptions& rOpt ); ++ void AsciiSave( SvStream& rStream, const ScImportOptions& rOpt, SCTAB nTab ); + + void Execute( SfxRequest& rReq ); + void GetState( SfxItemSet &rSet ); +diff --git a/sc/source/ui/inc/imoptdlg.hxx b/sc/source/ui/inc/imoptdlg.hxx +index bac941c2a377..2a4cba786b6e 100644 +--- a/sc/source/ui/inc/imoptdlg.hxx ++++ b/sc/source/ui/inc/imoptdlg.hxx +@@ -32,7 +32,8 @@ public: + ScImportOptions( sal_Unicode nFieldSep, sal_Unicode nTextSep, rtl_TextEncoding nEnc ) + : nFieldSepCode(nFieldSep), nTextSepCode(nTextSep), + bFixedWidth(false), bSaveAsShown(false), bQuoteAllText(false), +- bSaveNumberAsSuch(true), bSaveFormulas(false), bRemoveSpace(false) ++ bSaveNumberAsSuch(true), bSaveFormulas(false), bRemoveSpace(false), ++ bNewFilePerSheet(false) + { SetTextEncoding( nEnc ); } + + ScImportOptions& operator=( const ScImportOptions& rCpy ) = default; +@@ -51,6 +52,9 @@ public: + bool bSaveNumberAsSuch; + bool bSaveFormulas; + bool bRemoveSpace; ++ // currently only "0" for 'current sheet' and "-1" for all sheets (each to ++ // a separate file) are options ++ bool bNewFilePerSheet; + }; + + #endif // INCLUDED_SC_SOURCE_UI_INC_IMOPTDLG_HXX +-- +2.31.1 + diff --git a/libreoffice.spec b/libreoffice.spec index 09c54e3..321671e 100644 --- a/libreoffice.spec +++ b/libreoffice.spec @@ -50,7 +50,7 @@ Summary: Free Software Productivity Suite Name: libreoffice Epoch: 1 Version: %{libo_version}.2 -Release: 2%{?libo_prerelease}%{?dist} +Release: 3%{?libo_prerelease}%{?dist} License: (MPLv1.1 or LGPLv3+) and LGPLv3 and LGPLv2+ and BSD and (MPLv1.1 or GPLv2 or LGPLv2 or Netscape) and Public Domain and ASL 2.0 and MPLv2.0 and CC0 URL: http://www.libreoffice.org/ @@ -263,6 +263,7 @@ Patch13: 0001-allow-system-firebird-4.patch Patch14: 0001-Remove-unused-DOCTYPE-from-odk-examples-xcu-file.patch Patch15: 0001-math.desktop-include-Spreadsheet-category.patch Patch16: 0001-add-missing-xmlns-loext-to-example_sl-SI.xml.patch +Patch17: 0001-rhbz-1980800-allow-convert-to-csv-to-write-each-shee.patch # not upstreamed Patch500: 0001-disable-libe-book-support.patch @@ -2248,6 +2249,10 @@ gtk-update-icon-cache -q %{_datadir}/icons/hicolor &>/dev/null || : %{_includedir}/LibreOfficeKit %changelog +* Wed Jul 14 2021 Caolán McNamara - 1:7.1.4.2-3 +- Related: rhbz#1980800 allow convert to csv to write each sheet to + a separate file + * Wed Jun 30 2021 Caolán McNamara - 1:7.1.4.2-2 - Remove unused DOCTYPE from odk/examples xcu file - drop bsh buildrequires in rhel