pub struct SortState {
pub range: String,
pub conditions: Vec<SortCondition>,
}Expand description
A remembered sort order (<sortState>, CT_SortState). Written in two different places
depending on context: as a child of a worksheet-level <autoFilter> (Sheet::sort_state) or
as a sibling of a table’s own <autoFilter> (ExcelTable::sort_state) — see each field’s own
doc comment for the exact schema positioning, confirmed via sml.xsd’s CT_AutoFilter/
CT_Table sequences. Only the simplest, most common shape is modeled: a plain value-based
ascending/descending sort per range (ST_SortBy’s "value", the schema default) —
sortBy="cellColor"/ "fontColor"/"icon", customList, and dxfId/iconSet/iconId (color/
icon-based custom sort criteria) are not modeled, same “the common case, not every historical
Excel option” posture as SheetProtection/AutoFilterColumn. Storage only — this crate does
not itself reorder any rows, see ExcelTable.sort_state’s doc comment.
Fields§
§range: StringThe overall range this sort applies to (ref, required) — e.g. the table’s own data range,
or the autofiltered range.
conditions: Vec<SortCondition>Up to 64 sort keys, in priority order (<sortCondition>, CT_SortState’s own
maxOccurs="64" limit — not enforced here, same “caller’s responsibility” posture as
elsewhere in this crate).
Implementations§
Source§impl SortState
impl SortState
Sourcepub fn new(range: impl Into<String>) -> SortState
pub fn new(range: impl Into<String>) -> SortState
Creates a sort state over range with no sort keys yet.
Examples found in repository?
16fn main() -> office_toolkit::Result<()> {
17 let path = output_path("xlsx_tables.xlsx");
18
19 let plain_sheet = Sheet::new("Plain table")
20 .with_row(Row::new().with_text("Name").with_text("Score"))
21 .with_row(Row::new().with_text("Alice").with_number(92.0))
22 .with_row(Row::new().with_text("Bob").with_number(87.0))
23 .with_table(ExcelTable::new(
24 "ScoresTable",
25 "A1:B3",
26 vec!["Name", "Score"],
27 ));
28
29 let calculated_sheet = Sheet::new("Calculated and totals")
30 .with_row(
31 Row::new()
32 .with_text("Item")
33 .with_text("Qty")
34 .with_text("Unit price")
35 .with_text("Line total"),
36 )
37 .with_row(
38 Row::new()
39 .with_text("Widget")
40 .with_number(3.0)
41 .with_number(9.99)
42 .with_cell(Cell::formula("B2*C2").with_cached_value(29.97)),
43 )
44 .with_row(
45 Row::new()
46 .with_text("Gadget")
47 .with_number(2.0)
48 .with_number(19.99)
49 .with_cell(Cell::formula("B3*C3").with_cached_value(39.98)),
50 )
51 .with_row(Row::new().with_cell(Cell::text("Total")))
52 .with_table(
53 ExcelTable::with_columns(
54 "OrdersTable",
55 "A1:D4",
56 vec![
57 TableColumn::new("Item"),
58 TableColumn::new("Qty").with_totals_row_function(TotalsRowFunction::Sum),
59 TableColumn::new("Unit price"),
60 TableColumn::new("Line total")
61 .with_calculated_formula(
62 "OrdersTable[[#This Row],[Qty]]*OrdersTable[[#This Row],[Unit price]]",
63 )
64 .with_totals_row_function(TotalsRowFunction::Sum),
65 ],
66 )
67 .with_totals_row(true),
68 );
69
70 let styled_sheet = Sheet::new("Custom style and sort")
71 .with_row(Row::new().with_text("City").with_text("Population"))
72 .with_row(Row::new().with_text("Springfield").with_number(120_000.0))
73 .with_row(Row::new().with_text("Shelbyville").with_number(95_000.0))
74 .with_table(
75 ExcelTable::new("CitiesTable", "A1:B3", vec!["City", "Population"])
76 .with_table_style_name("CustomTableStyle")
77 .with_sort_state(
78 SortState::new("A2:B3")
79 .with_condition(SortCondition::new("B2:B3").with_descending(true)),
80 ),
81 );
82
83 let workbook = Workbook::new()
84 .with_table_style(
85 TableStyle::new("CustomTableStyle")
86 .with_element(TableStyleElement::new(
87 TableStyleElementType::HeaderRow,
88 CellFormat::new()
89 .with_bold(true)
90 .with_fill_color("FF2E74B5")
91 .with_font_color("FFFFFFFF"),
92 ))
93 .with_element(TableStyleElement::new(
94 TableStyleElementType::FirstRowStripe,
95 CellFormat::new().with_fill_color("FFF2F2F2"),
96 )),
97 )
98 .with_sheet(plain_sheet)
99 .with_sheet(calculated_sheet)
100 .with_sheet(styled_sheet);
101
102 workbook.save_to_file(&path)?;
103 println!("Wrote {}", path.display());
104 Ok(())
105}Sourcepub fn with_condition(self, condition: SortCondition) -> SortState
pub fn with_condition(self, condition: SortCondition) -> SortState
Appends a sort key and returns the sort state for chaining.
Examples found in repository?
16fn main() -> office_toolkit::Result<()> {
17 let path = output_path("xlsx_tables.xlsx");
18
19 let plain_sheet = Sheet::new("Plain table")
20 .with_row(Row::new().with_text("Name").with_text("Score"))
21 .with_row(Row::new().with_text("Alice").with_number(92.0))
22 .with_row(Row::new().with_text("Bob").with_number(87.0))
23 .with_table(ExcelTable::new(
24 "ScoresTable",
25 "A1:B3",
26 vec!["Name", "Score"],
27 ));
28
29 let calculated_sheet = Sheet::new("Calculated and totals")
30 .with_row(
31 Row::new()
32 .with_text("Item")
33 .with_text("Qty")
34 .with_text("Unit price")
35 .with_text("Line total"),
36 )
37 .with_row(
38 Row::new()
39 .with_text("Widget")
40 .with_number(3.0)
41 .with_number(9.99)
42 .with_cell(Cell::formula("B2*C2").with_cached_value(29.97)),
43 )
44 .with_row(
45 Row::new()
46 .with_text("Gadget")
47 .with_number(2.0)
48 .with_number(19.99)
49 .with_cell(Cell::formula("B3*C3").with_cached_value(39.98)),
50 )
51 .with_row(Row::new().with_cell(Cell::text("Total")))
52 .with_table(
53 ExcelTable::with_columns(
54 "OrdersTable",
55 "A1:D4",
56 vec![
57 TableColumn::new("Item"),
58 TableColumn::new("Qty").with_totals_row_function(TotalsRowFunction::Sum),
59 TableColumn::new("Unit price"),
60 TableColumn::new("Line total")
61 .with_calculated_formula(
62 "OrdersTable[[#This Row],[Qty]]*OrdersTable[[#This Row],[Unit price]]",
63 )
64 .with_totals_row_function(TotalsRowFunction::Sum),
65 ],
66 )
67 .with_totals_row(true),
68 );
69
70 let styled_sheet = Sheet::new("Custom style and sort")
71 .with_row(Row::new().with_text("City").with_text("Population"))
72 .with_row(Row::new().with_text("Springfield").with_number(120_000.0))
73 .with_row(Row::new().with_text("Shelbyville").with_number(95_000.0))
74 .with_table(
75 ExcelTable::new("CitiesTable", "A1:B3", vec!["City", "Population"])
76 .with_table_style_name("CustomTableStyle")
77 .with_sort_state(
78 SortState::new("A2:B3")
79 .with_condition(SortCondition::new("B2:B3").with_descending(true)),
80 ),
81 );
82
83 let workbook = Workbook::new()
84 .with_table_style(
85 TableStyle::new("CustomTableStyle")
86 .with_element(TableStyleElement::new(
87 TableStyleElementType::HeaderRow,
88 CellFormat::new()
89 .with_bold(true)
90 .with_fill_color("FF2E74B5")
91 .with_font_color("FFFFFFFF"),
92 ))
93 .with_element(TableStyleElement::new(
94 TableStyleElementType::FirstRowStripe,
95 CellFormat::new().with_fill_color("FFF2F2F2"),
96 )),
97 )
98 .with_sheet(plain_sheet)
99 .with_sheet(calculated_sheet)
100 .with_sheet(styled_sheet);
101
102 workbook.save_to_file(&path)?;
103 println!("Wrote {}", path.display());
104 Ok(())
105}