fix(xlsx): warn when value= and formula= are both supplied

When both value= and formula= are passed to Set or Add on the same
cell, formula wins (it is written last, clearing CellValue). Users who
typo'd or duplicated props previously had no indication the literal
value had been discarded.

Emit a stderr warning ("Both value= and formula= supplied — using
formula, value ignored.") from both the Add cell path and the Set
value/text path so the precedence is visible.
This commit is contained in:
zmworm 2026-04-19 07:32:02 +08:00
parent 21cf6573b8
commit 16ffbc8b56
2 changed files with 16 additions and 0 deletions

View file

@ -163,6 +163,14 @@ public partial class ExcelHandler
// R13-1: reject values longer than Excel's 32767-char limit
// before doing any conversion/serialization.
EnsureCellValueLength(value, cellRef);
// R13-3: if both value= and formula= are supplied, formula wins
// (established precedence — formula is written after value) but
// the discarded value is easy to miss. Warn on stderr.
if (properties.ContainsKey("formula"))
{
Console.Error.WriteLine(
"Warning: Both value= and formula= supplied — using formula, value ignored.");
}
// Auto-detect formula: value starting with '=' is treated as formula
if (value.StartsWith('=') && value.Length > 1)
{

View file

@ -1385,6 +1385,14 @@ public partial class ExcelHandler
case "value" or "text":
// R13-1: enforce Excel's 32767-char per-cell limit.
EnsureCellValueLength(value, cell.CellReference?.Value);
// R13-3: warn if both value= and formula= supplied — formula
// takes precedence below (explicit-formula case runs last and
// clears CellValue), so the literal value is silently discarded.
if (properties.Any(p => p.Key.Equals("formula", StringComparison.OrdinalIgnoreCase)))
{
Console.Error.WriteLine(
"Warning: Both value= and formula= supplied — using formula, value ignored.");
}
// Auto-detect formula: value starting with '=' is treated as formula
if (value.StartsWith('=') && value.Length > 1)
goto case "formula";