SEPURI-SAI-KRISHNA commented on code in PR #45407:
URL: https://github.com/apache/superset/pull/45407#discussion_r4385078443


##########
superset/utils/excel.py:
##########
@@ -49,12 +49,36 @@
 
 
 def _quote_formula(value: Any) -> Any:
-    """Prefix a string with a quote when it would parse as a formula."""
-    return (
-        f"'{value}"
-        if isinstance(value, str) and len(value) and value[0] in 
FORMULA_PREFIXES
-        else value
-    )
+    """Prefix a string with a quote when it would parse as a formula.
+
+    A string whose first character is ``=`` is stored as a formula cell by the
+    writer, so quoting it is what keeps a formula out of the workbook. Dropping
+    the quote would put one in.
+
+    Leading whitespace is stripped before the prefix is read, so ``" =cmd"`` is
+    quoted like ``"=cmd"``. That part is for consistency across the export 
paths
+    rather than to stop the workbook evaluating it: whitespace in front of the
+    prefix stops XlsxWriter recognising a formula at all, so the value is
+    written as a plain string cell either way. The CSV writer
+    (``_starts_like_spreadsheet_formula``) and the streaming writer both strip
+    before reading the prefix, so without this the same cell exports quoted
+    from two of the three paths and bare from this one. What the quote guards
+    against there is how a downstream viewer or a re-import reads the text, not
+    how XlsxWriter stored it.
+
+    Which *unprefixed* strings the writer treats as formulas is a wider
+    question than ``FORMULA_PREFIXES``, and this guard does not answer it: a
+    string wrapped in ``{=`` and ``}`` is stored as an array formula, which
+    neither the quote nor the ``strings_to_formulas`` workbook option prevents.

Review Comment:
   You are right, and I had measured this and then written the opposite. Through
   `write_row`, `{=1+1}` stores as `<f t="array" ref="A1">1+1</f>` and `'{=1+1}`
   stores as a plain string, with `strings_to_formulas: False` changing neither.
   
   Your point about reaching for something heavier made me fix the vagueness as
   well, not just the false half. `_write_token_as_string` reaches
   `_write_formula` from exactly two branches: `startswith("=")`, gated on
   `strings_to_formulas`, and `startswith("{=") and endswith("}")`, gated on
   nothing. So the set is bounded, the quote is the right tool, and extending
   `FORMULA_PREFIXES` is the whole of the follow-up. The paragraph says that now
   instead of calling it a wider open question.
   
   Checked the other three places and the description; this sentence was the 
only
   one making the claim.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to