Skip to main content

DataValidationRule

Struct DataValidationRule 

Source
pub struct DataValidationRule {
    pub range: String,
    pub kind: DataValidationKind,
    pub allow_blank: bool,
    pub input_message: Option<Message>,
    pub error_message: Option<Message>,
}
Expand description

A data validation rule (<dataValidation>, CT_DataValidation) applied over a cell range (sqref).

Only three of ST_DataValidationType’s eight values are modeled — see DataValidationKind; date/time/textLength/custom are not. showDropDown/imeMode are not modeled either (the former is a rarely-touched, confusingly-inverted flag most libraries leave alone; the latter is IME-input-method behavior, irrelevant outside East Asian input).

Fields§

§range: String

The cell range this rule applies to (sqref).

§kind: DataValidationKind

The validation performed.

§allow_blank: bool

Whether a blank cell is considered valid (allowBlank).

§input_message: Option<Message>

An input message shown when the cell is selected (showInputMessage is written automatically when this is Some).

§error_message: Option<Message>

An error message shown when an invalid value is entered (showErrorMessage is written automatically when this is Some). Always written with errorStyle="stop" (blocking) — the warning/ information (non-blocking) styles are not modeled.

Implementations§

Source§

impl DataValidationRule

Source

pub fn list( range: impl Into<String>, source: impl Into<String>, ) -> DataValidationRule

Creates a list validation rule with no messages, blanks allowed.

Examples found in repository?
examples/xlsx_conditional_formatting_and_validation.rs (line 81)
17fn main() -> office_toolkit::Result<()> {
18    let path = output_path("xlsx_conditional_formatting_and_validation.xlsx");
19
20    let cell_is_sheet = Sheet::new("Cell-is rule")
21        .with_row(Row::new().with_number(45.0))
22        .with_row(Row::new().with_number(72.0))
23        .with_row(Row::new().with_number(91.0))
24        .with_conditional_formatting_rule(ConditionalFormattingRule::cell_is(
25            "A1:A3",
26            ComparisonOperator::GreaterThan,
27            "70",
28            CellFormat::new()
29                .with_fill_color("FFC6EFCE")
30                .with_font_color("FF006100"),
31        ));
32
33    let scales_and_bars_sheet = Sheet::new("Color scale and data bar")
34        .with_row(Row::new().with_number(10.0))
35        .with_row(Row::new().with_number(50.0))
36        .with_row(Row::new().with_number(90.0))
37        .with_conditional_formatting_rule(ConditionalFormattingRule::color_scale(
38            "A1:A3",
39            vec![
40                ColorScaleStop::new(CfvoPosition::Min, "FFF8696B"),
41                ColorScaleStop::new(CfvoPosition::Max, "FF63BE7B"),
42            ],
43        ))
44        .with_row(Row::new().with_number(10.0))
45        .with_row(Row::new().with_number(50.0))
46        .with_row(Row::new().with_number(90.0))
47        .with_conditional_formatting_rule(ConditionalFormattingRule::data_bar(
48            "B1:B3",
49            CfvoPosition::Min,
50            CfvoPosition::Max,
51            "FF638EC6",
52        ));
53
54    let icon_set_and_ranking_sheet = Sheet::new("Icon set, top-10, duplicates")
55        .with_row(Row::new().with_number(1.0))
56        .with_row(Row::new().with_number(2.0))
57        .with_row(Row::new().with_number(3.0))
58        .with_conditional_formatting_rule(ConditionalFormattingRule::icon_set(
59            "A1:A3",
60            IconSetType::ThreeTrafficLights,
61        ))
62        .with_row(Row::new().with_number(4.0))
63        .with_row(Row::new().with_number(5.0))
64        .with_row(Row::new().with_number(6.0))
65        .with_conditional_formatting_rule(ConditionalFormattingRule::top10(
66            "B1:B3",
67            1,
68            false,
69            CellFormat::new().with_bold(true),
70        ))
71        .with_row(Row::new().with_text("A"))
72        .with_row(Row::new().with_text("A"))
73        .with_row(Row::new().with_text("B"))
74        .with_conditional_formatting_rule(ConditionalFormattingRule::duplicate_values(
75            "C1:C3",
76            CellFormat::new().with_fill_color("FFFFC7CE"),
77        ));
78
79    let data_validation_sheet = Sheet::new("Data validation")
80        .with_row(Row::new().with_text("Pick one"))
81        .with_data_validation_rule(DataValidationRule::list("A1:A1", "\"Small,Medium,Large\""))
82        .with_row(Row::new().with_number(0.0))
83        .with_data_validation_rule(
84            DataValidationRule::whole_number("B1:B1", ComparisonOperator::LessThanOrEqual, "100")
85                .with_input_message(
86                    Message::new("Enter a whole number from 0 to 100.").with_title("Valid range"),
87                )
88                .with_error_message(
89                    Message::new("That value is out of range.").with_title("Invalid entry"),
90                ),
91        );
92
93    let workbook = Workbook::new()
94        .with_sheet(cell_is_sheet)
95        .with_sheet(scales_and_bars_sheet)
96        .with_sheet(icon_set_and_ranking_sheet)
97        .with_sheet(data_validation_sheet);
98
99    workbook.save_to_file(&path)?;
100    println!("Wrote {}", path.display());
101    Ok(())
102}
Source

pub fn whole_number( range: impl Into<String>, operator: ComparisonOperator, formula1: impl Into<String>, ) -> DataValidationRule

Creates a whole validation rule with no messages, blanks allowed.

Examples found in repository?
examples/xlsx_conditional_formatting_and_validation.rs (line 84)
17fn main() -> office_toolkit::Result<()> {
18    let path = output_path("xlsx_conditional_formatting_and_validation.xlsx");
19
20    let cell_is_sheet = Sheet::new("Cell-is rule")
21        .with_row(Row::new().with_number(45.0))
22        .with_row(Row::new().with_number(72.0))
23        .with_row(Row::new().with_number(91.0))
24        .with_conditional_formatting_rule(ConditionalFormattingRule::cell_is(
25            "A1:A3",
26            ComparisonOperator::GreaterThan,
27            "70",
28            CellFormat::new()
29                .with_fill_color("FFC6EFCE")
30                .with_font_color("FF006100"),
31        ));
32
33    let scales_and_bars_sheet = Sheet::new("Color scale and data bar")
34        .with_row(Row::new().with_number(10.0))
35        .with_row(Row::new().with_number(50.0))
36        .with_row(Row::new().with_number(90.0))
37        .with_conditional_formatting_rule(ConditionalFormattingRule::color_scale(
38            "A1:A3",
39            vec![
40                ColorScaleStop::new(CfvoPosition::Min, "FFF8696B"),
41                ColorScaleStop::new(CfvoPosition::Max, "FF63BE7B"),
42            ],
43        ))
44        .with_row(Row::new().with_number(10.0))
45        .with_row(Row::new().with_number(50.0))
46        .with_row(Row::new().with_number(90.0))
47        .with_conditional_formatting_rule(ConditionalFormattingRule::data_bar(
48            "B1:B3",
49            CfvoPosition::Min,
50            CfvoPosition::Max,
51            "FF638EC6",
52        ));
53
54    let icon_set_and_ranking_sheet = Sheet::new("Icon set, top-10, duplicates")
55        .with_row(Row::new().with_number(1.0))
56        .with_row(Row::new().with_number(2.0))
57        .with_row(Row::new().with_number(3.0))
58        .with_conditional_formatting_rule(ConditionalFormattingRule::icon_set(
59            "A1:A3",
60            IconSetType::ThreeTrafficLights,
61        ))
62        .with_row(Row::new().with_number(4.0))
63        .with_row(Row::new().with_number(5.0))
64        .with_row(Row::new().with_number(6.0))
65        .with_conditional_formatting_rule(ConditionalFormattingRule::top10(
66            "B1:B3",
67            1,
68            false,
69            CellFormat::new().with_bold(true),
70        ))
71        .with_row(Row::new().with_text("A"))
72        .with_row(Row::new().with_text("A"))
73        .with_row(Row::new().with_text("B"))
74        .with_conditional_formatting_rule(ConditionalFormattingRule::duplicate_values(
75            "C1:C3",
76            CellFormat::new().with_fill_color("FFFFC7CE"),
77        ));
78
79    let data_validation_sheet = Sheet::new("Data validation")
80        .with_row(Row::new().with_text("Pick one"))
81        .with_data_validation_rule(DataValidationRule::list("A1:A1", "\"Small,Medium,Large\""))
82        .with_row(Row::new().with_number(0.0))
83        .with_data_validation_rule(
84            DataValidationRule::whole_number("B1:B1", ComparisonOperator::LessThanOrEqual, "100")
85                .with_input_message(
86                    Message::new("Enter a whole number from 0 to 100.").with_title("Valid range"),
87                )
88                .with_error_message(
89                    Message::new("That value is out of range.").with_title("Invalid entry"),
90                ),
91        );
92
93    let workbook = Workbook::new()
94        .with_sheet(cell_is_sheet)
95        .with_sheet(scales_and_bars_sheet)
96        .with_sheet(icon_set_and_ranking_sheet)
97        .with_sheet(data_validation_sheet);
98
99    workbook.save_to_file(&path)?;
100    println!("Wrote {}", path.display());
101    Ok(())
102}
Source

pub fn decimal( range: impl Into<String>, operator: ComparisonOperator, formula1: impl Into<String>, ) -> DataValidationRule

Creates a decimal validation rule with no messages, blanks allowed.

Source

pub fn date( range: impl Into<String>, operator: ComparisonOperator, formula1: impl Into<String>, ) -> DataValidationRule

Creates a date validation rule with no messages, blanks allowed.

Source

pub fn time( range: impl Into<String>, operator: ComparisonOperator, formula1: impl Into<String>, ) -> DataValidationRule

Creates a time validation rule with no messages, blanks allowed.

Source

pub fn text_length( range: impl Into<String>, operator: ComparisonOperator, formula1: impl Into<String>, ) -> DataValidationRule

Creates a textLength validation rule with no messages, blanks allowed.

Source

pub fn custom( range: impl Into<String>, formula: impl Into<String>, ) -> DataValidationRule

Creates a custom validation rule with no messages, blanks allowed.

Source

pub fn with_second_formula( self, formula2: impl Into<String>, ) -> DataValidationRule

Sets the second comparison formula (for Between/NotBetween) on a WholeNumber/Decimal/Date/Time/TextLength rule and returns it for chaining. A no-op for a List/Custom rule.

Source

pub fn with_allow_blank(self, allow_blank: bool) -> DataValidationRule

Sets whether a blank cell is considered valid and returns the rule for chaining.

Source

pub fn with_input_message(self, message: Message) -> DataValidationRule

Sets the input message and returns the rule for chaining.

Examples found in repository?
examples/xlsx_conditional_formatting_and_validation.rs (lines 85-87)
17fn main() -> office_toolkit::Result<()> {
18    let path = output_path("xlsx_conditional_formatting_and_validation.xlsx");
19
20    let cell_is_sheet = Sheet::new("Cell-is rule")
21        .with_row(Row::new().with_number(45.0))
22        .with_row(Row::new().with_number(72.0))
23        .with_row(Row::new().with_number(91.0))
24        .with_conditional_formatting_rule(ConditionalFormattingRule::cell_is(
25            "A1:A3",
26            ComparisonOperator::GreaterThan,
27            "70",
28            CellFormat::new()
29                .with_fill_color("FFC6EFCE")
30                .with_font_color("FF006100"),
31        ));
32
33    let scales_and_bars_sheet = Sheet::new("Color scale and data bar")
34        .with_row(Row::new().with_number(10.0))
35        .with_row(Row::new().with_number(50.0))
36        .with_row(Row::new().with_number(90.0))
37        .with_conditional_formatting_rule(ConditionalFormattingRule::color_scale(
38            "A1:A3",
39            vec![
40                ColorScaleStop::new(CfvoPosition::Min, "FFF8696B"),
41                ColorScaleStop::new(CfvoPosition::Max, "FF63BE7B"),
42            ],
43        ))
44        .with_row(Row::new().with_number(10.0))
45        .with_row(Row::new().with_number(50.0))
46        .with_row(Row::new().with_number(90.0))
47        .with_conditional_formatting_rule(ConditionalFormattingRule::data_bar(
48            "B1:B3",
49            CfvoPosition::Min,
50            CfvoPosition::Max,
51            "FF638EC6",
52        ));
53
54    let icon_set_and_ranking_sheet = Sheet::new("Icon set, top-10, duplicates")
55        .with_row(Row::new().with_number(1.0))
56        .with_row(Row::new().with_number(2.0))
57        .with_row(Row::new().with_number(3.0))
58        .with_conditional_formatting_rule(ConditionalFormattingRule::icon_set(
59            "A1:A3",
60            IconSetType::ThreeTrafficLights,
61        ))
62        .with_row(Row::new().with_number(4.0))
63        .with_row(Row::new().with_number(5.0))
64        .with_row(Row::new().with_number(6.0))
65        .with_conditional_formatting_rule(ConditionalFormattingRule::top10(
66            "B1:B3",
67            1,
68            false,
69            CellFormat::new().with_bold(true),
70        ))
71        .with_row(Row::new().with_text("A"))
72        .with_row(Row::new().with_text("A"))
73        .with_row(Row::new().with_text("B"))
74        .with_conditional_formatting_rule(ConditionalFormattingRule::duplicate_values(
75            "C1:C3",
76            CellFormat::new().with_fill_color("FFFFC7CE"),
77        ));
78
79    let data_validation_sheet = Sheet::new("Data validation")
80        .with_row(Row::new().with_text("Pick one"))
81        .with_data_validation_rule(DataValidationRule::list("A1:A1", "\"Small,Medium,Large\""))
82        .with_row(Row::new().with_number(0.0))
83        .with_data_validation_rule(
84            DataValidationRule::whole_number("B1:B1", ComparisonOperator::LessThanOrEqual, "100")
85                .with_input_message(
86                    Message::new("Enter a whole number from 0 to 100.").with_title("Valid range"),
87                )
88                .with_error_message(
89                    Message::new("That value is out of range.").with_title("Invalid entry"),
90                ),
91        );
92
93    let workbook = Workbook::new()
94        .with_sheet(cell_is_sheet)
95        .with_sheet(scales_and_bars_sheet)
96        .with_sheet(icon_set_and_ranking_sheet)
97        .with_sheet(data_validation_sheet);
98
99    workbook.save_to_file(&path)?;
100    println!("Wrote {}", path.display());
101    Ok(())
102}
Source

pub fn with_error_message(self, message: Message) -> DataValidationRule

Sets the error message and returns the rule for chaining.

Examples found in repository?
examples/xlsx_conditional_formatting_and_validation.rs (lines 88-90)
17fn main() -> office_toolkit::Result<()> {
18    let path = output_path("xlsx_conditional_formatting_and_validation.xlsx");
19
20    let cell_is_sheet = Sheet::new("Cell-is rule")
21        .with_row(Row::new().with_number(45.0))
22        .with_row(Row::new().with_number(72.0))
23        .with_row(Row::new().with_number(91.0))
24        .with_conditional_formatting_rule(ConditionalFormattingRule::cell_is(
25            "A1:A3",
26            ComparisonOperator::GreaterThan,
27            "70",
28            CellFormat::new()
29                .with_fill_color("FFC6EFCE")
30                .with_font_color("FF006100"),
31        ));
32
33    let scales_and_bars_sheet = Sheet::new("Color scale and data bar")
34        .with_row(Row::new().with_number(10.0))
35        .with_row(Row::new().with_number(50.0))
36        .with_row(Row::new().with_number(90.0))
37        .with_conditional_formatting_rule(ConditionalFormattingRule::color_scale(
38            "A1:A3",
39            vec![
40                ColorScaleStop::new(CfvoPosition::Min, "FFF8696B"),
41                ColorScaleStop::new(CfvoPosition::Max, "FF63BE7B"),
42            ],
43        ))
44        .with_row(Row::new().with_number(10.0))
45        .with_row(Row::new().with_number(50.0))
46        .with_row(Row::new().with_number(90.0))
47        .with_conditional_formatting_rule(ConditionalFormattingRule::data_bar(
48            "B1:B3",
49            CfvoPosition::Min,
50            CfvoPosition::Max,
51            "FF638EC6",
52        ));
53
54    let icon_set_and_ranking_sheet = Sheet::new("Icon set, top-10, duplicates")
55        .with_row(Row::new().with_number(1.0))
56        .with_row(Row::new().with_number(2.0))
57        .with_row(Row::new().with_number(3.0))
58        .with_conditional_formatting_rule(ConditionalFormattingRule::icon_set(
59            "A1:A3",
60            IconSetType::ThreeTrafficLights,
61        ))
62        .with_row(Row::new().with_number(4.0))
63        .with_row(Row::new().with_number(5.0))
64        .with_row(Row::new().with_number(6.0))
65        .with_conditional_formatting_rule(ConditionalFormattingRule::top10(
66            "B1:B3",
67            1,
68            false,
69            CellFormat::new().with_bold(true),
70        ))
71        .with_row(Row::new().with_text("A"))
72        .with_row(Row::new().with_text("A"))
73        .with_row(Row::new().with_text("B"))
74        .with_conditional_formatting_rule(ConditionalFormattingRule::duplicate_values(
75            "C1:C3",
76            CellFormat::new().with_fill_color("FFFFC7CE"),
77        ));
78
79    let data_validation_sheet = Sheet::new("Data validation")
80        .with_row(Row::new().with_text("Pick one"))
81        .with_data_validation_rule(DataValidationRule::list("A1:A1", "\"Small,Medium,Large\""))
82        .with_row(Row::new().with_number(0.0))
83        .with_data_validation_rule(
84            DataValidationRule::whole_number("B1:B1", ComparisonOperator::LessThanOrEqual, "100")
85                .with_input_message(
86                    Message::new("Enter a whole number from 0 to 100.").with_title("Valid range"),
87                )
88                .with_error_message(
89                    Message::new("That value is out of range.").with_title("Invalid entry"),
90                ),
91        );
92
93    let workbook = Workbook::new()
94        .with_sheet(cell_is_sheet)
95        .with_sheet(scales_and_bars_sheet)
96        .with_sheet(icon_set_and_ranking_sheet)
97        .with_sheet(data_validation_sheet);
98
99    workbook.save_to_file(&path)?;
100    println!("Wrote {}", path.display());
101    Ok(())
102}

Trait Implementations§

Source§

impl Clone for DataValidationRule

Source§

fn clone(&self) -> DataValidationRule

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DataValidationRule

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for DataValidationRule

Source§

fn eq(&self, other: &DataValidationRule) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for DataValidationRule

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.