sc/inc/stringutil.hxx           |    2 +-
 sc/qa/unit/ucalc.cxx            |    4 ++--
 sc/source/core/data/column3.cxx |   22 +++++-----------------
 sc/source/ui/view/tabvwsha.cxx  |   17 +++++++++--------
 4 files changed, 17 insertions(+), 28 deletions(-)

New commits:
commit 939724fe6abd16015dbef6c476f7f57a2dc466d8
Author:     Eike Rathke <er...@redhat.com>
AuthorDate: Wed Sep 14 15:01:47 2022 +0200
Commit:     Eike Rathke <er...@redhat.com>
CommitDate: Wed Sep 14 21:44:32 2022 +0200

    Related: tdf#149665 Unify input of a leading ' apostrophe in non-Text cell
    
    If a cell is not formatted as Text and
    ScSetStringParam::mbHandleApostrophe is true, all input (except
    one single apostrophe that otherwise would lead to an empty cell
    string) now is treated the same and the leading apostrophe is
    removed and the remainder is set as text content. The Input Line
    and editing a cell get an apostrophe prepended for the cases
    needed to generate such escaped string again.
    
    This made it necessary to adapt a unit test that exactly checked
    the old behaviour.
    
    Change-Id: I07b35d33f3704970784dc6de1322bda28903bb97
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/139934
    Reviewed-by: Eike Rathke <er...@redhat.com>
    Tested-by: Jenkins

diff --git a/sc/inc/stringutil.hxx b/sc/inc/stringutil.hxx
index 150a3ede269e..b2b58f60fc5a 100644
--- a/sc/inc/stringutil.hxx
+++ b/sc/inc/stringutil.hxx
@@ -84,7 +84,7 @@ struct SAL_WARN_UNUSED SC_DLLPUBLIC ScSetStringParam
 
     /**
      * When true, treat input with a leading apostrophe as an escape character
-     * for a numeric value content, to treat the numeric value as a text. When
+     * for all content, to treat also numeric value as a text. When
      * false, the whole string input including the leading apostrophe will be
      * entered literally as string.
      */
diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx
index 87ab1c55155e..a63519f41224 100644
--- a/sc/qa/unit/ucalc.cxx
+++ b/sc/qa/unit/ucalc.cxx
@@ -626,8 +626,8 @@ void Test::testInput()
     CPPUNIT_ASSERT_MESSAGE("String number should have the first apostrophe 
stripped.", bTest);
     m_pDoc->SetString(0, 0, 0, "'apple'");
     test = m_pDoc->GetString(0, 0, 0);
-    bTest = test == "'apple'";
-    CPPUNIT_ASSERT_MESSAGE("Text content should have retained the first 
apostrophe.", bTest);
+    bTest = test == "apple'";
+    CPPUNIT_ASSERT_MESSAGE("Text content should have the first apostrophe 
stripped.", bTest);
 
     // Customized string handling policy.
     ScSetStringParam aParam;
diff --git a/sc/source/core/data/column3.cxx b/sc/source/core/data/column3.cxx
index e144a0334036..d92318134098 100644
--- a/sc/source/core/data/column3.cxx
+++ b/sc/source/core/data/column3.cxx
@@ -2125,32 +2125,20 @@ bool ScColumn::ParseString(
     }
     else if ( cFirstChar == '\'') // 'Text
     {
-        bool bNumeric = false;
         if (aParam.mbHandleApostrophe)
         {
             // Cell format is not 'Text', and the first char is an apostrophe.
-            // Check if the input is considered a number with all leading
-            // apostrophes removed. All because ''1 should produce '1 not ''1,
-            // thus '''1 be ''1 and so on.
+            // Strip it and set text content.
             // NOTE: this corresponds with sc/source/ui/view/tabvwsha.cxx
             // ScTabViewShell::UpdateInputHandler() prepending an apostrophe if
             // necessary.
-            sal_Int32 i = 1;
-            while (i < rString.getLength() && rString[i] == '\'')
-                ++i;
-            if (i < rString.getLength())
-            {
-                OUString aTest = rString.copy(i);
-                double fTest;
-                bNumeric = aParam.mpNumFormatter->IsNumberFormat(aTest, 
nIndex, fTest);
-                if (bNumeric)
-                    // This is a number. Strip out the first apostrophe.
-                    rCell.set(rPool.intern(rString.copy(1)));
-            }
+            rCell.set(rPool.intern(rString.copy(1)));
         }
-        if (!bNumeric)
+        else
+        {
             // This is normal text. Take it as-is.
             rCell.set(rPool.intern(rString));
+        }
     }
     else
     {
diff --git a/sc/source/ui/view/tabvwsha.cxx b/sc/source/ui/view/tabvwsha.cxx
index dc389eb70723..9e86e5319de9 100644
--- a/sc/source/ui/view/tabvwsha.cxx
+++ b/sc/source/ui/view/tabvwsha.cxx
@@ -722,19 +722,20 @@ void ScTabViewShell::UpdateInputHandler( bool bForce /* = 
sal_False */, bool bSt
                 aString = ScCellFormat::GetInputString( rCell, nNumFmt, 
*pFormatter, rDoc );
                 if (rCell.getType() == CELLTYPE_STRING)
                 {
-                    sal_Int32 i = 0;
-                    while (i < aString.getLength() && aString[i] == '\'')
-                        ++i;
-                    OUString aTest((i && i < aString.getLength()) ? 
aString.copy(i) : aString);
                     // Put a ' in front if necessary, so that the string is not
                     // unintentionally interpreted as a number, and to show the
                     // user that it is a string (#35060#).
+                    // If cell is not formatted as Text, a leading apostrophe
+                    // needs another prepended, also '=' or '+' or '-'
+                    // otherwise starting a formula.
                     // NOTE: this corresponds with
                     // sc/source/core/data/column3.cxx ScColumn::ParseString()
-                    // removing one apostrophe also for multiple consecutive
-                    // apostrophes.
-                    // For number format 'Text' this never results in numeric.
-                    if (pFormatter->IsNumberFormat(aTest, nNumFmt, 
o3tl::temporary(double())))
+                    // removing one apostrophe.
+                    // For number format Text IsNumberFormat() would never
+                    // result in numeric anyway.
+                    if (!pFormatter->IsTextFormat(nNumFmt) && 
(aString.startsWith("'")
+                                || aString.startsWith("=") || 
aString.startsWith("+") || aString.startsWith("-")
+                                || pFormatter->IsNumberFormat(aString, 
nNumFmt, o3tl::temporary(double()))))
                         aString = "'" + aString;
                 }
             }

Reply via email to