Skip to content

Commit

Permalink
cond format: minor doc additions and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Nov 17, 2023
1 parent 7b82c64 commit 523726f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 42 deletions.
39 changes: 29 additions & 10 deletions src/conditional_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4623,14 +4623,14 @@ impl ConditionalFormatDataBar {
// Write the rule.
writer.xml_start_tag("cfRule", &attributes);

// Set the bar attributes, if any.
let mut attributes = vec![];

if self.bar_only {
attributes.push(("showValue", "0".to_string()));
}

writer.xml_start_tag("dataBar", &attributes);

// Write the min type/value.
Self::write_type(
&mut writer,
self.min_type,
Expand All @@ -4639,6 +4639,7 @@ impl ConditionalFormatDataBar {
false,
);

// Write the max type/value.
Self::write_type(
&mut writer,
self.max_type,
Expand All @@ -4647,17 +4648,18 @@ impl ConditionalFormatDataBar {
true,
);

// Write the bar/fill color.
Self::write_color(&mut writer, "color", self.fill_color);

writer.xml_end_tag("dataBar");

// Write the extLst element to indicate x14 extensions for Excel 2010+
// data bars.
if self.has_x14_extensions {
// Write the extLst element.
Self::write_extension_list(&mut writer, guid);
}

writer.xml_end_tag("cfRule");

writer.read_to_string()
}

Expand Down Expand Up @@ -4754,6 +4756,7 @@ impl ConditionalFormatDataBar {
Self::write_color(&mut writer, "x14:borderColor", self.border_color);
}

// Excel doesn't write some color tags if they match the fill color.
if self.fill_color != self.negative_fill_color {
Self::write_color(
&mut writer,
Expand Down Expand Up @@ -4884,10 +4887,26 @@ impl ConditionalFormatDataBar {
// ConditionalFormatValue
// -----------------------------------------------------------------------

/// The `ConditionalFormatValue` struct represents conditional format
/// value types.
/// The `ConditionalFormatValue` struct represents conditional format value
/// types.
///
/// Excel supports various types when specifying values in a conditional format
/// such as numbers, strings, dates, times and cell references.
/// `ConditionalFormatValue` is used to support a similar generic interface to
/// conditional format values. It supports:
///
/// - Numbers: Any Rust number that can convert [`Into`] [`f64`].
/// - Strings: Any Rust string type that can convert into String such as
/// [`&str`], [`String`], `&String` and `Cow<'_, str>`.
/// - Dates/times: [`ExcelDateTime`] values and if the `chrono` feature is
/// enabled [`chrono::NaiveDateTime`], [`chrono::NaiveDate`] and
/// [`chrono::NaiveTime`].
/// - Cell ranges: Use [`Formula`] in order to distinguish from strings. For
/// example `Formula::new(=A1)`.
///
/// TODO - Explain `ConditionalFormatValue`
/// [`chrono::NaiveDate`]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html
/// [`chrono::NaiveTime`]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveTime.html
/// [`chrono::NaiveDateTime`]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDateTime.html
///
#[derive(Clone)]
pub struct ConditionalFormatValue {
Expand Down Expand Up @@ -5547,8 +5566,8 @@ generate_conditional_common_methods!(
// Common methods.
// -----------------------------------------------------------------------

// TODO COW
fn range_to_anchor(range: &str) -> String {
// Extract the first cell from a range (potentially a multi range).
fn range_to_anchor(range: &str) -> &str {
let mut anchor = range;

// Extract cell/range from optional space separated list.
Expand All @@ -5561,5 +5580,5 @@ fn range_to_anchor(range: &str) -> String {
anchor = &anchor[0..position];
}

anchor.to_string()
anchor
}
31 changes: 0 additions & 31 deletions src/conditional_format/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1619,37 +1619,6 @@ mod conditional_format_tests {
worksheet.write(2, 0, 30)?;
worksheet.write(3, 0, 40)?;

/* todo
worksheet.conditional_formatting("A1:A4",
:
'type': "formula",
'criteria': "=$A$1>5",
'format': format,
);
worksheet.conditional_formatting("A1:A4",
:
'type': "formula",
'criteria': "=$A$2<80",
'format': format,
);
worksheet.conditional_formatting("A1:A4",
:
'type': "formula",
'criteria': ""1+2"",
'format': format,
);
worksheet.conditional_formatting("A1:A4",
:
'type': "formula",
'criteria': "=$A$3>$A$4",
'format': format,
);
*/

let conditional_format = ConditionalFormatFormula::new().set_value("=$A$1>5");

worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/bootstrap35.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn create_new_xlsx_file_2(filename: &str) -> Result<(), XlsxError> {
let format1 = Format::new().set_bold();

worksheet.write(0, 0, Formula::new("=1+2+3-6"))?;
worksheet.write(1, 0, Formula::new("=SIN(0)".to_string()))?; // Uses Sting type.
worksheet.write(1, 0, Formula::new("=SIN(0)".to_string()))?; // Uses String type.
worksheet.write_with_format(2, 0, Formula::new("SIN(0)"), &format1)?; // No equals sign.
worksheet.write(3, 0, Formula::new("=1+1").set_result("2"))?;
worksheet.write_with_format(4, 0, Formula::new("1+1").set_result("2"), &format1)?;
Expand Down

0 comments on commit 523726f

Please sign in to comment.