Skip to content

Commit

Permalink
cond format: add average style conditional format
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Nov 9, 2023
1 parent 48bec62 commit cfc9a89
Show file tree
Hide file tree
Showing 4 changed files with 476 additions and 8 deletions.
35 changes: 33 additions & 2 deletions examples/app_conditional_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
//! cells based on certain criteria.
use rust_xlsxwriter::{
ConditionalFormatCell, ConditionalFormatCellCriteria, ConditionalFormatDuplicate, Format,
Workbook, XlsxError,
ConditionalFormatAverage, ConditionalFormatAverageCriteria, ConditionalFormatCell,
ConditionalFormatCellCriteria, ConditionalFormatDuplicate, Format, Workbook, XlsxError,
};

fn main() -> Result<(), XlsxError> {
Expand Down Expand Up @@ -146,6 +146,37 @@ fn main() -> Result<(), XlsxError> {

worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?;

// -----------------------------------------------------------------------
// Example 4. Above and Below Average conditional formats.
// -----------------------------------------------------------------------
let caption = "Above average values are in light red. Below average values are in light green.";

// Add a worksheet to the workbook.
let worksheet = workbook.add_worksheet();

// Write the caption.
worksheet.write(0, 1, caption)?;

// Write the worksheet data.
worksheet.write_row_matrix(2, 1, data)?;

// Set the column widths for clarity.
for col_num in 1..=10u16 {
worksheet.set_column_width(col_num, 6)?;
}

// Write a conditional format over a range. The default criteria is Above Average.
let conditional_format = ConditionalFormatAverage::new().set_format(&format1);

worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?;

// Write another conditional format over the same range.
let conditional_format = ConditionalFormatAverage::new()
.set_criteria(ConditionalFormatAverageCriteria::BelowAverage)
.set_format(&format2);

worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?;

// -----------------------------------------------------------------------
// Save and close the file.
// -----------------------------------------------------------------------
Expand Down
63 changes: 63 additions & 0 deletions examples/doc_conditional_format_average.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2023, John McNamara, [email protected]

//! Example of how to add Average conditional formatting to a worksheet. Above
//! average values are in light red. Below average values are in light green.
use rust_xlsxwriter::{
ConditionalFormatAverage, ConditionalFormatAverageCriteria, Format, Workbook, XlsxError,
};

fn main() -> Result<(), XlsxError> {
// Create a new Excel file object.
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();

// Add some sample data.
let data = [
[34, 72, 38, 30, 75, 48, 75, 66, 84, 86],
[6, 24, 1, 84, 54, 62, 60, 3, 26, 59],
[28, 79, 97, 13, 85, 93, 93, 22, 5, 14],
[27, 71, 40, 17, 18, 79, 90, 93, 29, 47],
[88, 25, 33, 23, 67, 1, 59, 79, 47, 36],
[24, 100, 20, 88, 29, 33, 38, 54, 54, 88],
[6, 57, 88, 28, 10, 26, 37, 7, 41, 48],
[52, 78, 1, 96, 26, 45, 47, 33, 96, 36],
[60, 54, 81, 66, 81, 90, 80, 93, 12, 55],
[70, 5, 46, 14, 71, 19, 66, 36, 41, 21],
];
worksheet.write_row_matrix(2, 1, data)?;

// Set the column widths for clarity.
for col_num in 1..=10u16 {
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");

// Write a conditional format. The default criteria is Above Average.
let conditional_format = ConditionalFormatAverage::new().set_format(format1);

worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?;

// Write another conditional format over the same range.
let conditional_format = ConditionalFormatAverage::new()
.set_criteria(ConditionalFormatAverageCriteria::BelowAverage)
.set_format(format2);

worksheet.add_conditional_format(2, 1, 11, 10, &conditional_format)?;

// Save the file.
workbook.save("conditional_format.xlsx")?;

Ok(())
}
Loading

0 comments on commit cfc9a89

Please sign in to comment.