diff options
author | Eike Rathke <erack@redhat.com> | 2021-07-15 10:37:57 +0200 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2021-07-15 13:02:04 +0200 |
commit | fda91f8be16ba760e360940ebafd6244c648cb8c (patch) | |
tree | 35c56bba166691e0ad63792c69c940fe6cf7919e /sc | |
parent | 246c766cadcafc5d26f39f832a683a13f2dfe40b (diff) |
Related: tdf#135762 Allow --convert-to csv to specify 1-based sheet number
Same multifile mechanism as for -1 all sheets is used, so
soffice --convert-to csv:"Text - txt - csv (StarCalc)":44,34,UTF8,1,,0,false,true,false,false,false,2 sample.ods
writes a file sample-Sheet2.csv
Change-Id: Ib9248c9561e4e340c88458ac5dfd159e443a4cfd
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/118971
Reviewed-by: Eike Rathke <erack@redhat.com>
Tested-by: Jenkins
Diffstat (limited to 'sc')
-rw-r--r-- | sc/source/ui/dbgui/imoptdlg.cxx | 15 | ||||
-rw-r--r-- | sc/source/ui/docshell/docsh.cxx | 34 | ||||
-rw-r--r-- | sc/source/ui/inc/imoptdlg.hxx | 8 |
3 files changed, 47 insertions, 10 deletions
diff --git a/sc/source/ui/dbgui/imoptdlg.cxx b/sc/source/ui/dbgui/imoptdlg.cxx index d8c4fd810ea3..a362e4df0ee7 100644 --- a/sc/source/ui/dbgui/imoptdlg.cxx +++ b/sc/source/ui/dbgui/imoptdlg.cxx @@ -20,6 +20,7 @@ #include <imoptdlg.hxx> #include <asciiopt.hxx> #include <comphelper/string.hxx> +#include <unotools/charclass.hxx> #include <osl/thread.h> #include <global.hxx> @@ -43,7 +44,7 @@ ScImportOptions::ScImportOptions( const OUString& rStr ) bSaveNumberAsSuch = true; bSaveFormulas = false; bRemoveSpace = false; - bNewFilePerSheet = false; + nSheetToExport = 0; sal_Int32 nTokenCount = comphelper::string::getTokenCount(rStr, ','); if ( nTokenCount < 3 ) return; @@ -79,7 +80,15 @@ ScImportOptions::ScImportOptions( const OUString& rStr ) if ( nTokenCount >= 11 ) bRemoveSpace = rStr.getToken(0, ',', nIdx) == "true"; if ( nTokenCount >= 12 ) - bNewFilePerSheet = rStr.getToken(0, ',', nIdx) == "-1"; + { + const OUString aTok(rStr.getToken(0, ',', nIdx)); + if (aTok == "-1") + nSheetToExport = -1; // all + else if (aTok.isEmpty() || CharClass::isAsciiNumeric(aTok)) + nSheetToExport = aTok.toInt32(); + else + nSheetToExport = -23; // invalid, force error + } } } @@ -104,7 +113,7 @@ OUString ScImportOptions::BuildString() const "," + OUString::boolean( bRemoveSpace ) + // same as "Remove space" in ScAsciiOptions "," + - std::u16string_view(bNewFilePerSheet ? u"-1" : u"0") ; // Only available for command line --convert-to + OUString::number(nSheetToExport) ; // 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 873e5c598bab..21137a7bb1b6 100644 --- a/sc/source/ui/docshell/docsh.cxx +++ b/sc/source/ui/docshell/docsh.cxx @@ -2412,16 +2412,44 @@ bool ScDocShell::ConvertTo( SfxMedium &rMed ) weld::WaitObject aWait( GetActiveDialogParent() ); ScImportOptions aOptions( sItStr ); - if (aOptions.bNewFilePerSheet) + if (aOptions.nSheetToExport) { + // Only from command line --convert-to bRet = true; + SCTAB nStartTab; + SCTAB nCount = m_aDocument.GetTableCount(); + if (aOptions.nSheetToExport == -1) + { + // All sheets. + nStartTab = 0; + } + else if (0 < aOptions.nSheetToExport && aOptions.nSheetToExport <= nCount) + { + // One sheet, 1-based. + nCount = aOptions.nSheetToExport; + nStartTab = nCount - 1; + } + else + { + // Usage error, no export but log. + if (aOptions.nSheetToExport < 0) + std::cout << "Bad sheet number string given." << std::endl; + else + std::cout << "No sheet number " << OString::number(aOptions.nSheetToExport) + << ", number of sheets is " << nCount << std::endl; + nStartTab = 0; + nCount = 0; + SetError(SCERR_EXPORT_DATA); + bRet = false; + } + 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) + for (SCTAB i = nStartTab; i < nCount; ++i) { OUString sTabName; if (!m_aDocument.GetName(i, sTabName)) @@ -2447,7 +2475,7 @@ bool ScDocShell::ConvertTo( SfxMedium &rMed ) std::unique_ptr<SvStream> xStm = ::utl::UcbStreamHelper::CreateStream(aOutFile, StreamMode::TRUNC | StreamMode::WRITE); if (!xStm) { - SetError(SCERR_IMPORT_UNKNOWN); + SetError(ERRCODE_IO_CANTCREATE); bRet = false; break; } diff --git a/sc/source/ui/inc/imoptdlg.hxx b/sc/source/ui/inc/imoptdlg.hxx index 2314349f602a..935f271b0c5c 100644 --- a/sc/source/ui/inc/imoptdlg.hxx +++ b/sc/source/ui/inc/imoptdlg.hxx @@ -32,7 +32,7 @@ public: : nFieldSepCode(nFieldSep), nTextSepCode(nTextSep), bFixedWidth(false), bSaveAsShown(false), bQuoteAllText(false), bSaveNumberAsSuch(true), bSaveFormulas(false), bRemoveSpace(false), - bNewFilePerSheet(false) + nSheetToExport(0) { SetTextEncoding( nEnc ); } ScImportOptions& operator=( const ScImportOptions& rCpy ) = default; @@ -51,9 +51,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; + // "0" for 'current sheet', "-1" for all sheets (each to a separate file), + // or 1-based specific sheet number (to a separate file). + sal_Int32 nSheetToExport; }; /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |