From d777173183880a50da1e756451c5d6929622f99a Mon Sep 17 00:00:00 2001 From: John McNamara Date: Wed, 22 Nov 2023 23:47:25 +0000 Subject: [PATCH] cond format: refactor format rules with generic enum values --- examples/app_conditional_formatting.rs | 54 +- examples/doc_conditional_format_anchor.rs | 8 +- examples/doc_conditional_format_average.rs | 4 +- examples/doc_conditional_format_cell1.rs | 8 +- examples/doc_conditional_format_cell2.rs | 10 +- ...doc_conditional_format_cell_set_minimum.rs | 6 +- .../doc_conditional_format_cell_set_value.rs | 5 +- examples/doc_conditional_format_date.rs | 9 +- examples/doc_conditional_format_formula.rs | 4 +- .../doc_conditional_format_multi_range.rs | 8 +- examples/doc_conditional_format_text.rs | 16 +- examples/doc_conditional_format_top.rs | 10 +- src/conditional_format.rs | 826 ++++++++++-------- src/conditional_format/tests.rs | 200 ++--- src/cookbook.rs | 55 +- src/worksheet.rs | 8 +- tests/integration/cond_format01.rs | 5 +- tests/integration/cond_format02.rs | 7 +- tests/integration/cond_format03.rs | 10 +- tests/integration/cond_format04.rs | 8 +- tests/integration/cond_format05.rs | 5 +- tests/integration/cond_format06.rs | 6 +- tests/integration/cond_format07.rs | 8 +- tests/integration/cond_format08.rs | 5 +- tests/integration/cond_format10.rs | 5 +- tests/integration/cond_format11.rs | 6 +- tests/integration/cond_format12.rs | 6 +- tests/integration/cond_format14.rs | 5 +- tests/integration/cond_format15.rs | 8 +- tests/integration/cond_format16.rs | 8 +- tests/integration/cond_format19.rs | 5 +- 31 files changed, 667 insertions(+), 661 deletions(-) diff --git a/examples/app_conditional_formatting.rs b/examples/app_conditional_formatting.rs index fc00cd0a..404d2363 100644 --- a/examples/app_conditional_formatting.rs +++ b/examples/app_conditional_formatting.rs @@ -6,14 +6,14 @@ //! rust_xlsxwriter library. //! //! Conditional formatting allows you to apply a format to a cell or a range of -//! cells based on certain criteria. +//! cells based on user defined rule. use rust_xlsxwriter::{ ConditionalFormat2ColorScale, ConditionalFormat3ColorScale, ConditionalFormatAverage, - ConditionalFormatAverageCriteria, ConditionalFormatCell, ConditionalFormatCellCriteria, + ConditionalFormatAverageRule, ConditionalFormatCell, ConditionalFormatCellRule, ConditionalFormatDataBar, ConditionalFormatDataBarDirection, ConditionalFormatDuplicate, ConditionalFormatFormula, ConditionalFormatIconSet, ConditionalFormatIconType, - ConditionalFormatText, ConditionalFormatTextCriteria, ConditionalFormatTop, Format, Workbook, + ConditionalFormatText, ConditionalFormatTextRule, ConditionalFormatTop, Format, Workbook, XlsxError, }; @@ -72,16 +72,14 @@ fn main() -> Result<(), XlsxError> { // Write a conditional format over a range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) - .set_value(50) + .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) .set_format(&format1); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; // Write another conditional format over the same range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::LessThan) - .set_value(50) + .set_rule(ConditionalFormatCellRule::LessThan(50)) .set_format(&format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -108,18 +106,14 @@ fn main() -> Result<(), XlsxError> { // Write a conditional format over a range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::Between) - .set_minimum(30) - .set_maximum(70) + .set_rule(ConditionalFormatCellRule::Between(30, 70)) .set_format(&format1); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; // Write another conditional format over the same range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::NotBetween) - .set_minimum(30) - .set_maximum(70) + .set_rule(ConditionalFormatCellRule::NotBetween(30, 70)) .set_format(&format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -182,7 +176,7 @@ fn main() -> Result<(), XlsxError> { // Write another conditional format over the same range. let conditional_format = ConditionalFormatAverage::new() - .set_criteria(ConditionalFormatAverageCriteria::BelowAverage) + .set_rule(ConditionalFormatAverageRule::BelowAverage) .set_format(&format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -208,16 +202,14 @@ fn main() -> Result<(), XlsxError> { // Write a conditional format over a range. let conditional_format = ConditionalFormatTop::new() - .set_value(10) + .set_rule(rust_xlsxwriter::ConditionalFormatTopRule::Top(10)) .set_format(&format1); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; - // Invert the Top conditional format to show Bottom values in the same - // range. + // Also show the bottom values in the same range. let conditional_format = ConditionalFormatTop::new() - .invert() - .set_value(10) + .set_rule(rust_xlsxwriter::ConditionalFormatTopRule::Bottom(10)) .set_format(&format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -243,8 +235,7 @@ fn main() -> Result<(), XlsxError> { // Write a conditional format over a non-contiguous range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) - .set_value(50) + .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) .set_multi_range("B3:D6 I3:K6 B9:D12 I9:K12") .set_format(&format1); @@ -252,8 +243,7 @@ fn main() -> Result<(), XlsxError> { // Write another conditional format over the same range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::LessThan) - .set_value(50) + .set_rule(ConditionalFormatCellRule::LessThan(50)) .set_multi_range("B3:D6 I3:K6 B9:D12 I9:K12") .set_format(&format2); @@ -280,14 +270,14 @@ fn main() -> Result<(), XlsxError> { // Write a conditional format over a range. let conditional_format = ConditionalFormatFormula::new() - .set_value("=ISODD(B3)") + .set_rule("=ISODD(B3)") .set_format(&format1); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; // Write another conditional format over the same range. let conditional_format = ConditionalFormatFormula::new() - .set_value("=ISEVEN(B3)") + .set_rule("=ISEVEN(B3)") .set_format(&format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -329,32 +319,30 @@ fn main() -> Result<(), XlsxError> { // Write a text "containing" conditional format over a range. let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::Contains) - .set_value("rust") + .set_rule(ConditionalFormatTextRule::Contains("rust".to_string())) .set_format(&format2); worksheet.add_conditional_format(1, 0, 13, 0, &conditional_format)?; // Write a text "not containing" conditional format over the same range. let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::DoesNotContain) - .set_value("rust") + .set_rule(ConditionalFormatTextRule::DoesNotContain( + "rust".to_string(), + )) .set_format(&format1); worksheet.add_conditional_format(1, 0, 13, 0, &conditional_format)?; // Write a text "begins with" conditional format over a range. let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::BeginsWith) - .set_value("t") + .set_rule(ConditionalFormatTextRule::BeginsWith("t".to_string())) .set_format(&format2); worksheet.add_conditional_format(1, 2, 13, 2, &conditional_format)?; // Write a text "ends with" conditional format over the same range. let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::EndsWith) - .set_value("t") + .set_rule(ConditionalFormatTextRule::EndsWith("t".to_string())) .set_format(&format1); worksheet.add_conditional_format(1, 2, 13, 2, &conditional_format)?; diff --git a/examples/doc_conditional_format_anchor.rs b/examples/doc_conditional_format_anchor.rs index 1c73cf26..1ac99875 100644 --- a/examples/doc_conditional_format_anchor.rs +++ b/examples/doc_conditional_format_anchor.rs @@ -37,7 +37,7 @@ fn main() -> Result<(), XlsxError> { // The rule is applied to each cell in the range. let conditional_format = ConditionalFormatFormula::new() - .set_value("=ISEVEN(B3)") + .set_rule("=ISEVEN(B3)") .set_format(&format); worksheet.add_conditional_format(2, 1, 11, 8, &conditional_format)?; @@ -48,7 +48,7 @@ fn main() -> Result<(), XlsxError> { // The rule is applied to each row based on the first row in the column. let conditional_format = ConditionalFormatFormula::new() - .set_value("=ISEVEN($B3)") + .set_rule("=ISEVEN($B3)") .set_format(&format); worksheet.add_conditional_format(2, 1, 11, 8, &conditional_format)?; @@ -59,7 +59,7 @@ fn main() -> Result<(), XlsxError> { // The rule is applied to each column based on the first cell in the column. let conditional_format = ConditionalFormatFormula::new() - .set_value("=ISEVEN(B$3)") + .set_rule("=ISEVEN(B$3)") .set_format(&format); worksheet.add_conditional_format(2, 1, 11, 8, &conditional_format)?; @@ -70,7 +70,7 @@ fn main() -> Result<(), XlsxError> { // The rule is applied to the entire range based on the first cell in the range. let conditional_format = ConditionalFormatFormula::new() - .set_value("=ISEVEN($B$3)") + .set_rule("=ISEVEN($B$3)") .set_format(format); worksheet.add_conditional_format(2, 1, 11, 8, &conditional_format)?; diff --git a/examples/doc_conditional_format_average.rs b/examples/doc_conditional_format_average.rs index f5ee9ef8..14320677 100644 --- a/examples/doc_conditional_format_average.rs +++ b/examples/doc_conditional_format_average.rs @@ -6,7 +6,7 @@ //! average values are in light red. Below average values are in light green. use rust_xlsxwriter::{ - ConditionalFormatAverage, ConditionalFormatAverageCriteria, Format, Workbook, XlsxError, + ConditionalFormatAverage, ConditionalFormatAverageRule, Format, Workbook, XlsxError, }; fn main() -> Result<(), XlsxError> { @@ -51,7 +51,7 @@ fn main() -> Result<(), XlsxError> { // Write another conditional format over the same range. let conditional_format = ConditionalFormatAverage::new() - .set_criteria(ConditionalFormatAverageCriteria::BelowAverage) + .set_rule(ConditionalFormatAverageRule::BelowAverage) .set_format(format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; diff --git a/examples/doc_conditional_format_cell1.rs b/examples/doc_conditional_format_cell1.rs index 3a88cf8e..091a3168 100644 --- a/examples/doc_conditional_format_cell1.rs +++ b/examples/doc_conditional_format_cell1.rs @@ -6,7 +6,7 @@ //! with values >= 50 are in light red. Values < 50 are in light green. use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; fn main() -> Result<(), XlsxError> { @@ -46,16 +46,14 @@ fn main() -> Result<(), XlsxError> { // Write a conditional format over a range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) - .set_value(50) + .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) .set_format(format1); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; // Write another conditional format over the same range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::LessThan) - .set_value(50) + .set_rule(ConditionalFormatCellRule::LessThan(50)) .set_format(format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; diff --git a/examples/doc_conditional_format_cell2.rs b/examples/doc_conditional_format_cell2.rs index d659b704..527a1aeb 100644 --- a/examples/doc_conditional_format_cell2.rs +++ b/examples/doc_conditional_format_cell2.rs @@ -7,7 +7,7 @@ //! are in light green. use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; fn main() -> Result<(), XlsxError> { @@ -47,18 +47,14 @@ fn main() -> Result<(), XlsxError> { // Write a conditional format over a range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::Between) - .set_minimum(30) - .set_maximum(70) + .set_rule(ConditionalFormatCellRule::Between(30, 70)) .set_format(format1); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; // Write another conditional format over the same range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::NotBetween) - .set_minimum(30) - .set_maximum(70) + .set_rule(ConditionalFormatCellRule::NotBetween(30, 70)) .set_format(format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; diff --git a/examples/doc_conditional_format_cell_set_minimum.rs b/examples/doc_conditional_format_cell_set_minimum.rs index 353ae941..54780f32 100644 --- a/examples/doc_conditional_format_cell_set_minimum.rs +++ b/examples/doc_conditional_format_cell_set_minimum.rs @@ -6,7 +6,7 @@ //! between 40 and 60 are highlighted in light green. use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; fn main() -> Result<(), XlsxError> { @@ -26,9 +26,7 @@ fn main() -> Result<(), XlsxError> { // Write a conditional format over a range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::Between) - .set_minimum(40) - .set_maximum(60) + .set_rule(ConditionalFormatCellRule::Between(40, 60)) .set_format(format); worksheet.add_conditional_format(0, 0, 9, 0, &conditional_format)?; diff --git a/examples/doc_conditional_format_cell_set_value.rs b/examples/doc_conditional_format_cell_set_value.rs index 574c3310..1f53e3fa 100644 --- a/examples/doc_conditional_format_cell_set_value.rs +++ b/examples/doc_conditional_format_cell_set_value.rs @@ -6,7 +6,7 @@ //! with values >= 50 are in light green. use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; fn main() -> Result<(), XlsxError> { @@ -26,8 +26,7 @@ fn main() -> Result<(), XlsxError> { // Write a conditional format over a range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) - .set_value(50) + .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) .set_format(format); worksheet.add_conditional_format(0, 0, 9, 0, &conditional_format)?; diff --git a/examples/doc_conditional_format_date.rs b/examples/doc_conditional_format_date.rs index cac6f1b8..acdc4d91 100644 --- a/examples/doc_conditional_format_date.rs +++ b/examples/doc_conditional_format_date.rs @@ -9,8 +9,7 @@ //! run the example. use rust_xlsxwriter::{ - ConditionalFormatDate, ConditionalFormatDateCriteria, ExcelDateTime, Format, Workbook, - XlsxError, + ConditionalFormatDate, ConditionalFormatDateRule, ExcelDateTime, Format, Workbook, XlsxError, }; fn main() -> Result<(), XlsxError> { @@ -66,19 +65,19 @@ fn main() -> Result<(), XlsxError> { // Write conditional format over the same range. let conditional_format = ConditionalFormatDate::new() - .set_criteria(ConditionalFormatDateCriteria::LastMonth) + .set_rule(ConditionalFormatDateRule::LastMonth) .set_format(format1); worksheet.add_conditional_format(0, 0, 10, 0, &conditional_format)?; let conditional_format = ConditionalFormatDate::new() - .set_criteria(ConditionalFormatDateCriteria::ThisMonth) + .set_rule(ConditionalFormatDateRule::ThisMonth) .set_format(format2); worksheet.add_conditional_format(0, 0, 10, 0, &conditional_format)?; let conditional_format = ConditionalFormatDate::new() - .set_criteria(ConditionalFormatDateCriteria::NextMonth) + .set_rule(ConditionalFormatDateRule::NextMonth) .set_format(format3); worksheet.add_conditional_format(0, 0, 10, 0, &conditional_format)?; diff --git a/examples/doc_conditional_format_formula.rs b/examples/doc_conditional_format_formula.rs index 79948525..d1ec712c 100644 --- a/examples/doc_conditional_format_formula.rs +++ b/examples/doc_conditional_format_formula.rs @@ -45,14 +45,14 @@ fn main() -> Result<(), XlsxError> { // Write a conditional format over a range. let conditional_format = ConditionalFormatFormula::new() - .set_value("=ISODD(B3)") + .set_rule("=ISODD(B3)") .set_format(format1); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; // Write another conditional format over the same range. let conditional_format = ConditionalFormatFormula::new() - .set_value("=ISEVEN(B3)") + .set_rule("=ISEVEN(B3)") .set_format(format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; diff --git a/examples/doc_conditional_format_multi_range.rs b/examples/doc_conditional_format_multi_range.rs index a0afe946..e18782b9 100644 --- a/examples/doc_conditional_format_multi_range.rs +++ b/examples/doc_conditional_format_multi_range.rs @@ -8,7 +8,7 @@ //! have any conditional formatting. use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; fn main() -> Result<(), XlsxError> { @@ -48,8 +48,7 @@ fn main() -> Result<(), XlsxError> { // Write a conditional format over a non-contiguous range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) - .set_value(50) + .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) .set_multi_range("B3:D6 I3:K6 B9:D12 I9:K12") .set_format(format1); @@ -57,8 +56,7 @@ fn main() -> Result<(), XlsxError> { // Write another conditional format over the same range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::LessThan) - .set_value(50) + .set_rule(ConditionalFormatCellRule::LessThan(50)) .set_multi_range("B3:D6 I3:K6 B9:D12 I9:K12") .set_format(format2); diff --git a/examples/doc_conditional_format_text.rs b/examples/doc_conditional_format_text.rs index 9d6ea41a..e5684adc 100644 --- a/examples/doc_conditional_format_text.rs +++ b/examples/doc_conditional_format_text.rs @@ -5,7 +5,7 @@ //! Example of adding a text type conditional formatting to a worksheet. use rust_xlsxwriter::{ - ConditionalFormatText, ConditionalFormatTextCriteria, Format, Workbook, XlsxError, + ConditionalFormatText, ConditionalFormatTextRule, Format, Workbook, XlsxError, }; fn main() -> Result<(), XlsxError> { @@ -48,32 +48,30 @@ fn main() -> Result<(), XlsxError> { // Write a text "containing" conditional format over a range. let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::Contains) - .set_value("rust") + .set_rule(ConditionalFormatTextRule::Contains("rust".to_string())) .set_format(&format1); worksheet.add_conditional_format(0, 0, 12, 0, &conditional_format)?; // Write a text "not containing" conditional format over the same range. let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::DoesNotContain) - .set_value("rust") + .set_rule(ConditionalFormatTextRule::DoesNotContain( + "rust".to_string(), + )) .set_format(&format2); worksheet.add_conditional_format(0, 0, 12, 0, &conditional_format)?; // Write a text "begins with" conditional format over a range. let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::BeginsWith) - .set_value("t") + .set_rule(ConditionalFormatTextRule::BeginsWith("t".to_string())) .set_format(&format1); worksheet.add_conditional_format(0, 2, 12, 2, &conditional_format)?; // Write a text "ends with" conditional format over the same range. let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::EndsWith) - .set_value("t") + .set_rule(ConditionalFormatTextRule::EndsWith("t".to_string())) .set_format(&format2); worksheet.add_conditional_format(0, 2, 12, 2, &conditional_format)?; diff --git a/examples/doc_conditional_format_top.rs b/examples/doc_conditional_format_top.rs index 0fe4ae6b..2d530be4 100644 --- a/examples/doc_conditional_format_top.rs +++ b/examples/doc_conditional_format_top.rs @@ -5,7 +5,9 @@ //! Example of how to add Top and Bottom conditional formatting to a worksheet. //! Top 10 values are in light red. Bottom 10 values are in light green. -use rust_xlsxwriter::{ConditionalFormatTop, Format, Workbook, XlsxError}; +use rust_xlsxwriter::{ + ConditionalFormatTop, ConditionalFormatTopRule, Format, Workbook, XlsxError, +}; fn main() -> Result<(), XlsxError> { // Create a new Excel file object. @@ -44,16 +46,16 @@ fn main() -> Result<(), XlsxError> { // Write a conditional format over a range. let conditional_format = ConditionalFormatTop::new() - .set_value(10) + .set_rule(ConditionalFormatTopRule::Top(10)) .set_format(format1); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; // Invert the Top conditional format to show Bottom values. let conditional_format = ConditionalFormatTop::new() - .invert() - .set_value(10) + .set_rule(ConditionalFormatTopRule::Bottom(10)) .set_format(format2); + worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; // Save the file. diff --git a/src/conditional_format.rs b/src/conditional_format.rs index 36eb5c77..329430fb 100644 --- a/src/conditional_format.rs +++ b/src/conditional_format.rs @@ -21,7 +21,7 @@ //! # // This code is available in examples/doc_conditional_format_cell1.rs //! # //! # use rust_xlsxwriter::{ -//! # ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, +//! # ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, //! # }; //! # //! # fn main() -> Result<(), XlsxError> { @@ -49,28 +49,26 @@ //! # worksheet.set_column_width(col_num, 6)?; //! # } //! # -//! // Add a format. Light red fill with dark red text. -//! let format1 = Format::new() -//! .set_font_color("9C0006") -//! .set_background_color("FFC7CE"); -//! -//! // Add a format. Green fill with dark green text. -//! let format2 = Format::new() -//! .set_font_color("006100") -//! .set_background_color("C6EFCE"); -//! +//! # // Add a format. Light red fill with dark red text. +//! # let format1 = Format::new() +//! # .set_font_color("9C0006") +//! # .set_background_color("FFC7CE"); +//! # +//! # // Add a format. Green fill with dark green text. +//! # let format2 = Format::new() +//! # .set_font_color("006100") +//! # .set_background_color("C6EFCE"); +//! # //! // Write a conditional format over a range. //! let conditional_format = ConditionalFormatCell::new() -//! .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) -//! .set_value(50) +//! .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) //! .set_format(format1); //! //! worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; //! //! // Write another conditional format over the same range. //! let conditional_format = ConditionalFormatCell::new() -//! .set_criteria(ConditionalFormatCellCriteria::LessThan) -//! .set_value(50) +//! .set_rule(ConditionalFormatCellRule::LessThan(50)) //! .set_format(format2); //! //! worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -202,7 +200,7 @@ //! # // This code is available in examples/doc_conditional_format_multi_range.rs //! # //! # use rust_xlsxwriter::{ -//! # ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, +//! # ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, //! # }; //! # //! # fn main() -> Result<(), XlsxError> { @@ -242,8 +240,7 @@ //! # //! // Write a conditional format over a non-contiguous range. //! let conditional_format = ConditionalFormatCell::new() -//! .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) -//! .set_value(50) +//! .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) //! .set_multi_range("B3:D6 I3:K6 B9:D12 I9:K12") //! .set_format(format1); //! @@ -251,8 +248,7 @@ //! //! // Write another conditional format over the same range. //! let conditional_format = ConditionalFormatCell::new() -//! .set_criteria(ConditionalFormatCellCriteria::LessThan) -//! .set_value(50) +//! .set_rule(ConditionalFormatCellRule::LessThan(50)) //! .set_multi_range("B3:D6 I3:K6 B9:D12 I9:K12") //! .set_format(format2); //! @@ -342,7 +338,7 @@ //! # //! // The rule is applied to each cell in the range. //! let conditional_format = ConditionalFormatFormula::new() -//! .set_value("=ISEVEN(B3)") +//! .set_rule("=ISEVEN(B3)") //! .set_format(&format); //! //! # worksheet.add_conditional_format(2, 1, 11, 8, &conditional_format)?; @@ -353,7 +349,7 @@ //! # //! // The rule is applied to each row based on the first row in the column. //! let conditional_format = ConditionalFormatFormula::new() -//! .set_value("=ISEVEN($B3)") +//! .set_rule("=ISEVEN($B3)") //! .set_format(&format); //! //! # worksheet.add_conditional_format(2, 1, 11, 8, &conditional_format)?; @@ -364,7 +360,7 @@ //! # //! // The rule is applied to each column based on the first cell in the column. //! let conditional_format = ConditionalFormatFormula::new() -//! .set_value("=ISEVEN(B$3)") +//! .set_rule("=ISEVEN(B$3)") //! .set_format(&format); //! //! # worksheet.add_conditional_format(2, 1, 11, 8, &conditional_format)?; @@ -375,7 +371,7 @@ //! # //! // The rule is applied to the entire range based on the first cell in the range. //! let conditional_format = ConditionalFormatFormula::new() -//! .set_value("=ISEVEN($B$3)") +//! .set_rule("=ISEVEN($B$3)") //! .set_format(format); //! //! # worksheet.add_conditional_format(2, 1, 11, 8, &conditional_format)?; @@ -411,10 +407,9 @@ //! //! # Examples //! -//! Conditional formatting is a feature of Excel which allows you to apply a -//! format to a cell or a range of cells based on certain criteria. For example -//! you might apply rules like the following to highlight cells in different -//! ranges. +//! Conditional formatting is a feature of Excel which allows you to apply a format +//! to a cell or a range of cells based on user defined rules. For example you might +//! apply rules like the following to highlight cells in different ranges. //! //! //! @@ -439,16 +434,14 @@ //! //! // Write a conditional format over a range. //! let conditional_format = ConditionalFormatCell::new() -//! .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) -//! .set_value(50) +//! .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) //! .set_format(&format1); //! //! worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; //! //! // Write another conditional format over the same range. //! let conditional_format = ConditionalFormatCell::new() -//! .set_criteria(ConditionalFormatCellCriteria::LessThan) -//! .set_value(50) +//! .set_rule(ConditionalFormatCellRule::LessThan(50)) //! .set_format(&format2); //! //! worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -471,18 +464,14 @@ //! //! // Write a conditional format over a range. //! let conditional_format = ConditionalFormatCell::new() -//! .set_criteria(ConditionalFormatCellCriteria::Between) -//! .set_minimum(30) -//! .set_maximum(70) +//! .set_rule(ConditionalFormatCellRule::Between(30, 70)) //! .set_format(&format1); //! //! worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; //! //! // Write another conditional format over the same range. //! let conditional_format = ConditionalFormatCell::new() -//! .set_criteria(ConditionalFormatCellCriteria::NotBetween) -//! .set_minimum(30) -//! .set_maximum(70) +//! .set_rule(ConditionalFormatCellRule::NotBetween(30, 70)) //! .set_format(&format2); //! //! worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -539,7 +528,7 @@ //! //! // Write another conditional format over the same range. //! let conditional_format = ConditionalFormatAverage::new() -//! .set_criteria(ConditionalFormatAverageCriteria::BelowAverage) +//! .set_rule(ConditionalFormatAverageRule::BelowAverage) //! .set_format(&format2); //! //! worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -562,16 +551,14 @@ //! //! // Write a conditional format over a range. //! let conditional_format = ConditionalFormatTop::new() -//! .set_value(10) +//! .set_rule(rust_xlsxwriter::ConditionalFormatTopRule::Top(10)) //! .set_format(&format1); //! //! worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; //! -//! // Invert the Top conditional format to show Bottom values in the same -//! // range. +//! // Also show the bottom values in the same range. //! let conditional_format = ConditionalFormatTop::new() -//! .invert() -//! .set_value(10) +//! .set_rule(rust_xlsxwriter::ConditionalFormatTopRule::Bottom(10)) //! .set_format(&format2); //! //! worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -591,8 +578,7 @@ //! //! // Write a conditional format over a non-contiguous range. //! let conditional_format = ConditionalFormatCell::new() -//! .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) -//! .set_value(50) +//! .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) //! .set_multi_range("B3:D6 I3:K6 B9:D12 I9:K12") //! .set_format(&format1); //! @@ -600,8 +586,7 @@ //! //! // Write another conditional format over the same range. //! let conditional_format = ConditionalFormatCell::new() -//! .set_criteria(ConditionalFormatCellCriteria::LessThan) -//! .set_value(50) +//! .set_rule(ConditionalFormatCellRule::LessThan(50)) //! .set_multi_range("B3:D6 I3:K6 B9:D12 I9:K12") //! .set_format(&format2); //! @@ -625,14 +610,14 @@ //! //! // Write a conditional format over a range. //! let conditional_format = ConditionalFormatFormula::new() -//! .set_value("=ISODD(B3)") +//! .set_rule("=ISODD(B3)") //! .set_format(&format1); //! //! worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; //! //! // Write another conditional format over the same range. //! let conditional_format = ConditionalFormatFormula::new() -//! .set_value("=ISEVEN(B3)") +//! .set_rule("=ISEVEN(B3)") //! .set_format(&format2); //! //! worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -655,32 +640,30 @@ //! //! // Write a text "containing" conditional format over a range. //! let conditional_format = ConditionalFormatText::new() -//! .set_criteria(ConditionalFormatTextCriteria::Contains) -//! .set_value("rust") +//! .set_rule(ConditionalFormatTextRule::Contains("rust".to_string())) //! .set_format(&format2); //! //! worksheet.add_conditional_format(1, 0, 13, 0, &conditional_format)?; //! //! // Write a text "not containing" conditional format over the same range. //! let conditional_format = ConditionalFormatText::new() -//! .set_criteria(ConditionalFormatTextCriteria::DoesNotContain) -//! .set_value("rust") +//! .set_rule(ConditionalFormatTextRule::DoesNotContain( +//! "rust".to_string(), +//! )) //! .set_format(&format1); //! //! worksheet.add_conditional_format(1, 0, 13, 0, &conditional_format)?; //! //! // Write a text "begins with" conditional format over a range. //! let conditional_format = ConditionalFormatText::new() -//! .set_criteria(ConditionalFormatTextCriteria::BeginsWith) -//! .set_value("t") +//! .set_rule(ConditionalFormatTextRule::BeginsWith("t".to_string())) //! .set_format(&format2); //! //! worksheet.add_conditional_format(1, 2, 13, 2, &conditional_format)?; //! //! // Write a text "ends with" conditional format over the same range. //! let conditional_format = ConditionalFormatText::new() -//! .set_criteria(ConditionalFormatTextCriteria::EndsWith) -//! .set_value("t") +//! .set_rule(ConditionalFormatTextRule::EndsWith("t".to_string())) //! .set_format(&format1); //! //! worksheet.add_conditional_format(1, 2, 13, 2, &conditional_format)?; @@ -921,6 +904,8 @@ //! //! worksheet.add_conditional_format(13, 1, 13, 5, &conditional_format)?; //! ``` +//! + #![warn(missing_docs)] mod tests; @@ -1056,7 +1041,7 @@ generate_conditional_format_impls!( /// # // This code is available in examples/doc_conditional_format_cell1.rs /// # /// # use rust_xlsxwriter::{ -/// # ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, +/// # ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, /// # }; /// # /// # fn main() -> Result<(), XlsxError> { @@ -1096,16 +1081,14 @@ generate_conditional_format_impls!( /// # /// // Write a conditional format over a range. /// let conditional_format = ConditionalFormatCell::new() -/// .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) -/// .set_value(50) +/// .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) /// .set_format(format1); /// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; /// /// // Write another conditional format over the same range. /// let conditional_format = ConditionalFormatCell::new() -/// .set_criteria(ConditionalFormatCellCriteria::LessThan) -/// .set_value(50) +/// .set_rule(ConditionalFormatCellRule::LessThan(50)) /// .set_format(format2); /// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -1135,7 +1118,7 @@ generate_conditional_format_impls!( /// # // This code is available in examples/doc_conditional_format_cell2.rs /// # /// # use rust_xlsxwriter::{ -/// # ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, +/// # ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, /// # }; /// # /// # fn main() -> Result<(), XlsxError> { @@ -1175,18 +1158,14 @@ generate_conditional_format_impls!( /// # /// // Write a conditional format over a range. /// let conditional_format = ConditionalFormatCell::new() -/// .set_criteria(ConditionalFormatCellCriteria::Between) -/// .set_minimum(30) -/// .set_maximum(70) +/// .set_rule(ConditionalFormatCellRule::Between(30, 70)) /// .set_format(format1); /// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; /// /// // Write another conditional format over the same range. /// let conditional_format = ConditionalFormatCell::new() -/// .set_criteria(ConditionalFormatCellCriteria::NotBetween) -/// .set_minimum(30) -/// .set_maximum(70) +/// .set_rule(ConditionalFormatCellRule::NotBetween(30, 70)) /// .set_format(format2); /// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -1198,6 +1177,7 @@ generate_conditional_format_impls!( /// # } /// ``` /// +/// /// This creates conditional format rules like this: /// /// @@ -1209,9 +1189,7 @@ generate_conditional_format_impls!( /// #[derive(Clone)] pub struct ConditionalFormatCell { - minimum: ConditionalFormatValue, - maximum: ConditionalFormatValue, - criteria: ConditionalFormatCellCriteria, + rule: Option>, multi_range: String, stop_if_true: bool, has_x14_extensions: bool, @@ -1224,9 +1202,7 @@ impl ConditionalFormatCell { #[allow(clippy::new_without_default)] pub fn new() -> ConditionalFormatCell { ConditionalFormatCell { - minimum: ConditionalFormatValue::new_from_string(""), - maximum: ConditionalFormatValue::new_from_string(""), - criteria: ConditionalFormatCellCriteria::EqualTo, + rule: None, multi_range: String::new(), stop_if_true: false, has_x14_extensions: false, @@ -1235,23 +1211,29 @@ impl ConditionalFormatCell { } } - /// Set the value of the Cell conditional format rule. + /// Set the Cell conditional format rule. + /// + /// The conditional format rules for `ConditionalFormatCell` are equalities + /// such as `=`, `!=`, `>`, `<`, `>=`, `<=`, `between` or `not between`. + /// These are defined in an [`ConditionalFormatCellRule`] enum instance + /// along with the value that the equality refers to. This + /// [`ConditionalFormatValue`] value can be a string, number, date, time or + /// range formula. /// /// # Parameters /// - /// * `value` - Any type that can convert into a [`ConditionalFormatValue`] - /// which is effectively all types supported by Excel. + /// * `rule` - A [`ConditionalFormatCellRule`] enum value. /// /// # Examples /// - /// Example of adding a cell type conditional formatting to a worksheet. Cells - /// with values >= 50 are in light green. + /// Example of adding a cell type conditional formatting to a worksheet. + /// Cells with values >= 50 are in light green. /// /// ``` /// # // This code is available in examples/doc_conditional_format_cell_set_value.rs /// # /// # use rust_xlsxwriter::{ - /// # ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + /// # ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, /// # }; /// # /// # fn main() -> Result<(), XlsxError> { @@ -1271,8 +1253,7 @@ impl ConditionalFormatCell { /// # /// // Write a conditional format over a range. /// let conditional_format = ConditionalFormatCell::new() - /// .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) - /// .set_value(50) + /// .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) /// .set_format(format); /// /// worksheet.add_conditional_format(0, 0, 9, 0, &conditional_format)?; @@ -1286,21 +1267,8 @@ impl ConditionalFormatCell { /// /// Output file: /// - /// - /// - pub fn set_value(self, value: impl Into) -> ConditionalFormatCell { - self.set_minimum(value) - } - - /// Set the minimum value of the Cell "between" and "not between" - /// conditional format rules. - /// - /// # Parameters - /// - /// * `value` - Any type that can convert into a [`ConditionalFormatValue`] - /// which is effectively all types supported by Excel. - /// - /// # Examples + /// /// /// Example of adding a cell type conditional formatting to a worksheet. /// Values between 40 and 60 are highlighted in light green. @@ -1309,7 +1277,7 @@ impl ConditionalFormatCell { /// # // This code is available in examples/doc_conditional_format_cell_set_minimum.rs /// # /// # use rust_xlsxwriter::{ - /// # ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + /// # ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, /// # }; /// # /// # fn main() -> Result<(), XlsxError> { @@ -1329,9 +1297,7 @@ impl ConditionalFormatCell { /// # /// // Write a conditional format over a range. /// let conditional_format = ConditionalFormatCell::new() - /// .set_criteria(ConditionalFormatCellCriteria::Between) - /// .set_minimum(40) - /// .set_maximum(60) + /// .set_rule(ConditionalFormatCellRule::Between(40, 60)) /// .set_format(format); /// /// worksheet.add_conditional_format(0, 0, 9, 0, &conditional_format)?; @@ -1348,46 +1314,57 @@ impl ConditionalFormatCell { /// /// - pub fn set_minimum( - mut self, - value: impl Into, - ) -> ConditionalFormatCell { - self.minimum = value.into(); - self.minimum.quote_string(); - self - } + pub fn set_rule(mut self, rule: ConditionalFormatCellRule) -> ConditionalFormatCell + where + T: IntoConditionalFormatValue, + { + // Change from a generic type to a concrete ConditionalFormatValue type. + let mut rule = match rule { + ConditionalFormatCellRule::EqualTo(value) => { + ConditionalFormatCellRule::EqualTo(value.new_value()) + } + ConditionalFormatCellRule::NotEqualTo(value) => { + ConditionalFormatCellRule::NotEqualTo(value.new_value()) + } + ConditionalFormatCellRule::GreaterThan(value) => { + ConditionalFormatCellRule::GreaterThan(value.new_value()) + } + ConditionalFormatCellRule::GreaterThanOrEqualTo(value) => { + ConditionalFormatCellRule::GreaterThanOrEqualTo(value.new_value()) + } + ConditionalFormatCellRule::LessThan(value) => { + ConditionalFormatCellRule::LessThan(value.new_value()) + } + ConditionalFormatCellRule::LessThanOrEqualTo(value) => { + ConditionalFormatCellRule::LessThanOrEqualTo(value.new_value()) + } + ConditionalFormatCellRule::Between(min, max) => { + ConditionalFormatCellRule::Between(min.new_value(), max.new_value()) + } + ConditionalFormatCellRule::NotBetween(min, max) => { + ConditionalFormatCellRule::NotBetween(min.new_value(), max.new_value()) + } + }; - /// Set the maximum value of the Cell "between" and "not between" - /// conditional format rules. - /// - /// Set the example above. - /// - /// # Parameters - /// - /// * `value` - Any type that can convert into a [`ConditionalFormatValue`] - /// which is effectively all types supported by Excel. - /// - pub fn set_maximum( - mut self, - value: impl Into, - ) -> ConditionalFormatCell { - self.maximum = value.into(); - self.maximum.quote_string(); - self - } + // Excel requires that strings in Cell style conditional formats are + // quoted. + match &mut rule { + ConditionalFormatCellRule::EqualTo(value) + | ConditionalFormatCellRule::NotEqualTo(value) + | ConditionalFormatCellRule::LessThan(value) + | ConditionalFormatCellRule::LessThanOrEqualTo(value) + | ConditionalFormatCellRule::GreaterThan(value) + | ConditionalFormatCellRule::GreaterThanOrEqualTo(value) => { + value.quote_string(); + } + ConditionalFormatCellRule::Between(min, max) + | ConditionalFormatCellRule::NotBetween(min, max) => { + min.quote_string(); + max.quote_string(); + } + } - /// Set the criteria for the conditional format rule such as `=`, `!=`, `>`, - /// `<`, `>=`, `<=`, `between` or `not between`. - /// - /// # Parameters - /// - /// * `criteria` - A [`ConditionalFormatCellCriteria`] enum value. - /// - pub fn set_criteria( - mut self, - criteria: ConditionalFormatCellCriteria, - ) -> ConditionalFormatCell { - self.criteria = criteria; + self.rule = Some(rule); self } @@ -1412,27 +1389,10 @@ impl ConditionalFormatCell { // Validate the conditional format. pub(crate) fn validate(&self) -> Result<(), XlsxError> { - let error_message = match self.criteria { - ConditionalFormatCellCriteria::Between | ConditionalFormatCellCriteria::NotBetween => { - if self.minimum.value.is_empty() { - "'minimum' value must be set".to_string() - } else if self.maximum.value.is_empty() { - "'maximum' value must be set".to_string() - } else { - String::new() - } - } - _ => { - if self.minimum.value.is_empty() { - "'value' must be set".to_string() - } else { - String::new() - } - } - }; - - if !error_message.is_empty() { - return Err(XlsxError::ConditionalFormatError(error_message)); + if self.rule.is_none() { + return Err(XlsxError::ConditionalFormatError( + "ConditionalFormatCell rule must be set".to_string(), + )); } Ok(()) @@ -1446,6 +1406,10 @@ impl ConditionalFormatCell { _range: &str, _guid: &str, ) -> String { + let Some(rule) = &self.rule else { + return String::new(); + }; + let mut writer = XMLWriter::new(); let mut attributes = vec![("type", "cellIs".to_string())]; @@ -1462,16 +1426,25 @@ impl ConditionalFormatCell { attributes.push(("stopIfTrue", "1".to_string())); } - attributes.push(("operator", self.criteria.to_string())); + attributes.push(("operator", rule.to_string())); // Write the rule. writer.xml_start_tag("cfRule", &attributes); - writer.xml_data_element_only("formula", &self.minimum.value); - if self.criteria == ConditionalFormatCellCriteria::Between - || self.criteria == ConditionalFormatCellCriteria::NotBetween - { - writer.xml_data_element_only("formula", &self.maximum.value); + match rule { + ConditionalFormatCellRule::EqualTo(value) + | ConditionalFormatCellRule::NotEqualTo(value) + | ConditionalFormatCellRule::LessThan(value) + | ConditionalFormatCellRule::LessThanOrEqualTo(value) + | ConditionalFormatCellRule::GreaterThan(value) + | ConditionalFormatCellRule::GreaterThanOrEqualTo(value) => { + writer.xml_data_element_only("formula", &value.value); + } + ConditionalFormatCellRule::Between(min, max) + | ConditionalFormatCellRule::NotBetween(min, max) => { + writer.xml_data_element_only("formula", &min.value); + writer.xml_data_element_only("formula", &max.value); + } } writer.xml_end_tag("cfRule"); @@ -2142,14 +2115,14 @@ impl ConditionalFormatDuplicate { /// # /// // Write a conditional format over a range. /// let conditional_format = ConditionalFormatFormula::new() -/// .set_value("=ISODD(B3)") +/// .set_rule("=ISODD(B3)") /// .set_format(format1); /// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; /// /// // Write another conditional format over the same range. /// let conditional_format = ConditionalFormatFormula::new() -/// .set_value("=ISEVEN(B3)") +/// .set_rule("=ISEVEN(B3)") /// .set_format(format2); /// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -2195,9 +2168,9 @@ impl ConditionalFormatFormula { } } - /// Set the value of a Formula conditional format rule. + /// Set the rule of a Formula conditional format. /// - /// Set the formula in a Formula style conditional format. + /// Set the formula rule in a Formula style conditional format. /// /// There are some caveats to be aware of when creating the formula: /// @@ -2263,14 +2236,14 @@ impl ConditionalFormatFormula { /// # /// // Write a conditional format over a range. /// let conditional_format = ConditionalFormatFormula::new() - /// .set_value("=ISODD(B3)") + /// .set_rule("=ISODD(B3)") /// .set_format(format1); /// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; /// /// // Write another conditional format over the same range. /// let conditional_format = ConditionalFormatFormula::new() - /// .set_value("=ISEVEN(B3)") + /// .set_rule("=ISEVEN(B3)") /// .set_format(format2); /// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -2287,8 +2260,8 @@ impl ConditionalFormatFormula { /// /// - pub fn set_value(mut self, value: impl Into) -> ConditionalFormatFormula { - self.formula = value.into(); + pub fn set_rule(mut self, rule: impl Into) -> ConditionalFormatFormula { + self.formula = rule.into(); self } @@ -2388,7 +2361,7 @@ impl ConditionalFormatFormula { /// # // This code is available in examples/doc_conditional_format_average.rs /// # /// # use rust_xlsxwriter::{ -/// # ConditionalFormatAverage, ConditionalFormatAverageCriteria, Format, Workbook, XlsxError, +/// # ConditionalFormatAverage, ConditionalFormatAverageRule, Format, Workbook, XlsxError, /// # }; /// # /// # fn main() -> Result<(), XlsxError> { @@ -2433,7 +2406,7 @@ impl ConditionalFormatFormula { /// /// // Write another conditional format over the same range. /// let conditional_format = ConditionalFormatAverage::new() -/// .set_criteria(ConditionalFormatAverageCriteria::BelowAverage) +/// .set_rule(ConditionalFormatAverageRule::BelowAverage) /// .set_format(format2); /// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -2456,7 +2429,7 @@ impl ConditionalFormatFormula { /// #[derive(Clone)] pub struct ConditionalFormatAverage { - criteria: ConditionalFormatAverageCriteria, + criteria: ConditionalFormatAverageRule, multi_range: String, stop_if_true: bool, has_x14_extensions: bool, @@ -2469,7 +2442,7 @@ impl ConditionalFormatAverage { #[allow(clippy::new_without_default)] pub fn new() -> ConditionalFormatAverage { ConditionalFormatAverage { - criteria: ConditionalFormatAverageCriteria::AboveAverage, + criteria: ConditionalFormatAverageRule::AboveAverage, multi_range: String::new(), stop_if_true: false, has_x14_extensions: false, @@ -2478,17 +2451,14 @@ impl ConditionalFormatAverage { } } - /// Set the criteria for the Average conditional format rule such as above - /// or below average or standard deviation ranges. + /// Set the rule for the Average conditional format such as above or below + /// average or standard deviation ranges. /// /// # Parameters /// - /// * `criteria` - A [`ConditionalFormatAverageCriteria`] enum value. + /// * `rule` - A [`ConditionalFormatAverageRule`] enum value. /// - pub fn set_criteria( - mut self, - criteria: ConditionalFormatAverageCriteria, - ) -> ConditionalFormatAverage { + pub fn set_rule(mut self, criteria: ConditionalFormatAverageRule) -> ConditionalFormatAverage { self.criteria = criteria; self } @@ -2545,47 +2515,47 @@ impl ConditionalFormatAverage { // Set the Average specific attributes. match self.criteria { - ConditionalFormatAverageCriteria::AboveAverage => { + ConditionalFormatAverageRule::AboveAverage => { // There are no additional attributes for above average. } - ConditionalFormatAverageCriteria::BelowAverage => { + ConditionalFormatAverageRule::BelowAverage => { attributes.push(("aboveAverage", "0".to_string())); } - ConditionalFormatAverageCriteria::EqualOrAboveAverage => { + ConditionalFormatAverageRule::EqualOrAboveAverage => { attributes.push(("equalAverage", "1".to_string())); } - ConditionalFormatAverageCriteria::EqualOrBelowAverage => { + ConditionalFormatAverageRule::EqualOrBelowAverage => { attributes.push(("aboveAverage", "0".to_string())); attributes.push(("equalAverage", "1".to_string())); } - ConditionalFormatAverageCriteria::OneStandardDeviationAbove => { + ConditionalFormatAverageRule::OneStandardDeviationAbove => { attributes.push(("stdDev", "1".to_string())); } - ConditionalFormatAverageCriteria::OneStandardDeviationBelow => { + ConditionalFormatAverageRule::OneStandardDeviationBelow => { attributes.push(("aboveAverage", "0".to_string())); attributes.push(("stdDev", "1".to_string())); } - ConditionalFormatAverageCriteria::TwoStandardDeviationsAbove => { + ConditionalFormatAverageRule::TwoStandardDeviationsAbove => { attributes.push(("stdDev", "2".to_string())); } - ConditionalFormatAverageCriteria::TwoStandardDeviationsBelow => { + ConditionalFormatAverageRule::TwoStandardDeviationsBelow => { attributes.push(("aboveAverage", "0".to_string())); attributes.push(("stdDev", "2".to_string())); } - ConditionalFormatAverageCriteria::ThreeStandardDeviationsAbove => { + ConditionalFormatAverageRule::ThreeStandardDeviationsAbove => { attributes.push(("stdDev", "3".to_string())); } - ConditionalFormatAverageCriteria::ThreeStandardDeviationsBelow => { + ConditionalFormatAverageRule::ThreeStandardDeviationsBelow => { attributes.push(("aboveAverage", "0".to_string())); attributes.push(("stdDev", "3".to_string())); @@ -2631,7 +2601,9 @@ impl ConditionalFormatAverage { /// ``` /// # // This code is available in examples/doc_conditional_format_top.rs /// # -/// # use rust_xlsxwriter::{ConditionalFormatTop, Format, Workbook, XlsxError}; +/// # use rust_xlsxwriter::{ +/// # ConditionalFormatTop, ConditionalFormatTopRule, Format, Workbook, XlsxError, +/// # }; /// # /// # fn main() -> Result<(), XlsxError> { /// # // Create a new Excel file object. @@ -2670,16 +2642,16 @@ impl ConditionalFormatAverage { /// # /// // Write a conditional format over a range. /// let conditional_format = ConditionalFormatTop::new() -/// .set_value(10) +/// .set_rule(ConditionalFormatTopRule::Top(10)) /// .set_format(format1); /// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; /// /// // Invert the Top conditional format to show Bottom values. /// let conditional_format = ConditionalFormatTop::new() -/// .invert() -/// .set_value(10) +/// .set_rule(ConditionalFormatTopRule::Bottom(10)) /// .set_format(format2); +/// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; /// /// # // Save the file. @@ -2700,9 +2672,8 @@ impl ConditionalFormatAverage { /// #[derive(Clone)] pub struct ConditionalFormatTop { - value: u16, - is_inverted: bool, - is_percent: bool, + rule: ConditionalFormatTopRule, + multi_range: String, stop_if_true: bool, has_x14_extensions: bool, @@ -2715,9 +2686,8 @@ impl ConditionalFormatTop { #[allow(clippy::new_without_default)] pub fn new() -> ConditionalFormatTop { ConditionalFormatTop { - value: 10, - is_inverted: false, - is_percent: false, + rule: ConditionalFormatTopRule::Top(10), + multi_range: String::new(), stop_if_true: false, has_x14_extensions: false, @@ -2730,27 +2700,8 @@ impl ConditionalFormatTop { /// /// See the example above. /// - pub fn set_value(mut self, value: u16) -> ConditionalFormatTop { - self.value = value; - self - } - - /// Invert the functionality of the conditional format to the get Bottom values - /// instead of the Top values. - /// - /// See the example above. - /// - pub fn invert(mut self) -> ConditionalFormatTop { - self.is_inverted = true; - self - } - - /// Show the top/bottom percentage instead of rank. - /// - /// See the example above. - /// - pub fn set_percent(mut self) -> ConditionalFormatTop { - self.is_percent = true; + pub fn set_rule(mut self, rule: ConditionalFormatTopRule) -> ConditionalFormatTop { + self.rule = rule; self } @@ -2775,10 +2726,23 @@ impl ConditionalFormatTop { // Validate the conditional format. pub(crate) fn validate(&self) -> Result<(), XlsxError> { - if !(1..=1000).contains(&self.value) { - return Err(XlsxError::ConditionalFormatError( - "value must be in the Excel range 1..1000".to_string(), - )); + match &self.rule { + ConditionalFormatTopRule::Top(value) | ConditionalFormatTopRule::Bottom(value) => { + if !(1..=1000).contains(value) { + return Err(XlsxError::ConditionalFormatError(format!( + "ConditionalFormatTop value '{value}' must be in Excel range: 1..1000." + ))); + } + } + + ConditionalFormatTopRule::TopPercent(value) + | ConditionalFormatTopRule::BottomPercent(value) => { + if !(1..=100).contains(value) { + return Err(XlsxError::ConditionalFormatError(format!( + "ConditionalFormatTop percent value '{value}' must be in Excel range: 1..100." + ))); + } + } } Ok(()) @@ -2808,15 +2772,32 @@ impl ConditionalFormatTop { attributes.push(("stopIfTrue", "1".to_string())); } - if self.is_percent { - attributes.push(("percent", "1".to_string())); + // Add attribute for percentage rules. + match self.rule { + ConditionalFormatTopRule::TopPercent(_) + | ConditionalFormatTopRule::BottomPercent(_) => { + attributes.push(("percent", "1".to_string())); + } + _ => (), } - if self.is_inverted { - attributes.push(("bottom", "1".to_string())); + // Add attribute for bottom types. + match self.rule { + ConditionalFormatTopRule::Bottom(_) | ConditionalFormatTopRule::BottomPercent(_) => { + attributes.push(("bottom", "1".to_string())); + } + _ => (), } - attributes.push(("rank", self.value.to_string())); + // Add the rank value. + match &self.rule { + ConditionalFormatTopRule::Top(value) + | ConditionalFormatTopRule::TopPercent(value) + | ConditionalFormatTopRule::Bottom(value) + | ConditionalFormatTopRule::BottomPercent(value) => { + attributes.push(("rank", value.to_string())); + } + } // Write the rule. writer.xml_empty_tag("cfRule", &attributes); @@ -2854,7 +2835,7 @@ impl ConditionalFormatTop { /// # // This code is available in examples/doc_conditional_format_text.rs /// # /// # use rust_xlsxwriter::{ -/// # ConditionalFormatText, ConditionalFormatTextCriteria, Format, Workbook, XlsxError, +/// # ConditionalFormatText, ConditionalFormatTextRule, Format, Workbook, XlsxError, /// # }; /// # /// # fn main() -> Result<(), XlsxError> { @@ -2897,36 +2878,32 @@ impl ConditionalFormatTop { /// # /// // Write a text "containing" conditional format over a range. /// let conditional_format = ConditionalFormatText::new() -/// .set_criteria(ConditionalFormatTextCriteria::Contains) -/// .set_value("rust") +/// .set_rule(ConditionalFormatTextRule::Contains("rust".to_string())) /// .set_format(&format1); /// /// worksheet.add_conditional_format(0, 0, 12, 0, &conditional_format)?; /// /// // Write a text "not containing" conditional format over the same range. /// let conditional_format = ConditionalFormatText::new() -/// .set_criteria(ConditionalFormatTextCriteria::DoesNotContain) -/// .set_value("rust") +/// .set_rule(ConditionalFormatTextRule::DoesNotContain("rust".to_string())) /// .set_format(&format2); /// /// worksheet.add_conditional_format(0, 0, 12, 0, &conditional_format)?; /// /// // Write a text "begins with" conditional format over a range. /// let conditional_format = ConditionalFormatText::new() -/// .set_criteria(ConditionalFormatTextCriteria::BeginsWith) -/// .set_value("t") +/// .set_rule(ConditionalFormatTextRule::BeginsWith("t".to_string())) /// .set_format(&format1); /// /// worksheet.add_conditional_format(0, 2, 12, 2, &conditional_format)?; /// /// // Write a text "ends with" conditional format over the same range. /// let conditional_format = ConditionalFormatText::new() -/// .set_criteria(ConditionalFormatTextCriteria::EndsWith) -/// .set_value("t") +/// .set_rule(ConditionalFormatTextRule::EndsWith("t".to_string())) /// .set_format(&format2); /// /// worksheet.add_conditional_format(0, 2, 12, 2, &conditional_format)?; -/// # +/// /// # // Save the file. /// # workbook.save("conditional_format.xlsx")?; /// # @@ -2944,8 +2921,8 @@ impl ConditionalFormatTop { /// #[derive(Clone)] pub struct ConditionalFormatText { - value: String, - criteria: ConditionalFormatTextCriteria, + rule: Option, + multi_range: String, stop_if_true: bool, has_x14_extensions: bool, @@ -2958,8 +2935,8 @@ impl ConditionalFormatText { #[allow(clippy::new_without_default)] pub fn new() -> ConditionalFormatText { ConditionalFormatText { - value: String::new(), - criteria: ConditionalFormatTextCriteria::Contains, + rule: None, + multi_range: String::new(), stop_if_true: false, has_x14_extensions: false, @@ -2968,32 +2945,17 @@ impl ConditionalFormatText { } } - /// Set the value of the Text conditional format rule. - /// - /// # Parameters - /// - /// * `value` - A string like value. - /// - /// Newer versions of Excel support support using a cell reference for the - /// value but that isn't currently supported by `rust_xlsxwriter`. - /// - pub fn set_value(mut self, value: impl Into) -> ConditionalFormatText { - self.value = value.into(); - self - } - - /// Set the criteria for the Text conditional format rule such "contains" or + /// Set the rule for the Text conditional format rule such "contains" or /// "starts with". /// + /// See the example above. + /// /// # Parameters /// - /// * `criteria` - A [`ConditionalFormatTextCriteria`] enum value. + /// * `rule` - A [`ConditionalFormatTextRule`] enum value. /// - pub fn set_criteria( - mut self, - criteria: ConditionalFormatTextCriteria, - ) -> ConditionalFormatText { - self.criteria = criteria; + pub fn set_rule(mut self, rule: ConditionalFormatTextRule) -> ConditionalFormatText { + self.rule = Some(rule); self } @@ -3018,9 +2980,9 @@ impl ConditionalFormatText { // Validate the conditional format. pub(crate) fn validate(&self) -> Result<(), XlsxError> { - if self.value.is_empty() { + if self.rule.is_none() { return Err(XlsxError::ConditionalFormatError( - "Text conditional format string cannot be empty".to_string(), + "Text conditional format string must have a rule".to_string(), )); } @@ -3035,27 +2997,31 @@ impl ConditionalFormatText { range: &str, _guid: &str, ) -> String { + let Some(rule) = &self.rule else { + return String::new(); + }; + let mut writer = XMLWriter::new(); let mut attributes = vec![]; - let text = self.value.clone(); let anchor = &range_to_anchor(range); - let length = text.len(); // Set the rule attributes based on the criteria. - let formula = match self.criteria { - ConditionalFormatTextCriteria::Contains => { + let formula = match rule { + ConditionalFormatTextRule::Contains(text) => { attributes.push(("type", "containsText".to_string())); format!(r#"NOT(ISERROR(SEARCH("{text}",{anchor})))"#) } - ConditionalFormatTextCriteria::DoesNotContain => { + ConditionalFormatTextRule::DoesNotContain(text) => { attributes.push(("type", "notContainsText".to_string())); format!(r#"ISERROR(SEARCH("{text}",{anchor}))"#) } - ConditionalFormatTextCriteria::BeginsWith => { + ConditionalFormatTextRule::BeginsWith(text) => { + let length = text.len(); attributes.push(("type", "beginsWith".to_string())); format!(r#"LEFT({anchor},{length})="{text}""#) } - ConditionalFormatTextCriteria::EndsWith => { + ConditionalFormatTextRule::EndsWith(text) => { + let length = text.len(); attributes.push(("type", "endsWith".to_string())); format!(r#"RIGHT({anchor},{length})="{text}""#) } @@ -3075,8 +3041,16 @@ impl ConditionalFormatText { } // Add the attributes. - attributes.push(("operator", self.criteria.to_string())); - attributes.push(("text", text)); + attributes.push(("operator", rule.to_string())); + + match rule { + ConditionalFormatTextRule::Contains(text) + | ConditionalFormatTextRule::DoesNotContain(text) + | ConditionalFormatTextRule::BeginsWith(text) + | ConditionalFormatTextRule::EndsWith(text) => { + attributes.push(("text", text.clone())); + } + }; writer.xml_start_tag("cfRule", &attributes); writer.xml_data_element_only("formula", &formula); @@ -3121,7 +3095,7 @@ impl ConditionalFormatText { /// # // This code is available in examples/doc_conditional_format_date.rs /// # /// # use rust_xlsxwriter::{ -/// # ConditionalFormatDate, ConditionalFormatDateCriteria, ExcelDateTime, Format, Workbook, +/// # ConditionalFormatDate, ConditionalFormatDateRule, ExcelDateTime, Format, Workbook, /// # XlsxError, /// # }; /// # @@ -3178,19 +3152,19 @@ impl ConditionalFormatText { /// # /// // Write conditional format over the same range. /// let conditional_format = ConditionalFormatDate::new() -/// .set_criteria(ConditionalFormatDateCriteria::LastMonth) +/// .set_rule(ConditionalFormatDateRule::LastMonth) /// .set_format(format1); /// /// worksheet.add_conditional_format(0, 0, 10, 0, &conditional_format)?; /// /// let conditional_format = ConditionalFormatDate::new() -/// .set_criteria(ConditionalFormatDateCriteria::ThisMonth) +/// .set_rule(ConditionalFormatDateRule::ThisMonth) /// .set_format(format2); /// /// worksheet.add_conditional_format(0, 0, 10, 0, &conditional_format)?; /// /// let conditional_format = ConditionalFormatDate::new() -/// .set_criteria(ConditionalFormatDateCriteria::NextMonth) +/// .set_rule(ConditionalFormatDateRule::NextMonth) /// .set_format(format3); /// /// worksheet.add_conditional_format(0, 0, 10, 0, &conditional_format)?; @@ -3214,7 +3188,8 @@ impl ConditionalFormatText { /// #[derive(Clone)] pub struct ConditionalFormatDate { - criteria: ConditionalFormatDateCriteria, + rule: ConditionalFormatDateRule, + multi_range: String, stop_if_true: bool, has_x14_extensions: bool, @@ -3227,7 +3202,8 @@ impl ConditionalFormatDate { #[allow(clippy::new_without_default)] pub fn new() -> ConditionalFormatDate { ConditionalFormatDate { - criteria: ConditionalFormatDateCriteria::Yesterday, + rule: ConditionalFormatDateRule::Yesterday, + multi_range: String::new(), stop_if_true: false, has_x14_extensions: false, @@ -3236,18 +3212,15 @@ impl ConditionalFormatDate { } } - /// Set the criteria for the Dates Occurring conditional format rule such + /// Set the rule for the Dates Occurring conditional format rule such /// "Today" or "Last Week". /// /// # Parameters /// - /// * `criteria` - A [`ConditionalFormatDateCriteria`] enum value. + /// * `rule` - A [`ConditionalFormatDateRule`] enum value. /// - pub fn set_criteria( - mut self, - criteria: ConditionalFormatDateCriteria, - ) -> ConditionalFormatDate { - self.criteria = criteria; + pub fn set_rule(mut self, criteria: ConditionalFormatDateRule) -> ConditionalFormatDate { + self.rule = criteria; self } @@ -3290,41 +3263,41 @@ impl ConditionalFormatDate { let anchor = &range_to_anchor(range); // Set the rule attributes based on the criteria. - let formula = match self.criteria { - ConditionalFormatDateCriteria::Yesterday => { + let formula = match self.rule { + ConditionalFormatDateRule::Yesterday => { format!("FLOOR({anchor},1)=TODAY()-1") } - ConditionalFormatDateCriteria::Today => { + ConditionalFormatDateRule::Today => { format!("FLOOR({anchor},1)=TODAY()") } - ConditionalFormatDateCriteria::Tomorrow => { + ConditionalFormatDateRule::Tomorrow => { format!("FLOOR({anchor},1)=TODAY()+1") } - ConditionalFormatDateCriteria::Last7Days => { + ConditionalFormatDateRule::Last7Days => { format!("AND(TODAY()-FLOOR({anchor},1)<=6,FLOOR({anchor},1)<=TODAY())") } - ConditionalFormatDateCriteria::LastWeek => { + ConditionalFormatDateRule::LastWeek => { format!("AND(TODAY()-ROUNDDOWN({anchor},0)>=(WEEKDAY(TODAY())),TODAY()-ROUNDDOWN({anchor},0)<(WEEKDAY(TODAY())+7))") } - ConditionalFormatDateCriteria::ThisWeek => { + ConditionalFormatDateRule::ThisWeek => { format!("AND(TODAY()-ROUNDDOWN({anchor},0)<=WEEKDAY(TODAY())-1,ROUNDDOWN({anchor},0)-TODAY()<=7-WEEKDAY(TODAY()))") } - ConditionalFormatDateCriteria::NextWeek => { + ConditionalFormatDateRule::NextWeek => { format!("AND(ROUNDDOWN({anchor},0)-TODAY()>(7-WEEKDAY(TODAY())),ROUNDDOWN({anchor},0)-TODAY()<(15-WEEKDAY(TODAY())))") } - ConditionalFormatDateCriteria::LastMonth => { + ConditionalFormatDateRule::LastMonth => { format!("AND(MONTH({anchor})=MONTH(TODAY())-1,OR(YEAR({anchor})=YEAR(TODAY()),AND(MONTH({anchor})=1,YEAR({anchor})=YEAR(TODAY())-1)))") } - ConditionalFormatDateCriteria::ThisMonth => { + ConditionalFormatDateRule::ThisMonth => { format!("AND(MONTH({anchor})=MONTH(TODAY()),YEAR({anchor})=YEAR(TODAY()))") } - ConditionalFormatDateCriteria::NextMonth => { + ConditionalFormatDateRule::NextMonth => { format!("AND(MONTH({anchor})=MONTH(TODAY())+1,OR(YEAR({anchor})=YEAR(TODAY()),AND(MONTH({anchor})=12,YEAR({anchor})=YEAR(TODAY())+1)))") } }; @@ -3343,7 +3316,7 @@ impl ConditionalFormatDate { } // Add the attributes. - attributes.push(("timePeriod", self.criteria.to_string())); + attributes.push(("timePeriod", self.rule.to_string())); writer.xml_start_tag("cfRule", &attributes); writer.xml_data_element_only("formula", &formula); @@ -6691,6 +6664,7 @@ impl ConditionalFormatValue { } } +// From/Into traits for ConditionalFormatValue. macro_rules! conditional_format_value_from_string { ($($t:ty)*) => ($( impl From<$t> for ConditionalFormatValue { @@ -6759,65 +6733,126 @@ impl From<&NaiveTime> for ConditionalFormatValue { } } +/// Trait to map rust types into an [`ConditionalFormatValue`] value. +/// +/// The `IntoConditionalFormatValue` trait is used to map Rust types like +/// strings, numbers, dates, times and formulas into a generic type that can be +/// used to replicate Excel data types used in Conditional Formats. +/// +/// See [`ConditionalFormatValue`] for more information. +/// +pub trait IntoConditionalFormatValue { + /// Function to turn types into a [`ConditionalFormatValue`] enum value. + fn new_value(self) -> ConditionalFormatValue; +} + +impl IntoConditionalFormatValue for ConditionalFormatValue { + fn new_value(self) -> ConditionalFormatValue { + self.clone() + } +} + +macro_rules! conditional_format_value_from_type { + ($($t:ty)*) => ($( + impl IntoConditionalFormatValue for $t { + fn new_value(self) -> ConditionalFormatValue { + self.into() + } + } + )*) +} +conditional_format_value_from_type!( + &str &String String Cow<'_, str> + u8 i8 u16 i16 u32 i32 f32 f64 + Formula + ExcelDateTime &ExcelDateTime +); + +#[cfg(feature = "chrono")] +conditional_format_value_from_type!(&NaiveDate & NaiveDateTime & NaiveTime); + // ----------------------------------------------------------------------- -// ConditionalFormatCellCriteria +// ConditionalFormatCellRule // ----------------------------------------------------------------------- -/// The `ConditionalFormatCellCriteria` enum defines the conditional format -/// criteria for [`ConditionalFormatCell`]. +/// The `ConditionalFormatCellRule` enum defines the conditional format rule for +/// [`ConditionalFormatCell`]. /// /// #[derive(Clone, Copy, PartialEq, Eq)] -pub enum ConditionalFormatCellCriteria { +pub enum ConditionalFormatCellRule { /// Show the conditional format for cells that are equal to the target value. - EqualTo, + EqualTo(T), /// Show the conditional format for cells that are not equal to the target value. - NotEqualTo, + NotEqualTo(T), /// Show the conditional format for cells that are greater than the target value. - GreaterThan, + GreaterThan(T), /// Show the conditional format for cells that are greater than or equal to the target value. - GreaterThanOrEqualTo, + GreaterThanOrEqualTo(T), /// Show the conditional format for cells that are less than the target value. - LessThan, + LessThan(T), /// Show the conditional format for cells that are less than or equal to the target value. - LessThanOrEqualTo, + LessThanOrEqualTo(T), /// Show the conditional format for cells that are between the target values. - Between, + Between(T, T), /// Show the conditional format for cells that are not between the target values. - NotBetween, + NotBetween(T, T), } -impl fmt::Display for ConditionalFormatCellCriteria { +impl fmt::Display for ConditionalFormatCellRule { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - ConditionalFormatCellCriteria::EqualTo => write!(f, "equal"), - ConditionalFormatCellCriteria::Between => write!(f, "between"), - ConditionalFormatCellCriteria::LessThan => write!(f, "lessThan"), - ConditionalFormatCellCriteria::NotEqualTo => write!(f, "notEqual"), - ConditionalFormatCellCriteria::NotBetween => write!(f, "notBetween"), - ConditionalFormatCellCriteria::GreaterThan => write!(f, "greaterThan"), - ConditionalFormatCellCriteria::LessThanOrEqualTo => write!(f, "lessThanOrEqual"), - ConditionalFormatCellCriteria::GreaterThanOrEqualTo => write!(f, "greaterThanOrEqual"), + ConditionalFormatCellRule::EqualTo(_) => write!(f, "equal"), + ConditionalFormatCellRule::Between(_, _) => write!(f, "between"), + ConditionalFormatCellRule::LessThan(_) => write!(f, "lessThan"), + ConditionalFormatCellRule::NotEqualTo(_) => write!(f, "notEqual"), + ConditionalFormatCellRule::NotBetween(_, _) => write!(f, "notBetween"), + ConditionalFormatCellRule::GreaterThan(_) => write!(f, "greaterThan"), + ConditionalFormatCellRule::LessThanOrEqualTo(_) => write!(f, "lessThanOrEqual"), + ConditionalFormatCellRule::GreaterThanOrEqualTo(_) => write!(f, "greaterThanOrEqual"), } } } // ----------------------------------------------------------------------- -// ConditionalFormatAverageCriteria +// ConditionalFormatTopRule +// ----------------------------------------------------------------------- + +/// The `ConditionalFormatTopRule` enum defines the conditional format rule for +/// [`ConditionalFormatCell`]. +/// +/// +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum ConditionalFormatTopRule { + /// Show the conditional format for cells that are in the top X. + Top(u16), + + /// Show the conditional format for cells that are in the bottom X. + Bottom(u16), + + /// Show the conditional format for cells that are in the top X%. + TopPercent(u16), + + /// Show the conditional format for cells that are in the bottom X%. + BottomPercent(u16), +} + +// ----------------------------------------------------------------------- +// ConditionalFormatAverageRule // ----------------------------------------------------------------------- -/// The `ConditionalFormatAverageCriteria` enum defines the conditional format +/// The `ConditionalFormatAverageRule` enum defines the conditional format /// criteria for [`ConditionalFormatCell`]. /// #[derive(Clone, Copy, PartialEq, Eq)] -pub enum ConditionalFormatAverageCriteria { +pub enum ConditionalFormatAverageRule { /// Show the conditional format for cells above the average for the range. /// This is the default. AboveAverage, @@ -6859,48 +6894,48 @@ pub enum ConditionalFormatAverageCriteria { } // ----------------------------------------------------------------------- -// ConditionalFormatTextCriteria +// ConditionalFormatTextRule // ----------------------------------------------------------------------- -/// The `ConditionalFormatTextCriteria` enum defines the conditional format +/// The `ConditionalFormatTextRule` enum defines the conditional format /// criteria for [`ConditionalFormatText`]. /// /// -#[derive(Clone, Copy, PartialEq, Eq)] -pub enum ConditionalFormatTextCriteria { +#[derive(Clone, PartialEq, Eq)] +pub enum ConditionalFormatTextRule { /// Show the conditional format for text that contains to the target string. - Contains, + Contains(String), /// Show the conditional format for text that do not contain to the target string. - DoesNotContain, + DoesNotContain(String), /// Show the conditional format for text that begins with the target string. - BeginsWith, + BeginsWith(String), /// Show the conditional format for text that ends with the target string. - EndsWith, + EndsWith(String), } -impl fmt::Display for ConditionalFormatTextCriteria { +impl fmt::Display for ConditionalFormatTextRule { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - ConditionalFormatTextCriteria::Contains => write!(f, "containsText"), - ConditionalFormatTextCriteria::DoesNotContain => write!(f, "notContains"), - ConditionalFormatTextCriteria::BeginsWith => write!(f, "beginsWith"), - ConditionalFormatTextCriteria::EndsWith => write!(f, "endsWith"), + ConditionalFormatTextRule::Contains(_) => write!(f, "containsText"), + ConditionalFormatTextRule::DoesNotContain(_) => write!(f, "notContains"), + ConditionalFormatTextRule::BeginsWith(_) => write!(f, "beginsWith"), + ConditionalFormatTextRule::EndsWith(_) => write!(f, "endsWith"), } } } // ----------------------------------------------------------------------- -// ConditionalFormatDateCriteria +// ConditionalFormatDateRule // ----------------------------------------------------------------------- -/// The `ConditionalFormatDateCriteria` enum defines the conditional format +/// The `ConditionalFormatDateRule` enum defines the conditional format /// criteria for [`ConditionalFormatDate`]. /// #[derive(Clone, Copy, PartialEq, Eq)] -pub enum ConditionalFormatDateCriteria { +pub enum ConditionalFormatDateRule { /// Show the conditional format for dates occurring yesterday. This is the /// default. Yesterday, @@ -6942,19 +6977,19 @@ pub enum ConditionalFormatDateCriteria { NextMonth, } -impl fmt::Display for ConditionalFormatDateCriteria { +impl fmt::Display for ConditionalFormatDateRule { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - ConditionalFormatDateCriteria::Today => write!(f, "today"), - ConditionalFormatDateCriteria::Tomorrow => write!(f, "tomorrow"), - ConditionalFormatDateCriteria::LastWeek => write!(f, "lastWeek"), - ConditionalFormatDateCriteria::NextWeek => write!(f, "nextWeek"), - ConditionalFormatDateCriteria::ThisWeek => write!(f, "thisWeek"), - ConditionalFormatDateCriteria::Last7Days => write!(f, "last7Days"), - ConditionalFormatDateCriteria::LastMonth => write!(f, "lastMonth"), - ConditionalFormatDateCriteria::NextMonth => write!(f, "nextMonth"), - ConditionalFormatDateCriteria::ThisMonth => write!(f, "thisMonth"), - ConditionalFormatDateCriteria::Yesterday => write!(f, "yesterday"), + ConditionalFormatDateRule::Today => write!(f, "today"), + ConditionalFormatDateRule::Tomorrow => write!(f, "tomorrow"), + ConditionalFormatDateRule::LastWeek => write!(f, "lastWeek"), + ConditionalFormatDateRule::NextWeek => write!(f, "nextWeek"), + ConditionalFormatDateRule::ThisWeek => write!(f, "thisWeek"), + ConditionalFormatDateRule::Last7Days => write!(f, "last7Days"), + ConditionalFormatDateRule::LastMonth => write!(f, "lastMonth"), + ConditionalFormatDateRule::NextMonth => write!(f, "nextMonth"), + ConditionalFormatDateRule::ThisMonth => write!(f, "thisMonth"), + ConditionalFormatDateRule::Yesterday => write!(f, "yesterday"), } } } @@ -7431,14 +7466,13 @@ macro_rules! generate_conditional_common_methods { pub(crate) fn has_x14_only(&self) -> bool { self.has_x14_only } - } )*) } generate_conditional_common_methods!( ConditionalFormatAverage ConditionalFormatBlank - ConditionalFormatCell + ConditionalFormatDate ConditionalFormatDuplicate ConditionalFormatError @@ -7451,6 +7485,76 @@ generate_conditional_common_methods!( ConditionalFormatIconSet ); +impl ConditionalFormatCell { + /// Set an additional multi-cell range for the conditional format. + /// + /// The `set_multi_range()` method is used to extend a conditional + /// format over non-contiguous ranges like `"B3:D6 I3:K6 B9:D12 + /// I9:K12"`. + /// + /// See [Selecting a non-contiguous + /// range](crate::conditional_format#selecting-a-non-contiguous-range) + /// for more information. + /// + /// # Parameters + /// + /// * `range` - A string like type representing an Excel range. + /// + /// Note, you can use an Excel range like `"$B$3:$D$6,$I$3:$K$6"` or + /// omit the `$` anchors and replace the commas with spaces to have a + /// clearer range like `"B3:D6 I3:K6"`. The documentation and examples + /// use the latter format for clarity but it you are copying and + /// pasting from Excel you can use the first format. + /// + /// Note, if the range is invalid then Excel will omit it silently. + /// + pub fn set_multi_range(mut self, range: impl Into) -> ConditionalFormatCell { + self.multi_range = range.into().replace('$', "").replace(',', " "); + self + } + + /// Set the "Stop if True" option for the conditional format rule. + /// + /// The `set_stop_if_true()` method can be used to set the “Stop if true” + /// feature of a conditional formatting rule when more than one rule is + /// applied to a cell or a range of cells. When this parameter is set then + /// subsequent rules are not evaluated if the current rule is true. + /// + /// # Parameters + /// + /// * `enable` - Turn the property on/off. It is off by default. + /// + pub fn set_stop_if_true(mut self, enable: bool) -> ConditionalFormatCell { + self.stop_if_true = enable; + self + } + + // Get the index of the format object in the conditional format. + pub(crate) fn format_index(&self) -> Option { + self.format.as_ref().map(|format| format.dxf_index) + } + + // Get a reference to the format object in the conditional format. + pub(crate) fn format_as_mut(&mut self) -> Option<&mut Format> { + self.format.as_mut() + } + + // Get the multi-cell range for the conditional format, if present. + pub(crate) fn multi_range(&self) -> String { + self.multi_range.clone() + } + + /// Check if the conditional format uses Excel 2010+ extensions. + pub(crate) fn has_x14_extensions(&self) -> bool { + self.has_x14_extensions + } + + /// Check if the conditional format uses Excel 2010+ extensions only. + pub(crate) fn has_x14_only(&self) -> bool { + self.has_x14_only + } +} + // ----------------------------------------------------------------------- // Common methods. // ----------------------------------------------------------------------- diff --git a/src/conditional_format/tests.rs b/src/conditional_format/tests.rs index 17a43a6b..2745a9c9 100644 --- a/src/conditional_format/tests.rs +++ b/src/conditional_format/tests.rs @@ -12,24 +12,25 @@ mod conditional_format_tests { use crate::ConditionalFormat2ColorScale; use crate::ConditionalFormat3ColorScale; use crate::ConditionalFormatAverage; - use crate::ConditionalFormatAverageCriteria; + use crate::ConditionalFormatAverageRule; use crate::ConditionalFormatBlank; use crate::ConditionalFormatCell; - use crate::ConditionalFormatCellCriteria; + use crate::ConditionalFormatCellRule; use crate::ConditionalFormatCustomIcon; use crate::ConditionalFormatDataBar; use crate::ConditionalFormatDataBarAxisPosition; use crate::ConditionalFormatDataBarDirection; use crate::ConditionalFormatDate; - use crate::ConditionalFormatDateCriteria; + use crate::ConditionalFormatDateRule; use crate::ConditionalFormatDuplicate; use crate::ConditionalFormatError; use crate::ConditionalFormatFormula; use crate::ConditionalFormatIconSet; use crate::ConditionalFormatIconType; use crate::ConditionalFormatText; - use crate::ConditionalFormatTextCriteria; + use crate::ConditionalFormatTextRule; use crate::ConditionalFormatTop; + use crate::ConditionalFormatTopRule; use crate::ConditionalFormatType; use crate::ExcelDateTime; use crate::Formula; @@ -38,9 +39,8 @@ mod conditional_format_tests { #[test] fn quoted_string_01() { - let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::EqualTo) - .set_value(5); + let conditional_format = + ConditionalFormatCell::new().set_rule(ConditionalFormatCellRule::EqualTo(5)); let got = conditional_format.rule(None, 1, "", ""); let expected = @@ -51,9 +51,8 @@ mod conditional_format_tests { #[test] fn quoted_string_02() { - let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::EqualTo) - .set_value("Foo"); + let conditional_format = + ConditionalFormatCell::new().set_rule(ConditionalFormatCellRule::EqualTo("Foo")); let got = conditional_format.rule(None, 1, "", ""); let expected = r#""Foo""#; @@ -63,9 +62,8 @@ mod conditional_format_tests { #[test] fn quoted_string_03() { - let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::EqualTo) - .set_value("\"Foo\""); + let conditional_format = + ConditionalFormatCell::new().set_rule(ConditionalFormatCellRule::EqualTo("\"Foo\"")); let got = conditional_format.rule(None, 1, "", ""); let expected = r#""Foo""#; @@ -75,9 +73,8 @@ mod conditional_format_tests { #[test] fn quoted_string_04() { - let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::EqualTo) - .set_value("Foo \" Bar"); + let conditional_format = + ConditionalFormatCell::new().set_rule(ConditionalFormatCellRule::EqualTo("Foo \" Bar")); let got = conditional_format.rule(None, 1, "", ""); let expected = r#""Foo "" Bar""#; @@ -89,37 +86,20 @@ mod conditional_format_tests { fn validation_checks() { // Check validations for various conditional formats. - // Cell format must have a non-None type. + // Cell format must have a rule. let conditional_format = ConditionalFormatCell::new(); let result = conditional_format.validate(); assert!(matches!(result, Err(XlsxError::ConditionalFormatError(_)))); - // Cell format must have a value. - let conditional_format = - ConditionalFormatCell::new().set_criteria(ConditionalFormatCellCriteria::EqualTo); - let result = conditional_format.validate(); - assert!(matches!(result, Err(XlsxError::ConditionalFormatError(_)))); - - // Cell between format must have a min value. - let conditional_format = - ConditionalFormatCell::new().set_criteria(ConditionalFormatCellCriteria::Between); - let result = conditional_format.validate(); - assert!(matches!(result, Err(XlsxError::ConditionalFormatError(_)))); - - // Cell between format must have a max value. - let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::Between) - .set_minimum(1); - let result = conditional_format.validate(); - assert!(matches!(result, Err(XlsxError::ConditionalFormatError(_)))); - // Top value must be in the Excel range 1..1000. - let conditional_format = ConditionalFormatTop::new().set_value(0); + let conditional_format = + ConditionalFormatTop::new().set_rule(ConditionalFormatTopRule::Top(0)); let result = conditional_format.validate(); assert!(matches!(result, Err(XlsxError::ConditionalFormatError(_)))); // Top value must be in the Excel range 1..1000. - let conditional_format = ConditionalFormatTop::new().set_value(1001); + let conditional_format = + ConditionalFormatTop::new().set_rule(ConditionalFormatTopRule::Top(1001)); let result = conditional_format.validate(); assert!(matches!(result, Err(XlsxError::ConditionalFormatError(_)))); @@ -188,9 +168,8 @@ mod conditional_format_tests { worksheet.write(2, 0, 30)?; worksheet.write(3, 0, 40)?; - let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(5); + let conditional_format = + ConditionalFormatCell::new().set_rule(ConditionalFormatCellRule::GreaterThan(5)); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; @@ -258,8 +237,7 @@ mod conditional_format_tests { worksheet.write(0, 1, 5)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(Formula::new("$B$1")); + .set_rule(ConditionalFormatCellRule::GreaterThan(Formula::new("$B$1"))); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; @@ -327,17 +305,13 @@ mod conditional_format_tests { worksheet.write(2, 0, 30)?; worksheet.write(3, 0, 40)?; - let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::Between) - .set_minimum(20) - .set_maximum(30); + let conditional_format = + ConditionalFormatCell::new().set_rule(ConditionalFormatCellRule::Between(20, 30)); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::NotBetween) - .set_minimum(20) - .set_maximum(30) + .set_rule(ConditionalFormatCellRule::NotBetween(20, 30)) .set_multi_range("A1:A4"); // Additional test for multi_range. worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; @@ -478,48 +452,48 @@ mod conditional_format_tests { worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; - let conditional_format = ConditionalFormatAverage::new() - .set_criteria(ConditionalFormatAverageCriteria::BelowAverage); + let conditional_format = + ConditionalFormatAverage::new().set_rule(ConditionalFormatAverageRule::BelowAverage); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatAverage::new() - .set_criteria(ConditionalFormatAverageCriteria::EqualOrAboveAverage); + .set_rule(ConditionalFormatAverageRule::EqualOrAboveAverage); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatAverage::new() - .set_criteria(ConditionalFormatAverageCriteria::EqualOrBelowAverage); + .set_rule(ConditionalFormatAverageRule::EqualOrBelowAverage); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatAverage::new() - .set_criteria(ConditionalFormatAverageCriteria::OneStandardDeviationAbove); + .set_rule(ConditionalFormatAverageRule::OneStandardDeviationAbove); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatAverage::new() - .set_criteria(ConditionalFormatAverageCriteria::OneStandardDeviationBelow); + .set_rule(ConditionalFormatAverageRule::OneStandardDeviationBelow); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatAverage::new() - .set_criteria(ConditionalFormatAverageCriteria::TwoStandardDeviationsAbove); + .set_rule(ConditionalFormatAverageRule::TwoStandardDeviationsAbove); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatAverage::new() - .set_criteria(ConditionalFormatAverageCriteria::TwoStandardDeviationsBelow); + .set_rule(ConditionalFormatAverageRule::TwoStandardDeviationsBelow); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatAverage::new() - .set_criteria(ConditionalFormatAverageCriteria::ThreeStandardDeviationsAbove); + .set_rule(ConditionalFormatAverageRule::ThreeStandardDeviationsAbove); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatAverage::new() - .set_criteria(ConditionalFormatAverageCriteria::ThreeStandardDeviationsBelow); + .set_rule(ConditionalFormatAverageRule::ThreeStandardDeviationsBelow); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; @@ -592,18 +566,22 @@ mod conditional_format_tests { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatTop::new(); + worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; - let conditional_format = ConditionalFormatTop::new().invert().set_value(16); + let conditional_format = + ConditionalFormatTop::new().set_rule(ConditionalFormatTopRule::Bottom(16)); + worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; - let conditional_format = ConditionalFormatTop::new().set_value(17).set_percent(); + let conditional_format = + ConditionalFormatTop::new().set_rule(ConditionalFormatTopRule::TopPercent(17)); + worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; - let conditional_format = ConditionalFormatTop::new() - .invert() - .set_value(18) - .set_percent(); + let conditional_format = + ConditionalFormatTop::new().set_rule(ConditionalFormatTopRule::BottomPercent(18)); + worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; worksheet.assemble_xml_file(); @@ -669,26 +647,22 @@ mod conditional_format_tests { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::Contains) - .set_value("foo"); + .set_rule(ConditionalFormatTextRule::Contains("foo".to_string())); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::DoesNotContain) - .set_value("foo"); + .set_rule(ConditionalFormatTextRule::DoesNotContain("foo".to_string())); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::BeginsWith) - .set_value("b"); + .set_rule(ConditionalFormatTextRule::BeginsWith("b".to_string())); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::EndsWith) - .set_value("b"); + .set_rule(ConditionalFormatTextRule::EndsWith("b".to_string())); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; @@ -764,30 +738,26 @@ mod conditional_format_tests { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::Contains) - .set_multi_range("A2") - .set_value("foo"); + .set_rule(ConditionalFormatTextRule::Contains("foo".to_string())) + .set_multi_range("A2"); worksheet.add_conditional_format(1, 0, 4, 0, &conditional_format)?; let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::DoesNotContain) - .set_multi_range("B2:B3") - .set_value("foo"); + .set_rule(ConditionalFormatTextRule::DoesNotContain("foo".to_string())) + .set_multi_range("B2:B3"); worksheet.add_conditional_format(1, 0, 4, 0, &conditional_format)?; let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::BeginsWith) - .set_multi_range("C2 C3") - .set_value("b"); + .set_rule(ConditionalFormatTextRule::BeginsWith("b".to_string())) + .set_multi_range("C2 C3"); worksheet.add_conditional_format(1, 0, 4, 0, &conditional_format)?; let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::EndsWith) - .set_multi_range("D2:D3 D4") - .set_value("b"); + .set_rule(ConditionalFormatTextRule::EndsWith("b".to_string())) + .set_multi_range("D2:D3 D4"); worksheet.add_conditional_format(1, 0, 4, 0, &conditional_format)?; @@ -868,43 +838,43 @@ mod conditional_format_tests { worksheet.write(3, 0, 40)?; let conditional_format = - ConditionalFormatDate::new().set_criteria(ConditionalFormatDateCriteria::Yesterday); + ConditionalFormatDate::new().set_rule(ConditionalFormatDateRule::Yesterday); worksheet.add_conditional_format(1, 2, 4, 2, &conditional_format)?; let conditional_format = - ConditionalFormatDate::new().set_criteria(ConditionalFormatDateCriteria::Today); + ConditionalFormatDate::new().set_rule(ConditionalFormatDateRule::Today); worksheet.add_conditional_format(1, 2, 4, 2, &conditional_format)?; let conditional_format = - ConditionalFormatDate::new().set_criteria(ConditionalFormatDateCriteria::Tomorrow); + ConditionalFormatDate::new().set_rule(ConditionalFormatDateRule::Tomorrow); worksheet.add_conditional_format(1, 2, 4, 2, &conditional_format)?; let conditional_format = - ConditionalFormatDate::new().set_criteria(ConditionalFormatDateCriteria::Last7Days); + ConditionalFormatDate::new().set_rule(ConditionalFormatDateRule::Last7Days); worksheet.add_conditional_format(1, 2, 4, 2, &conditional_format)?; let conditional_format = - ConditionalFormatDate::new().set_criteria(ConditionalFormatDateCriteria::LastWeek); + ConditionalFormatDate::new().set_rule(ConditionalFormatDateRule::LastWeek); worksheet.add_conditional_format(1, 2, 4, 2, &conditional_format)?; let conditional_format = - ConditionalFormatDate::new().set_criteria(ConditionalFormatDateCriteria::ThisWeek); + ConditionalFormatDate::new().set_rule(ConditionalFormatDateRule::ThisWeek); worksheet.add_conditional_format(1, 2, 4, 2, &conditional_format)?; let conditional_format = - ConditionalFormatDate::new().set_criteria(ConditionalFormatDateCriteria::NextWeek); + ConditionalFormatDate::new().set_rule(ConditionalFormatDateRule::NextWeek); worksheet.add_conditional_format(1, 2, 4, 2, &conditional_format)?; let conditional_format = - ConditionalFormatDate::new().set_criteria(ConditionalFormatDateCriteria::LastMonth); + ConditionalFormatDate::new().set_rule(ConditionalFormatDateRule::LastMonth); worksheet.add_conditional_format(1, 2, 4, 2, &conditional_format)?; let conditional_format = - ConditionalFormatDate::new().set_criteria(ConditionalFormatDateCriteria::ThisMonth); + ConditionalFormatDate::new().set_rule(ConditionalFormatDateRule::ThisMonth); worksheet.add_conditional_format(1, 2, 4, 2, &conditional_format)?; let conditional_format = - ConditionalFormatDate::new().set_criteria(ConditionalFormatDateCriteria::NextMonth); + ConditionalFormatDate::new().set_rule(ConditionalFormatDateRule::NextMonth); worksheet.add_conditional_format(1, 2, 4, 2, &conditional_format)?; worksheet.assemble_xml_file(); @@ -1077,9 +1047,9 @@ mod conditional_format_tests { worksheet.write(2, 0, 30)?; worksheet.write(3, 0, 40)?; - let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(ExcelDateTime::parse_from_str("2024-01-01")?); + let conditional_format = ConditionalFormatCell::new().set_rule( + ConditionalFormatCellRule::GreaterThan(ExcelDateTime::parse_from_str("2024-01-01")?), + ); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; @@ -1144,10 +1114,11 @@ mod conditional_format_tests { worksheet.write(2, 0, 30)?; worksheet.write(3, 0, 40)?; - let conditional_format = ConditionalFormatCell::new() - .set_minimum(ExcelDateTime::parse_from_str("2024-01-01")?) - .set_maximum(ExcelDateTime::parse_from_str("2024-01-10")?) - .set_criteria(ConditionalFormatCellCriteria::Between); + let conditional_format = + ConditionalFormatCell::new().set_rule(ConditionalFormatCellRule::Between( + ExcelDateTime::parse_from_str("2024-01-01")?, + ExcelDateTime::parse_from_str("2024-01-10")?, + )); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; @@ -1658,19 +1629,19 @@ mod conditional_format_tests { worksheet.write(2, 0, 30)?; worksheet.write(3, 0, 40)?; - let conditional_format = ConditionalFormatFormula::new().set_value("=$A$1>5"); + let conditional_format = ConditionalFormatFormula::new().set_rule("=$A$1>5"); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; - let conditional_format = ConditionalFormatFormula::new().set_value("=$A$2<80"); + let conditional_format = ConditionalFormatFormula::new().set_rule("=$A$2<80"); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; - let conditional_format = ConditionalFormatFormula::new().set_value("\"1+2\""); + let conditional_format = ConditionalFormatFormula::new().set_rule("\"1+2\""); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; - let conditional_format = ConditionalFormatFormula::new().set_value("=$A$3>$A$4"); + let conditional_format = ConditionalFormatFormula::new().set_rule("=$A$3>$A$4"); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; @@ -2233,26 +2204,22 @@ mod conditional_format_tests { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::BeginsWith) - .set_value("b"); + .set_rule(ConditionalFormatTextRule::BeginsWith("b".to_string())); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::BeginsWith) - .set_value("bc"); + .set_rule(ConditionalFormatTextRule::BeginsWith("bc".to_string())); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::EndsWith) - .set_value("z"); + .set_rule(ConditionalFormatTextRule::EndsWith("z".to_string())); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::EndsWith) - .set_value("yz"); + .set_rule(ConditionalFormatTextRule::EndsWith("yz".to_string())); worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; @@ -2327,8 +2294,7 @@ mod conditional_format_tests { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(5) + .set_rule(ConditionalFormatCellRule::GreaterThan(5)) .set_stop_if_true(true); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; diff --git a/src/cookbook.rs b/src/cookbook.rs index 4ba847a2..27ac497a 100644 --- a/src/cookbook.rs +++ b/src/cookbook.rs @@ -1430,10 +1430,9 @@ Code to generate the above example: # Adding Conditional Formatting -Conditional formatting is a feature of Excel which allows you to apply a -format to a cell or a range of cells based on certain criteria. For example -you might apply rules like the following to highlight cells in different -ranges. +Conditional formatting is a feature of Excel which allows you to apply a format +to a cell or a range of cells based on user defined rules. For example you might +apply rules like the following to highlight cells in different ranges. @@ -1458,16 +1457,14 @@ Code to generate the above example: // Write a conditional format over a range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) - .set_value(50) + .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) .set_format(&format1); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; // Write another conditional format over the same range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::LessThan) - .set_value(50) + .set_rule(ConditionalFormatCellRule::LessThan(50)) .set_format(&format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -1490,18 +1487,14 @@ Code to generate the above example: // Write a conditional format over a range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::Between) - .set_minimum(30) - .set_maximum(70) + .set_rule(ConditionalFormatCellRule::Between(30, 70)) .set_format(&format1); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; // Write another conditional format over the same range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::NotBetween) - .set_minimum(30) - .set_maximum(70) + .set_rule(ConditionalFormatCellRule::NotBetween(30, 70)) .set_format(&format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -1558,7 +1551,7 @@ Code to generate the above example: // Write another conditional format over the same range. let conditional_format = ConditionalFormatAverage::new() - .set_criteria(ConditionalFormatAverageCriteria::BelowAverage) + .set_rule(ConditionalFormatAverageRule::BelowAverage) .set_format(&format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -1581,16 +1574,14 @@ Code to generate the above example: // Write a conditional format over a range. let conditional_format = ConditionalFormatTop::new() - .set_value(10) + .set_rule(rust_xlsxwriter::ConditionalFormatTopRule::Top(10)) .set_format(&format1); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; - // Invert the Top conditional format to show Bottom values in the same - // range. + // Also show the bottom values in the same range. let conditional_format = ConditionalFormatTop::new() - .invert() - .set_value(10) + .set_rule(rust_xlsxwriter::ConditionalFormatTopRule::Bottom(10)) .set_format(&format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -1610,8 +1601,7 @@ Code to generate the above example: // Write a conditional format over a non-contiguous range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) - .set_value(50) + .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) .set_multi_range("B3:D6 I3:K6 B9:D12 I9:K12") .set_format(&format1); @@ -1619,8 +1609,7 @@ Code to generate the above example: // Write another conditional format over the same range. let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::LessThan) - .set_value(50) + .set_rule(ConditionalFormatCellRule::LessThan(50)) .set_multi_range("B3:D6 I3:K6 B9:D12 I9:K12") .set_format(&format2); @@ -1644,14 +1633,14 @@ Code to generate the above example: // Write a conditional format over a range. let conditional_format = ConditionalFormatFormula::new() - .set_value("=ISODD(B3)") + .set_rule("=ISODD(B3)") .set_format(&format1); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; // Write another conditional format over the same range. let conditional_format = ConditionalFormatFormula::new() - .set_value("=ISEVEN(B3)") + .set_rule("=ISEVEN(B3)") .set_format(&format2); worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; @@ -1674,32 +1663,30 @@ Code to generate the above example: // Write a text "containing" conditional format over a range. let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::Contains) - .set_value("rust") + .set_rule(ConditionalFormatTextRule::Contains("rust".to_string())) .set_format(&format2); worksheet.add_conditional_format(1, 0, 13, 0, &conditional_format)?; // Write a text "not containing" conditional format over the same range. let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::DoesNotContain) - .set_value("rust") + .set_rule(ConditionalFormatTextRule::DoesNotContain( + "rust".to_string(), + )) .set_format(&format1); worksheet.add_conditional_format(1, 0, 13, 0, &conditional_format)?; // Write a text "begins with" conditional format over a range. let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::BeginsWith) - .set_value("t") + .set_rule(ConditionalFormatTextRule::BeginsWith("t".to_string())) .set_format(&format2); worksheet.add_conditional_format(1, 2, 13, 2, &conditional_format)?; // Write a text "ends with" conditional format over the same range. let conditional_format = ConditionalFormatText::new() - .set_criteria(ConditionalFormatTextCriteria::EndsWith) - .set_value("t") + .set_rule(ConditionalFormatTextRule::EndsWith("t".to_string())) .set_format(&format1); worksheet.add_conditional_format(1, 2, 13, 2, &conditional_format)?; diff --git a/src/worksheet.rs b/src/worksheet.rs index 6f452878..429d254f 100644 --- a/src/worksheet.rs +++ b/src/worksheet.rs @@ -5074,7 +5074,7 @@ impl Worksheet { /// # // This code is available in examples/doc_conditional_format_cell1.rs /// # /// # use rust_xlsxwriter::{ - /// # ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + /// # ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, /// # }; /// # /// # fn main() -> Result<(), XlsxError> { @@ -5114,16 +5114,14 @@ impl Worksheet { /// # /// // Write a conditional format over a range. /// let conditional_format = ConditionalFormatCell::new() - /// .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) - /// .set_value(50) + /// .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) /// .set_format(format1); /// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; /// /// // Write another conditional format over the same range. /// let conditional_format = ConditionalFormatCell::new() - /// .set_criteria(ConditionalFormatCellCriteria::LessThan) - /// .set_value(50) + /// .set_rule(ConditionalFormatCellRule::LessThan(50)) /// .set_format(format2); /// /// worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?; diff --git a/tests/integration/cond_format01.rs b/tests/integration/cond_format01.rs index 7780f98e..7e0772b3 100644 --- a/tests/integration/cond_format01.rs +++ b/tests/integration/cond_format01.rs @@ -7,7 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -26,8 +26,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(5) + .set_rule(ConditionalFormatCellRule::GreaterThan(5)) .set_format(format); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; diff --git a/tests/integration/cond_format02.rs b/tests/integration/cond_format02.rs index d28cd28f..5bfb1be7 100644 --- a/tests/integration/cond_format02.rs +++ b/tests/integration/cond_format02.rs @@ -6,7 +6,7 @@ // Copyright 2022-2023, John McNamara, jmcnamara@cpan.org use crate::common; -use rust_xlsxwriter::{ConditionalFormatCell, ConditionalFormatCellCriteria, Workbook, XlsxError}; +use rust_xlsxwriter::{ConditionalFormatCell, ConditionalFormatCellRule, Workbook, XlsxError}; // Create rust_xlsxwriter file to compare against Excel file. fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { @@ -19,9 +19,8 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(2, 0, 30)?; worksheet.write(3, 0, 40)?; - let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::LessThan) - .set_value(5); + let conditional_format = + ConditionalFormatCell::new().set_rule(ConditionalFormatCellRule::LessThan(5)); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; diff --git a/tests/integration/cond_format03.rs b/tests/integration/cond_format03.rs index ce108ef1..82d3244c 100644 --- a/tests/integration/cond_format03.rs +++ b/tests/integration/cond_format03.rs @@ -7,8 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, FormatUnderline, Workbook, - XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, FormatUnderline, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -30,16 +29,13 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::Between) - .set_minimum(2) - .set_maximum(6) + .set_rule(ConditionalFormatCellRule::Between(2, 6)) .set_format(format1); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(1) + .set_rule(ConditionalFormatCellRule::GreaterThan(1)) .set_format(format2); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; diff --git a/tests/integration/cond_format04.rs b/tests/integration/cond_format04.rs index e4da901b..5ac1e155 100644 --- a/tests/integration/cond_format04.rs +++ b/tests/integration/cond_format04.rs @@ -7,7 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -29,15 +29,13 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(2) + .set_rule(ConditionalFormatCellRule::GreaterThan(2)) .set_format(format1); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::LessThan) - .set_value(8) + .set_rule(ConditionalFormatCellRule::LessThan(8)) .set_format(format2); worksheet.add_conditional_format(1, 0, 1, 0, &conditional_format)?; diff --git a/tests/integration/cond_format05.rs b/tests/integration/cond_format05.rs index b13e40b3..27cc226c 100644 --- a/tests/integration/cond_format05.rs +++ b/tests/integration/cond_format05.rs @@ -7,7 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, FormatBorder, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, FormatBorder, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -24,8 +24,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::EqualTo) - .set_value(7) + .set_rule(ConditionalFormatCellRule::EqualTo(7)) .set_format(format1); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; diff --git a/tests/integration/cond_format06.rs b/tests/integration/cond_format06.rs index b526775a..06a483c7 100644 --- a/tests/integration/cond_format06.rs +++ b/tests/integration/cond_format06.rs @@ -7,8 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, FormatPattern, Workbook, - XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, FormatPattern, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -28,8 +27,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(7) + .set_rule(ConditionalFormatCellRule::GreaterThan(7)) .set_format(format); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; diff --git a/tests/integration/cond_format07.rs b/tests/integration/cond_format07.rs index aee5ffbc..c3e0eb2b 100644 --- a/tests/integration/cond_format07.rs +++ b/tests/integration/cond_format07.rs @@ -7,7 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -39,15 +39,13 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write_row_matrix(0, 0, data)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThanOrEqualTo) - .set_value(50) + .set_rule(ConditionalFormatCellRule::GreaterThanOrEqualTo(50)) .set_format(format1); worksheet.add_conditional_format(0, 0, 9, 9, &conditional_format)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::LessThan) - .set_value(50) + .set_rule(ConditionalFormatCellRule::LessThan(50)) .set_format(format2); worksheet.add_conditional_format(0, 0, 9, 9, &conditional_format)?; diff --git a/tests/integration/cond_format08.rs b/tests/integration/cond_format08.rs index e0c6a0de..b779085c 100644 --- a/tests/integration/cond_format08.rs +++ b/tests/integration/cond_format08.rs @@ -7,7 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -26,8 +26,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(5) + .set_rule(ConditionalFormatCellRule::GreaterThan(5)) .set_format(format); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; diff --git a/tests/integration/cond_format10.rs b/tests/integration/cond_format10.rs index eab9922e..b44589fb 100644 --- a/tests/integration/cond_format10.rs +++ b/tests/integration/cond_format10.rs @@ -7,7 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -26,8 +26,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(5, 1, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(20) + .set_rule(ConditionalFormatCellRule::GreaterThan(20)) .set_format(format); worksheet.add_conditional_format(2, 1, 5, 1, &conditional_format)?; diff --git a/tests/integration/cond_format11.rs b/tests/integration/cond_format11.rs index 147629b5..bb1372fd 100644 --- a/tests/integration/cond_format11.rs +++ b/tests/integration/cond_format11.rs @@ -7,8 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, FormatPattern, Workbook, - XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, FormatPattern, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -30,8 +29,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(5, 1, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(20) + .set_rule(ConditionalFormatCellRule::GreaterThan(20)) .set_format(format); worksheet.add_conditional_format(2, 1, 5, 1, &conditional_format)?; diff --git a/tests/integration/cond_format12.rs b/tests/integration/cond_format12.rs index 96d766d4..8a189b92 100644 --- a/tests/integration/cond_format12.rs +++ b/tests/integration/cond_format12.rs @@ -7,8 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, FormatPattern, Workbook, - XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, FormatPattern, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -30,8 +29,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(5, 1, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(20) + .set_rule(ConditionalFormatCellRule::GreaterThan(20)) .set_format(format); worksheet.add_conditional_format(2, 1, 5, 1, &conditional_format)?; diff --git a/tests/integration/cond_format14.rs b/tests/integration/cond_format14.rs index a9931933..8d0447c9 100644 --- a/tests/integration/cond_format14.rs +++ b/tests/integration/cond_format14.rs @@ -7,7 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -24,8 +24,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(5) + .set_rule(ConditionalFormatCellRule::GreaterThan(5)) .set_format(format) .set_stop_if_true(true); diff --git a/tests/integration/cond_format15.rs b/tests/integration/cond_format15.rs index 46eef766..1a3e868e 100644 --- a/tests/integration/cond_format15.rs +++ b/tests/integration/cond_format15.rs @@ -7,7 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -25,16 +25,14 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::LessThan) - .set_value(5) + .set_rule(ConditionalFormatCellRule::LessThan(5)) .set_format(format1) .set_stop_if_true(true); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(20) + .set_rule(ConditionalFormatCellRule::GreaterThan(20)) .set_format(format2) .set_stop_if_true(true); diff --git a/tests/integration/cond_format16.rs b/tests/integration/cond_format16.rs index a0eff086..9f799dbb 100644 --- a/tests/integration/cond_format16.rs +++ b/tests/integration/cond_format16.rs @@ -7,7 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -25,16 +25,14 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::LessThan) - .set_value(5) + .set_rule(ConditionalFormatCellRule::LessThan(5)) .set_format(format1) .set_stop_if_true(false); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::GreaterThan) - .set_value(20) + .set_rule(ConditionalFormatCellRule::GreaterThan(20)) .set_format(format2) .set_stop_if_true(true); diff --git a/tests/integration/cond_format19.rs b/tests/integration/cond_format19.rs index 3d5654dd..62284bd9 100644 --- a/tests/integration/cond_format19.rs +++ b/tests/integration/cond_format19.rs @@ -7,7 +7,7 @@ use crate::common; use rust_xlsxwriter::{ - ConditionalFormatCell, ConditionalFormatCellCriteria, Format, Workbook, XlsxError, + ConditionalFormatCell, ConditionalFormatCellRule, Format, Workbook, XlsxError, }; // Create rust_xlsxwriter file to compare against Excel file. @@ -26,8 +26,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> { worksheet.write(3, 0, 40)?; let conditional_format = ConditionalFormatCell::new() - .set_criteria(ConditionalFormatCellCriteria::EqualTo) - .set_value("X") + .set_rule(ConditionalFormatCellRule::EqualTo("X")) .set_format(format); worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?;