spreadsheet-kit 0.11.1

Core spreadsheet automation primitives — shared types, edit normalization, and session traits for agent-facing surfaces
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
use anyhow::Result;
use spreadsheet_kit as spreadsheet_mcp;
use spreadsheet_mcp::tools::{
    ListWorkbooksParams, WorkbookStyleSummaryParams, list_workbooks, workbook_style_summary,
};
use umya_spreadsheet::structs::drawing::Color2Type;
use umya_spreadsheet::{
    ConditionalFormatValues, ConditionalFormatting, ConditionalFormattingRule, Formula,
};

mod support;

#[tokio::test(flavor = "current_thread")]
async fn workbook_style_summary_reports_theme_and_infers_default_style() -> Result<()> {
    let workspace = support::TestWorkspace::new();
    workspace.create_workbook("wb_styles.xlsx", |book| {
        let sheet1 = book.get_sheet_by_name_mut("Sheet1").unwrap();
        for col in ['A', 'B', 'C', 'D', 'E'] {
            let addr = format!("{col}1");
            sheet1.get_cell_mut(addr.as_str()).set_value("x");
        }

        sheet1.get_cell_mut("F1").set_value("Header");
        sheet1.get_style_mut("F1").get_font_mut().set_bold(true);

        book.new_sheet("Sheet2").unwrap();
        let sheet2 = book.get_sheet_by_name_mut("Sheet2").unwrap();
        sheet2.get_cell_mut("A1").set_value_number(1);

        let mut cf = ConditionalFormatting::default();
        cf.get_sequence_of_references_mut().set_sqref("A1:A3");

        let mut rule = ConditionalFormattingRule::default();
        rule.set_type(ConditionalFormatValues::Expression);
        rule.set_priority(1);
        let mut formula = Formula::default();
        formula.set_string_value("A1>0");
        rule.set_formula(formula);
        cf.add_conditional_collection(rule);
        sheet2.add_conditional_formatting_collection(cf);
    });

    let state = workspace.app_state();
    let list = list_workbooks(
        state.clone(),
        ListWorkbooksParams {
            slug_prefix: None,
            folder: None,
            path_glob: None,
            limit: None,
            offset: None,
            include_paths: None,
        },
    )
    .await?;
    let workbook_id = list.workbooks[0].workbook_id.clone();

    let summary = workbook_style_summary(
        state,
        WorkbookStyleSummaryParams {
            workbook_or_fork_id: workbook_id,
            max_styles: None,
            max_conditional_formats: None,
            max_cells_scan: None,
            summary_only: Some(false),
            include_descriptor: None,
            include_example_cells: None,
            include_theme: None,
            include_conditional_formats: None,
        },
    )
    .await?;

    let theme = summary.theme.as_ref().expect("theme present");
    assert!(!theme.colors.is_empty());

    assert!(!summary.styles.is_empty());
    let default_style_id = summary
        .inferred_default_style_id
        .clone()
        .expect("default style id");
    let default_style = summary
        .styles
        .iter()
        .find(|s| s.style_id == default_style_id)
        .expect("default style usage");
    let inferred_font = summary
        .inferred_default_font
        .as_ref()
        .expect("default font");
    let default_font_name = default_style
        .descriptor
        .as_ref()
        .and_then(|d| d.font.as_ref())
        .and_then(|f| f.name.clone());
    if default_font_name.is_some() {
        assert_eq!(default_font_name, inferred_font.name);
    } else {
        assert!(inferred_font.name.is_some());
    }

    assert_eq!(summary.conditional_formats.len(), 1);
    let cf_summary = &summary.conditional_formats[0];
    assert_eq!(cf_summary.sheet_name, "Sheet2");
    assert_eq!(cf_summary.range, "A1:A3");
    assert!(cf_summary.rule_types.iter().any(|t| t == "expression"));

    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn workbook_style_summary_truncates_large_style_counts() -> Result<()> {
    let workspace = support::TestWorkspace::new();
    workspace.create_workbook("many_styles.xlsx", |book| {
        let sheet = book.get_sheet_by_name_mut("Sheet1").unwrap();
        for i in 0..205u32 {
            let row = i + 1;
            let addr = format!("A{row}");
            sheet.get_cell_mut(addr.as_str()).set_value_number(i as i32);
            let color = format!("FF{:02X}0000", (i % 256) as u8);
            sheet
                .get_style_mut(addr.as_str())
                .get_font_mut()
                .get_color_mut()
                .set_argb(color);
        }
    });

    let state = workspace.app_state();
    let list = list_workbooks(
        state.clone(),
        ListWorkbooksParams {
            slug_prefix: None,
            folder: None,
            path_glob: None,
            limit: None,
            offset: None,
            include_paths: None,
        },
    )
    .await?;
    let workbook_id = list.workbooks[0].workbook_id.clone();

    let summary = workbook_style_summary(
        state,
        WorkbookStyleSummaryParams {
            workbook_or_fork_id: workbook_id,
            max_styles: None,
            max_conditional_formats: None,
            max_cells_scan: None,
            summary_only: Some(false),
            include_descriptor: None,
            include_example_cells: None,
            include_theme: None,
            include_conditional_formats: None,
        },
    )
    .await?;

    assert!(summary.total_styles >= 205);
    assert!(summary.styles_truncated);
    assert_eq!(summary.styles.len(), 200);
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn workbook_style_summary_handles_empty_workbook() -> Result<()> {
    let workspace = support::TestWorkspace::new();
    workspace.create_workbook("empty.xlsx", |_book| {});

    let state = workspace.app_state();
    let list = list_workbooks(
        state.clone(),
        ListWorkbooksParams {
            slug_prefix: None,
            folder: None,
            path_glob: None,
            limit: None,
            offset: None,
            include_paths: None,
        },
    )
    .await?;
    let workbook_id = list.workbooks[0].workbook_id.clone();

    let summary = workbook_style_summary(
        state,
        WorkbookStyleSummaryParams {
            workbook_or_fork_id: workbook_id,
            max_styles: None,
            max_conditional_formats: None,
            max_cells_scan: None,
            summary_only: Some(false),
            include_descriptor: None,
            include_example_cells: None,
            include_theme: None,
            include_conditional_formats: None,
        },
    )
    .await?;

    assert_eq!(summary.total_styles, 0);
    assert!(summary.styles.is_empty());
    assert!(summary.inferred_default_style_id.is_none());
    assert!(summary.theme.is_some());
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn workbook_style_summary_omits_empty_theme_colors() -> Result<()> {
    let workspace = support::TestWorkspace::new();
    workspace.create_workbook("theme.xlsx", |book| {
        let scheme = book
            .get_theme_mut()
            .get_theme_elements_mut()
            .get_color_scheme_mut();
        scheme.set_accent1(Color2Type::default());
        scheme.set_accent2(Color2Type::default());
    });

    let state = workspace.app_state();
    let list = list_workbooks(
        state.clone(),
        ListWorkbooksParams {
            slug_prefix: None,
            folder: None,
            path_glob: None,
            limit: None,
            offset: None,
            include_paths: None,
        },
    )
    .await?;
    let workbook_id = list.workbooks[0].workbook_id.clone();

    let summary = workbook_style_summary(
        state,
        WorkbookStyleSummaryParams {
            workbook_or_fork_id: workbook_id,
            max_styles: None,
            max_conditional_formats: None,
            max_cells_scan: None,
            summary_only: Some(false),
            include_descriptor: None,
            include_example_cells: None,
            include_theme: None,
            include_conditional_formats: None,
        },
    )
    .await?;

    let theme = summary.theme.expect("theme present");
    assert!(!theme.colors.contains_key("accent1"));
    assert!(!theme.colors.contains_key("accent2"));
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn workbook_style_summary_sets_scan_truncated_when_limit_exceeded() -> Result<()> {
    let workspace = support::TestWorkspace::new();
    workspace.create_workbook("scan.xlsx", |book| {
        let sheet = book.get_sheet_by_name_mut("Sheet1").unwrap();
        for i in 0..1100u32 {
            let row = i + 1;
            let addr = format!("A{row}");
            sheet.get_cell_mut(addr.as_str()).set_value_number(i as i32);
        }
    });

    let state = workspace.app_state();
    let list = list_workbooks(
        state.clone(),
        ListWorkbooksParams {
            slug_prefix: None,
            folder: None,
            path_glob: None,
            limit: None,
            offset: None,
            include_paths: None,
        },
    )
    .await?;
    let workbook_id = list.workbooks[0].workbook_id.clone();

    let summary = workbook_style_summary(
        state,
        WorkbookStyleSummaryParams {
            workbook_or_fork_id: workbook_id,
            max_styles: None,
            max_conditional_formats: None,
            max_cells_scan: Some(1000),
            summary_only: Some(false),
            include_descriptor: None,
            include_example_cells: None,
            include_theme: None,
            include_conditional_formats: None,
        },
    )
    .await?;

    assert!(summary.scan_truncated);
    assert!(summary.notes.iter().any(|n| n.contains("Stopped scanning")));
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn workbook_style_summary_aggregates_multiple_cf_rules_and_sheets() -> Result<()> {
    let workspace = support::TestWorkspace::new();
    workspace.create_workbook("cf_multi.xlsx", |book| {
        let sheet1 = book.get_sheet_by_name_mut("Sheet1").unwrap();
        let mut cf1 = ConditionalFormatting::default();
        cf1.get_sequence_of_references_mut().set_sqref("A1:A3");
        let mut rule1 = ConditionalFormattingRule::default();
        rule1.set_type(ConditionalFormatValues::Expression);
        rule1.set_priority(1);
        let mut formula = Formula::default();
        formula.set_string_value("A1>0");
        rule1.set_formula(formula);
        cf1.add_conditional_collection(rule1);
        let mut rule2 = ConditionalFormattingRule::default();
        rule2.set_type(ConditionalFormatValues::DuplicateValues);
        rule2.set_priority(2);
        cf1.add_conditional_collection(rule2);
        sheet1.add_conditional_formatting_collection(cf1);

        book.new_sheet("Sheet2").unwrap();
        let sheet2 = book.get_sheet_by_name_mut("Sheet2").unwrap();
        let mut cf2 = ConditionalFormatting::default();
        cf2.get_sequence_of_references_mut().set_sqref("B1:B2");
        let mut rule3 = ConditionalFormattingRule::default();
        rule3.set_type(ConditionalFormatValues::Expression);
        rule3.set_priority(1);
        let mut formula = Formula::default();
        formula.set_string_value("B1>0");
        rule3.set_formula(formula);
        cf2.add_conditional_collection(rule3);
        sheet2.add_conditional_formatting_collection(cf2);
    });

    let state = workspace.app_state();
    let list = list_workbooks(
        state.clone(),
        ListWorkbooksParams {
            slug_prefix: None,
            folder: None,
            path_glob: None,
            limit: None,
            offset: None,
            include_paths: None,
        },
    )
    .await?;
    let workbook_id = list.workbooks[0].workbook_id.clone();

    let summary = workbook_style_summary(
        state,
        WorkbookStyleSummaryParams {
            workbook_or_fork_id: workbook_id,
            max_styles: None,
            max_conditional_formats: None,
            max_cells_scan: None,
            summary_only: Some(false),
            include_descriptor: None,
            include_example_cells: None,
            include_theme: None,
            include_conditional_formats: None,
        },
    )
    .await?;

    assert_eq!(summary.conditional_formats.len(), 2);
    let sheet1_cf = summary
        .conditional_formats
        .iter()
        .find(|c| c.sheet_name == "Sheet1")
        .expect("Sheet1 CF");
    assert_eq!(sheet1_cf.range, "A1:A3");
    assert_eq!(sheet1_cf.rule_count, 2);
    assert!(sheet1_cf.rule_types.iter().any(|t| t == "expression"));
    assert!(sheet1_cf.rule_types.iter().any(|t| t == "duplicateValues"));

    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn workbook_style_summary_truncates_conditional_formats() -> Result<()> {
    let workspace = support::TestWorkspace::new();
    workspace.create_workbook("cf_trunc.xlsx", |book| {
        let sheet1 = book.get_sheet_by_name_mut("Sheet1").unwrap();
        let mut cf1 = ConditionalFormatting::default();
        cf1.get_sequence_of_references_mut().set_sqref("A1:A2");
        let mut rule1 = ConditionalFormattingRule::default();
        rule1.set_type(ConditionalFormatValues::Expression);
        rule1.set_priority(1);
        let mut formula = Formula::default();
        formula.set_string_value("A1>0");
        rule1.set_formula(formula);
        cf1.add_conditional_collection(rule1);
        sheet1.add_conditional_formatting_collection(cf1);

        let mut cf2 = ConditionalFormatting::default();
        cf2.get_sequence_of_references_mut().set_sqref("B1:B2");
        let mut rule2 = ConditionalFormattingRule::default();
        rule2.set_type(ConditionalFormatValues::Expression);
        rule2.set_priority(1);
        let mut formula = Formula::default();
        formula.set_string_value("B1>0");
        rule2.set_formula(formula);
        cf2.add_conditional_collection(rule2);
        sheet1.add_conditional_formatting_collection(cf2);
    });

    let state = workspace.app_state();
    let list = list_workbooks(
        state.clone(),
        ListWorkbooksParams {
            slug_prefix: None,
            folder: None,
            path_glob: None,
            limit: None,
            offset: None,
            include_paths: None,
        },
    )
    .await?;
    let workbook_id = list.workbooks[0].workbook_id.clone();

    let summary = workbook_style_summary(
        state,
        WorkbookStyleSummaryParams {
            workbook_or_fork_id: workbook_id,
            max_styles: None,
            max_conditional_formats: Some(1),
            max_cells_scan: None,
            summary_only: Some(false),
            include_descriptor: None,
            include_example_cells: None,
            include_theme: None,
            include_conditional_formats: None,
        },
    )
    .await?;

    assert_eq!(summary.conditional_formats.len(), 1);
    assert!(summary.conditional_formats_truncated);
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn workbook_style_summary_aggregates_identical_styles_across_sheets() -> Result<()> {
    let workspace = support::TestWorkspace::new();
    workspace.create_workbook("style_invariant.xlsx", |book| {
        let sheet1 = book.get_sheet_by_name_mut("Sheet1").unwrap();
        sheet1.get_cell_mut("A1").set_value("x");
        sheet1.get_style_mut("A1").get_font_mut().set_bold(true);

        book.new_sheet("Sheet2").unwrap();
        let sheet2 = book.get_sheet_by_name_mut("Sheet2").unwrap();
        sheet2.get_cell_mut("A1").set_value("x");
        sheet2.get_style_mut("A1").get_font_mut().set_bold(true);
    });

    let state = workspace.app_state();
    let list = list_workbooks(
        state.clone(),
        ListWorkbooksParams {
            slug_prefix: None,
            folder: None,
            path_glob: None,
            limit: None,
            offset: None,
            include_paths: None,
        },
    )
    .await?;
    let workbook_id = list.workbooks[0].workbook_id.clone();

    let summary = workbook_style_summary(
        state,
        WorkbookStyleSummaryParams {
            workbook_or_fork_id: workbook_id,
            max_styles: None,
            max_conditional_formats: None,
            max_cells_scan: None,
            summary_only: Some(false),
            include_descriptor: None,
            include_example_cells: None,
            include_theme: None,
            include_conditional_formats: None,
        },
    )
    .await?;

    let invariant = summary
        .styles
        .iter()
        .find(|s| s.occurrences == 2)
        .expect("aggregated style");
    assert!(invariant.example_cells.iter().any(|c| c == "Sheet1!A1"));
    assert!(invariant.example_cells.iter().any(|c| c == "Sheet2!A1"));
    Ok(())
}