Skip to content

Commit

Permalink
cond format: add text style conditional format
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Nov 11, 2023
1 parent 1b6f98c commit 6dedbf1
Show file tree
Hide file tree
Showing 5 changed files with 756 additions and 35 deletions.
71 changes: 69 additions & 2 deletions examples/app_conditional_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use rust_xlsxwriter::{
ConditionalFormatAverage, ConditionalFormatAverageCriteria, ConditionalFormatCell,
ConditionalFormatCellCriteria, ConditionalFormatDuplicate, ConditionalFormatTop, Format,
Workbook, XlsxError,
ConditionalFormatCellCriteria, ConditionalFormatDuplicate, ConditionalFormatText,
ConditionalFormatTextCriteria, ConditionalFormatTop, Format, Workbook, XlsxError,
};

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

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

// -----------------------------------------------------------------------
// Example 7. Text style conditional formats.
// -----------------------------------------------------------------------
let caption =
"Column A shows words that contain the sub-word 'rust'. Column C shows words that start/end with 't'";

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

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

// Add some sample data.
let word_list = [
"apocrustic",
"burstwort",
"cloudburst",
"crustification",
"distrustfulness",
"laurustine",
"outburst",
"rusticism",
"thunderburst",
"trustee",
"trustworthiness",
"unburstableness",
"unfrustratable",
];
worksheet.write_column(1, 0, word_list)?;
worksheet.write_column(1, 2, word_list)?;

// Set the column widths for clarity.
worksheet.set_column_width(0, 20)?;
worksheet.set_column_width(2, 20)?;

// Write a text "containing" conditional format over a range.
let conditional_format = ConditionalFormatText::new()
.set_criteria(ConditionalFormatTextCriteria::Contains)
.set_value("rust")
.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_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_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_format(&format1);

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

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

//! Example of adding a text type conditional formatting to a worksheet.
use rust_xlsxwriter::{
ConditionalFormatText, ConditionalFormatTextCriteria, 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 = [
"apocrustic",
"burstwort",
"cloudburst",
"crustification",
"distrustfulness",
"laurustine",
"outburst",
"rusticism",
"thunderburst",
"trustee",
"trustworthiness",
"unburstableness",
"unfrustratable",
];
worksheet.write_column(0, 0, data)?;
worksheet.write_column(0, 2, data)?;

// Set the column widths for clarity.
worksheet.set_column_width(0, 20)?;
worksheet.set_column_width(2, 20)?;

// Add a format. Green fill with dark green text.
let format1 = Format::new()
.set_font_color("006100")
.set_background_color("C6EFCE");

// Add a format. Light red fill with dark red text.
let format2 = Format::new()
.set_font_color("9C0006")
.set_background_color("FFC7CE");

// Write a text "containing" conditional format over a range.
let conditional_format = ConditionalFormatText::new()
.set_criteria(ConditionalFormatTextCriteria::Contains)
.set_value("rust")
.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_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_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_format(&format2);

worksheet.add_conditional_format(0, 2, 12, 2, &conditional_format)?;

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

Ok(())
}
Loading

0 comments on commit 6dedbf1

Please sign in to comment.