#1448: a legacy "Count Numbers of" caption follows a new function

Addresses CodeRabbit review on #3219, "Recognize the legacy Count
Numbers caption when the function changes": workbooks saved before
Count Numbers was captioned "Count of" carry "Count Numbers of Val"
(optionally with a number suffix). pivot.valueSettings now treats that
as an automatic caption of a Count Numbers field, so changing the
function renames it; a custom name stays.

Test: legacy_count_numbers_caption_follows_the_function.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Hector VillarrealandClaude Opus 5.5 committed 2026-11-03 00:27:25 -06:00
1 parent 7bea3d1d6a
commit 044599c6ef
2 files changed
+24 -3

No files matched your search

+7 -3
View File
@@ -507,9 +507,13 @@ pub(super) fn value_settings(s: &mut Session, p: &Json) -> Result<Json> {
let mut v = cur.clone();
if let Some(f) = func {
v.func = f;
// An automatic caption follows the function.
let auto = auto_name(cur.func, &cur.source_col);
if cur.name.eq_ignore_ascii_case(&auto) || cur.name.strip_prefix(auto.as_str()).is_some_and(|r| r.chars().all(|c| c.is_ascii_digit())) {
// An automatic caption follows the function. Count Numbers was captioned "Count
// Numbers of …" before it took Excel's "Count of …", and saved workbooks still carry it.
let is_auto = |base: &str| {
cur.name.eq_ignore_ascii_case(base) || cur.name.strip_prefix(base).is_some_and(|r| r.chars().all(|c| c.is_ascii_digit()))
};
let legacy = cur.func == PivotFunc::CountNumbers && is_auto(&format!("Count Numbers of {}", cur.source_col));
if is_auto(&auto_name(cur.func, &cur.source_col)) || legacy {
v.name = unique_value_name(pt, &auto_name(f, &cur.source_col), Some(i));
}
}
+17
View File
@@ -1029,3 +1029,20 @@ fn rank_ties_are_dense_and_count_numbers_is_captioned_count_of() {
s.execute("pivot.valueSettings", json!({"index": 0, "showAs": "normal", "func": "countNumbers"})).unwrap();
assert_eq!(text(&s, "E3"), "Count of Val");
}
#[test]
fn legacy_count_numbers_caption_follows_the_function() {
// Workbooks saved before Count Numbers was captioned "Count of" carry "Count Numbers of Val";
// that is still an automatic caption, so it follows a new function. A custom name stays.
let mut s = Session::new();
s.new_workbook();
let rows = json!([["Key", "Val"], ["A", 5], ["B", 4]]);
s.execute("range.setValues", json!({"range": "A1", "values": rows})).unwrap();
s.execute("insert.pivotTable", json!({"source": "Sheet1!$A$1:$B$3", "destination": "Sheet1!D3", "rows": ["Key"], "values": ["Val"]})).unwrap();
for (legacy, after) in [("Count Numbers of Val", "Sum of Val"), ("Count Numbers of Val2", "Sum of Val"), ("Numbers counted", "Numbers counted")] {
s.execute("pivot.valueSettings", json!({"index": 0, "func": "countNumbers", "name": legacy})).unwrap();
assert_eq!(text(&s, "E3"), legacy);
s.execute("pivot.valueSettings", json!({"index": 0, "func": "sum"})).unwrap();
assert_eq!(text(&s, "E3"), after, "{legacy}");
}
}