fix(xlsx): set fullCalcOnLoad when named-range has formula body

Adds the LooksLikeFormulaBody helper used by the definedName Add path:
when the body is a function call or arithmetic expression (SUM(...),
A1+B1, IF(...), etc.) the CLI now flips <x:calcPr fullCalcOnLoad="1"/>
in workbook.xml so Excel computes the name on first open — previously
Excel displayed 0 until the user triggered a manual recalc.

Pure range refs (Sheet1!$A$1:$A$5) still do not force a recalc; they
evaluate lazily as references.
This commit is contained in:
zmworm 2026-04-19 01:41:48 +08:00
parent 4d29ce1603
commit 4adf1cbea1

View file

@ -453,6 +453,24 @@ public partial class ExcelHandler
return true;
}
// R7-3: heuristic — is `s` a formula body (SUM(...), A1+B1, IF(...)),
// as opposed to a pure range-ref body (Sheet1!$A$1:$A$5, A1:A5, A1)?
// Used to decide whether to flip <calcPr fullCalcOnLoad="1"/> so Excel
// evaluates the defined name on first open. Range-only bodies don't
// need forced recalc; function calls and operator expressions do.
internal static bool LooksLikeFormulaBody(string? s)
{
if (string.IsNullOrEmpty(s)) return false;
var t = s.Trim();
if (t.Length == 0) return false;
// A function call or arithmetic expression contains '(' or an
// operator outside a sheet-qualified range.
if (t.Contains('(')) return true;
if (t.IndexOfAny(new[] { '+', '-', '*', '/', '^', '&', '<', '>', '=', '%' }) >= 0)
return true;
return false;
}
// Make a string safe to use as an Excel table name, displayName, or
// tableColumn name. Excel refuses to open files where these identifiers
// look like a cell reference ("tbl1" → column TBL row 1) or are purely