Skip to content

Commit

Permalink
data validation: add list dropdown toggle
Browse files Browse the repository at this point in the history
Feature #97
  • Loading branch information
jmcnamara committed Jul 13, 2024
1 parent 38f6dd9 commit ac6b754
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/data_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ pub struct DataValidation {
pub(crate) ignore_blank: bool,
pub(crate) show_input_message: bool,
pub(crate) show_error_message: bool,
pub(crate) show_dropdown: bool,
pub(crate) multi_range: String,
pub(crate) input_title: String,
pub(crate) error_title: String,
Expand All @@ -205,6 +206,7 @@ impl DataValidation {
ignore_blank: true,
show_input_message: true,
show_error_message: true,
show_dropdown: true,
multi_range: String::new(),
input_title: String::new(),
error_title: String::new(),
Expand Down Expand Up @@ -986,6 +988,28 @@ impl DataValidation {
self
}

/// Turn on/off the in-cell dropdown for list data validations.
///
/// By default the Excel list data validation has an "In-cell drop-down"
/// option turned on. This shows a dropdown arrow for list style data
/// validations and displays the list items.
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/data_validation_allow_list_strings_dialog.png">
///
/// If this option is turned off the data validation will restrict input to
/// the specified list values but it won't display a visual indicator of
/// what those values are.
///
/// # Parameters
///
/// * `enable` - Turn the property on/off. It is on by default.
///
pub fn show_dropdown(mut self, enable: bool) -> DataValidation {
self.show_dropdown = enable;
self
}

/// Toggle option to show an input message when the cell is entered.
///
/// This function is used to toggle the option that controls whether an
Expand Down
41 changes: 41 additions & 0 deletions src/data_validation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1630,4 +1630,45 @@ mod data_validation_tests {

Ok(())
}

#[test]
fn data_validation_27() -> Result<(), XlsxError> {
let mut worksheet = Worksheet::new();
worksheet.set_selected(true);

let data_validation = DataValidation::new()
.allow_list_strings(&["Foo", "Bar", "Baz"])?
.show_dropdown(false);

worksheet.add_data_validation(0, 0, 0, 0, &data_validation)?;

worksheet.assemble_xml_file();

let got = worksheet.writer.read_to_str();
let got = xml_to_vec(got);

let expected = xml_to_vec(
r#"
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<dimension ref="A1"/>
<sheetViews>
<sheetView tabSelected="1" workbookViewId="0"/>
</sheetViews>
<sheetFormatPr defaultRowHeight="15"/>
<sheetData/>
<dataValidations count="1">
<dataValidation type="list" allowBlank="1" showDropDown="1" showInputMessage="1" showErrorMessage="1" sqref="A1">
<formula1>"Foo,Bar,Baz"</formula1>
</dataValidation>
</dataValidations>
<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>
"#,
);

assert_eq!(expected, got);

Ok(())
}
}
8 changes: 8 additions & 0 deletions src/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13462,6 +13462,10 @@ impl Worksheet {
attributes.push(("allowBlank", "1".to_string()));
}

if !data_validation.show_dropdown {
attributes.push(("showDropDown", "1".to_string()));
}

if data_validation.show_input_message {
attributes.push(("showInputMessage", "1".to_string()));
}
Expand Down Expand Up @@ -13519,6 +13523,10 @@ impl Worksheet {
attributes.push(("allowBlank", "1".to_string()));
}

if !data_validation.show_dropdown {
attributes.push(("showDropDown", "1".to_string()));
}

if data_validation.show_input_message {
attributes.push(("showInputMessage", "1".to_string()));
}
Expand Down

0 comments on commit ac6b754

Please sign in to comment.