From 523726fdc5bebc013be051fda7fc9d750e369f48 Mon Sep 17 00:00:00 2001 From: John McNamara Date: Fri, 17 Nov 2023 01:23:20 +0000 Subject: [PATCH] cond format: minor doc additions and refactoring --- src/conditional_format.rs | 39 ++++++++++++++++++++++++-------- src/conditional_format/tests.rs | 31 ------------------------- tests/integration/bootstrap35.rs | 2 +- 3 files changed, 30 insertions(+), 42 deletions(-) diff --git a/src/conditional_format.rs b/src/conditional_format.rs index f0c0068c..184e73a7 100644 --- a/src/conditional_format.rs +++ b/src/conditional_format.rs @@ -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, @@ -4639,6 +4639,7 @@ impl ConditionalFormatDataBar { false, ); + // Write the max type/value. Self::write_type( &mut writer, self.max_type, @@ -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() } @@ -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, @@ -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 { @@ -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. @@ -5561,5 +5580,5 @@ fn range_to_anchor(range: &str) -> String { anchor = &anchor[0..position]; } - anchor.to_string() + anchor } diff --git a/src/conditional_format/tests.rs b/src/conditional_format/tests.rs index a8e0418e..1bb3c750 100644 --- a/src/conditional_format/tests.rs +++ b/src/conditional_format/tests.rs @@ -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)?; diff --git a/tests/integration/bootstrap35.rs b/tests/integration/bootstrap35.rs index ed3f58b5..0abc2086 100644 --- a/tests/integration/bootstrap35.rs +++ b/tests/integration/bootstrap35.rs @@ -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)?;