[Libreoffice-commits] core.git: desktop/source sc/source

Eike Rathke (via logerrit) logerrit at kemper.freedesktop.org
Thu Jul 15 11:02:38 UTC 2021


 desktop/source/app/dispatchwatcher.cxx |   13 ++++++++++--
 sc/source/ui/dbgui/imoptdlg.cxx        |   15 +++++++++++---
 sc/source/ui/docshell/docsh.cxx        |   34 ++++++++++++++++++++++++++++++---
 sc/source/ui/inc/imoptdlg.hxx          |    8 +++----
 4 files changed, 58 insertions(+), 12 deletions(-)

New commits:
commit fda91f8be16ba760e360940ebafd6244c648cb8c
Author:     Eike Rathke <erack at redhat.com>
AuthorDate: Thu Jul 15 10:37:57 2021 +0200
Commit:     Eike Rathke <erack at redhat.com>
CommitDate: Thu Jul 15 13:02:04 2021 +0200

    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 at redhat.com>
    Tested-by: Jenkins

diff --git a/desktop/source/app/dispatchwatcher.cxx b/desktop/source/app/dispatchwatcher.cxx
index 8541c273cc83..1c4d878614c5 100644
--- a/desktop/source/app/dispatchwatcher.cxx
+++ b/desktop/source/app/dispatchwatcher.cxx
@@ -625,11 +625,20 @@ bool DispatchWatcher::executeDispatchRequests( const std::vector<DispatchRequest
                                     if (sFilterName == "Text - txt - csv (StarCalc)")
                                     {
                                         sal_Int32 nIdx(0);
-                                        // If the 11th token token is '-1' then we export a file
+                                        // If the 11th token is '-1' then we export a file
                                         // per sheet where the file name is based on the suggested
                                         // output filename concatenated with the sheet name, so adjust
                                         // the output and overwrite messages
-                                        bMultiFileTarget = sFilterOptions.getToken(11, ',', nIdx) == "-1";
+                                        // If the 11th token is not present or numeric 0 then the
+                                        // default sheet is exported with the output filename. If it
+                                        // is numeric >0 then that sheet (1-based) with the output
+                                        // filename concatenated with the sheet name. So even if
+                                        // that is a single file, the multi file target mechanism is
+                                        // used.
+                                        const OUString aTok(sFilterOptions.getToken(11, ',', nIdx));
+                                        // Actual validity is checked in Calc, here just check for
+                                        // presence of numeric value at start.
+                                        bMultiFileTarget = (!aTok.isEmpty() && aTok.toInt32() != 0);
                                     }
 
                                     conversionProperties[1].Value <<= sFilterName;
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: */


More information about the Libreoffice-commits mailing list