pub struct TableColumn {
pub name: String,
pub calculated_formula: Option<String>,
pub totals_row_function: Option<TotalsRowFunction>,
pub totals_row_label: Option<String>,
pub totals_row_formula: Option<String>,
}Expand description
One column of an ExcelTable (<tableColumn>, CT_TableColumn) — (plain name only),
extended for calculated_formula/ totals_row_function/totals_row_label.
Fields§
§name: StringThe column’s name, matching the header row’s actual cell text (see ExcelTable::columns’s
doc comment).
calculated_formula: Option<String>This column’s calculated-column formula (<calculatedColumnFormula>), if any. Declaring a
column calculated in the table definition only; this crate does not automatically
propagate a <f> formula onto every data row’s cell in this column — the caller remains
responsible for each data row’s own cell actually carrying a matching CellValue::Formula
if the per-row formulas should appear too (storage only, same “caller keeps things in sync”
posture as ExcelTable::columns itself).
Structured references must use the fully-qualified/expanded form, TableName[[#This Row],[ColumnName]] — not the shorthand [@ColumnName]. Both are valid Excel syntax when
typed into the UI (Excel silently normalizes one to the other), but a real Excel
installation rejects and strips the whole <table> on open when the shorthand form is what
ends up stored verbatim in calculatedColumnFormula (and/or a data row’s own <f>) — a
genuine Excel-authored file stores exactly this expanded form (Table1[[#This Row],[ColumnName]]) rather than [@ColumnName]. This crate does not rewrite or validate
formula text — passing a shorthand structured reference here silently produces a file real
Excel repairs on open.
totals_row_function: Option<TotalsRowFunction>This column’s totals-row aggregate function (<tableColumn totalsRowFunction="..">), if
any. Meaningless unless ExcelTable::show_totals_row is also true. Writes only the
totalsRowFunction=".." attribute itself on <tableColumn> — unlike an earlier version of
this crate, it does not auto-generate a matching
<totalsRowFormula>SUBTOTAL(n,[ColumnName])</totalsRowFormula> table-metadata child
element for a built-in function anymore: real Excel never writes that particular child
element either (the function name alone determines what it computes internally).
This is a separate thing from the totals row’s own cell in sheetData, which the caller
remains responsible for (same “caller keeps things in sync” posture as
calculated_formula): real Excel’s own totals row is a
genuine formula cell, not a plain cached number — confirmed against a real Excel-authored
file, whose totals row cell for a sum column literally contains
<f>SUBTOTAL(109,TableName[ColumnName])</f> with a cached <v>, and for average,
<f>SUBTOTAL(101,TableName[ColumnName])</f>. The 10x-prefixed SUBTOTAL function codes
(ignoring manually-hidden rows) real Excel tables use are, in TotalsRowFunction order:
Average → 101, Count → 102, CountNums → 103, Max → 104, Min → 105, StdDev → 107,
Var → 110, Sum → 109 (Custom has no fixed code — the caller supplies
totals_row_formula directly instead). A totals row cell
left as a plain cached number instead of this formula is one of the concrete differences
from real Excel output that causes a real Excel installation to reject and repair the file.
Only TotalsRowFunction::Custom pairs with an explicit
totals_row_formula. Also see
totals_row_label’s doc comment: setting both on the same
column, this field is silently dropped at write time.
totals_row_label: Option<String>This column’s totals-row label (<tableColumn totalsRowLabel="..">), shown instead of a
computed value — typically only meaningful on the first column of a totals row (e.g.
"Total"). The same real Excel-authored reference file mentioned in totals_row_function’s
doc comment also shows that the totals row’s own cell for the label column carries the label
text itself as a plain string value too (redundant with this attribute, but present) — the
caller remains responsible for that cell’s actual value matching. Mutually exclusive with
totals_row_function on the same column and enforced by the writer (real Excel’s own UI
only ever offers one or the other per column): when both are set here, the label wins and
totals_row_function/totals_row_formula are simply not written for that column.
totals_row_formula: Option<String>This column’s literal totals-row formula text (<totalsRowFormula>), only written when
totals_row_function is TotalsRowFunction::Custom and totals_row_label is None —
every other function variant writes no <totalsRowFormula> at all (see
totals_row_function’s doc comment), and a label on the same column takes priority over
both (see totals_row_label’s doc comment). If this formula uses a structured reference,
see calculated_formula’s doc comment: use the expanded TableName[[#This Row],[ColumnName]] form, not [@ColumnName].
Implementations§
Source§impl TableColumn
impl TableColumn
Sourcepub fn new(name: impl Into<String>) -> TableColumn
pub fn new(name: impl Into<String>) -> TableColumn
Creates a plain column with no calculated formula or totals-row function 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_calculated_formula(self, formula: impl Into<String>) -> TableColumn
pub fn with_calculated_formula(self, formula: impl Into<String>) -> TableColumn
Sets this column’s calculated-column formula and returns it 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}Sourcepub fn with_totals_row_function(
self,
function: TotalsRowFunction,
) -> TableColumn
pub fn with_totals_row_function( self, function: TotalsRowFunction, ) -> TableColumn
Sets this column’s totals-row aggregate function and returns it 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}Sourcepub fn with_totals_row_label(self, label: impl Into<String>) -> TableColumn
pub fn with_totals_row_label(self, label: impl Into<String>) -> TableColumn
Sets this column’s totals-row label and returns it for chaining.
Sourcepub fn with_totals_row_formula(self, formula: impl Into<String>) -> TableColumn
pub fn with_totals_row_formula(self, formula: impl Into<String>) -> TableColumn
Sets this column’s literal totals-row formula text (only used when totals_row_function is
TotalsRowFunction::Custom) and returns it for chaining.
Trait Implementations§
Source§impl Clone for TableColumn
impl Clone for TableColumn
Source§fn clone(&self) -> TableColumn
fn clone(&self) -> TableColumn
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more