-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cond format: add blanks and errors style conditional format
- Loading branch information
Showing
4 changed files
with
561 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
// | ||
// Copyright 2022-2023, John McNamara, [email protected] | ||
|
||
//! Example of how to add a blank/non-blank conditional formatting to a | ||
//! worksheet. Blank values are in light red. Non-blank values are in light | ||
//! green. Note, that we invert the Blank rule to get Non-blank values. | ||
use rust_xlsxwriter::{ConditionalFormatBlank, 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 = [ | ||
"Not blank", | ||
"", | ||
"", | ||
"Not blank", | ||
"Not blank", | ||
"", | ||
"Not blank", | ||
"Not blank", | ||
"", | ||
"Not blank", | ||
"", | ||
"Not blank", | ||
]; | ||
worksheet.write_column(0, 0, 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 over a range. | ||
let conditional_format = ConditionalFormatBlank::new().set_format(format1); | ||
|
||
worksheet.add_conditional_format(0, 0, 11, 0, &conditional_format)?; | ||
|
||
// Invert the blank conditional format to show non-blank values. | ||
let conditional_format = ConditionalFormatBlank::new().invert().set_format(format2); | ||
|
||
worksheet.add_conditional_format(0, 0, 11, 0, &conditional_format)?; | ||
|
||
// Save the file. | ||
workbook.save("conditional_format.xlsx")?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
// | ||
// Copyright 2022-2023, John McNamara, [email protected] | ||
|
||
//! Example of how to add a error/non-error conditional formatting to a | ||
//! worksheet. Error values are in light red. Non-error values are in light | ||
//! green. Note, that we invert the Error rule to get Non-error values. | ||
use rust_xlsxwriter::{ConditionalFormatError, Format, Formula, 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. | ||
worksheet.write(0, 0, Formula::new("=1/1"))?; | ||
worksheet.write(1, 0, Formula::new("=1/0"))?; | ||
worksheet.write(2, 0, Formula::new("=1/0"))?; | ||
worksheet.write(3, 0, Formula::new("=1/1"))?; | ||
worksheet.write(4, 0, Formula::new("=1/1"))?; | ||
worksheet.write(5, 0, Formula::new("=1/0"))?; | ||
worksheet.write(6, 0, Formula::new("=1/1"))?; | ||
worksheet.write(7, 0, Formula::new("=1/1"))?; | ||
worksheet.write(8, 0, Formula::new("=1/0"))?; | ||
worksheet.write(9, 0, Formula::new("=1/1"))?; | ||
worksheet.write(10, 0, Formula::new("=1/0"))?; | ||
worksheet.write(11, 0, Formula::new("=1/1"))?; | ||
|
||
// 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 over a range. | ||
let conditional_format = ConditionalFormatError::new().set_format(format1); | ||
|
||
worksheet.add_conditional_format(0, 0, 11, 0, &conditional_format)?; | ||
|
||
// Invert the error conditional format to show non-error values. | ||
let conditional_format = ConditionalFormatError::new().invert().set_format(format2); | ||
|
||
worksheet.add_conditional_format(0, 0, 11, 0, &conditional_format)?; | ||
|
||
// Save the file. | ||
workbook.save("conditional_format.xlsx")?; | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.