Skip to content

Commit

Permalink
cond format: refactor format rules with generic enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Nov 24, 2023
1 parent 81a3871 commit f32e446
Show file tree
Hide file tree
Showing 31 changed files with 757 additions and 662 deletions.
54 changes: 21 additions & 33 deletions examples/app_conditional_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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)?;
Expand All @@ -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)?;
Expand Down Expand Up @@ -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)?;
Expand All @@ -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)?;
Expand All @@ -243,17 +235,15 @@ 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);

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_multi_range("B3:D6 I3:K6 B9:D12 I9:K12")
.set_format(&format2);

Expand All @@ -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)?;
Expand Down Expand Up @@ -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)?;
Expand Down
8 changes: 4 additions & 4 deletions examples/doc_conditional_format_anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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)?;
Expand All @@ -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)?;
Expand All @@ -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)?;
Expand Down
4 changes: 2 additions & 2 deletions examples/doc_conditional_format_average.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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)?;
Expand Down
8 changes: 3 additions & 5 deletions examples/doc_conditional_format_cell1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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)?;
Expand Down
10 changes: 3 additions & 7 deletions examples/doc_conditional_format_cell2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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)?;
Expand Down
6 changes: 2 additions & 4 deletions examples/doc_conditional_format_cell_set_minimum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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)?;
Expand Down
5 changes: 2 additions & 3 deletions examples/doc_conditional_format_cell_set_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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)?;
Expand Down
9 changes: 4 additions & 5 deletions examples/doc_conditional_format_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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)?;
Expand Down
4 changes: 2 additions & 2 deletions examples/doc_conditional_format_formula.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
8 changes: 3 additions & 5 deletions examples/doc_conditional_format_multi_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -48,17 +48,15 @@ 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);

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_multi_range("B3:D6 I3:K6 B9:D12 I9:K12")
.set_format(format2);

Expand Down
Loading

0 comments on commit f32e446

Please sign in to comment.