diff --git a/src/conditional_format.rs b/src/conditional_format.rs index 184e73a7..5a191c76 100644 --- a/src/conditional_format.rs +++ b/src/conditional_format.rs @@ -2340,6 +2340,7 @@ impl ConditionalFormatText { 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 { @@ -2353,11 +2354,11 @@ impl ConditionalFormatText { } ConditionalFormatTextCriteria::BeginsWith => { attributes.push(("type", "beginsWith".to_string())); - format!(r#"LEFT({anchor},1)="{text}""#) + format!(r#"LEFT({anchor},{length})="{text}""#) } ConditionalFormatTextCriteria::EndsWith => { attributes.push(("type", "endsWith".to_string())); - format!(r#"RIGHT({anchor},1)="{text}""#) + format!(r#"RIGHT({anchor},{length})="{text}""#) } }; diff --git a/src/conditional_format/tests.rs b/src/conditional_format/tests.rs index 1bb3c750..3c2b8362 100644 --- a/src/conditional_format/tests.rs +++ b/src/conditional_format/tests.rs @@ -1695,6 +1695,373 @@ mod conditional_format_tests { Ok(()) } + #[test] + fn conditional_format_16() -> Result<(), XlsxError> { + let mut worksheet = Worksheet::new(); + worksheet.set_selected(true); + + worksheet.write(0, 0, 1)?; + worksheet.write(1, 0, 2)?; + worksheet.write(2, 0, 3)?; + worksheet.write(3, 0, 4)?; + worksheet.write(4, 0, 5)?; + worksheet.write(5, 0, 6)?; + worksheet.write(6, 0, 7)?; + worksheet.write(7, 0, 8)?; + worksheet.write(8, 0, 9)?; + worksheet.write(9, 0, 10)?; + worksheet.write(10, 0, 11)?; + worksheet.write(11, 0, 12)?; + + let conditional_format = ConditionalFormat3ColorScale::new() + .set_minimum_color("C5D9F1") + .set_midpoint_color("8DB4E3") + .set_maximum_color("538ED5"); + + worksheet.add_conditional_format(0, 0, 11, 0, &conditional_format)?; + + worksheet.assemble_xml_file(); + + let got = worksheet.writer.read_to_str(); + let got = xml_to_vec(got); + + let expected = xml_to_vec( + r#" + + + + + + + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + 5 + + + + + 6 + + + + + 7 + + + + + 8 + + + + + 9 + + + + + 10 + + + + + 11 + + + + + 12 + + + + + + + + + + + + + + + + + + "#, + ); + + assert_eq!(expected, got); + + Ok(()) + } + + #[test] + fn conditional_format_17() -> Result<(), XlsxError> { + let mut worksheet = Worksheet::new(); + worksheet.set_selected(true); + + worksheet.write(0, 0, 1)?; + worksheet.write(1, 0, 2)?; + worksheet.write(2, 0, 3)?; + worksheet.write(3, 0, 4)?; + worksheet.write(4, 0, 5)?; + worksheet.write(5, 0, 6)?; + worksheet.write(6, 0, 7)?; + worksheet.write(7, 0, 8)?; + worksheet.write(8, 0, 9)?; + worksheet.write(9, 0, 10)?; + worksheet.write(10, 0, 11)?; + worksheet.write(11, 0, 12)?; + + let conditional_format = ConditionalFormat3ColorScale::new() + .set_minimum(ConditionalFormatType::Number, Formula::new("$A$10")) + .set_midpoint(ConditionalFormatType::Percent, 52) + .set_maximum(ConditionalFormatType::Percentile, 99); + + worksheet.add_conditional_format(0, 0, 11, 0, &conditional_format)?; + + worksheet.assemble_xml_file(); + + let got = worksheet.writer.read_to_str(); + let got = xml_to_vec(got); + + let expected = xml_to_vec( + r#" + + + + + + + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + 5 + + + + + 6 + + + + + 7 + + + + + 8 + + + + + 9 + + + + + 10 + + + + + 11 + + + + + 12 + + + + + + + + + + + + + + + + + + "#, + ); + + assert_eq!(expected, got); + + Ok(()) + } + + #[test] + fn conditional_format_18() -> Result<(), XlsxError> { + let mut worksheet = Worksheet::new(); + worksheet.set_selected(true); + + worksheet.write(0, 0, 1)?; + worksheet.write(1, 0, 2)?; + worksheet.write(2, 0, 3)?; + worksheet.write(3, 0, 4)?; + worksheet.write(4, 0, 5)?; + worksheet.write(5, 0, 6)?; + worksheet.write(6, 0, 7)?; + worksheet.write(7, 0, 8)?; + worksheet.write(8, 0, 9)?; + worksheet.write(9, 0, 10)?; + worksheet.write(10, 0, 11)?; + worksheet.write(11, 0, 12)?; + + let conditional_format = + ConditionalFormat3ColorScale::new().set_multi_range("$A$3:$A$4,A1,A6:$A$8,$A10,A$12"); + + worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; + + worksheet.assemble_xml_file(); + + let got = worksheet.writer.read_to_str(); + let got = xml_to_vec(got); + + let expected = xml_to_vec( + r#" + + + + + + + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + 5 + + + + + 6 + + + + + 7 + + + + + 8 + + + + + 9 + + + + + 10 + + + + + 11 + + + + + 12 + + + + + + + + + + + + + + + + + + "#, + ); + + assert_eq!(expected, got); + + Ok(()) + } + #[test] fn conditional_format_19() -> Result<(), XlsxError> { let mut worksheet = Worksheet::new(); @@ -1816,6 +2183,168 @@ mod conditional_format_tests { Ok(()) } + #[test] + fn conditional_format_20() -> Result<(), XlsxError> { + let mut worksheet = Worksheet::new(); + worksheet.set_selected(true); + + worksheet.write(0, 0, 10)?; + worksheet.write(1, 0, 20)?; + worksheet.write(2, 0, 30)?; + worksheet.write(3, 0, 40)?; + + let conditional_format = ConditionalFormatText::new() + .set_criteria(ConditionalFormatTextCriteria::BeginsWith) + .set_value("b"); + + worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; + + let conditional_format = ConditionalFormatText::new() + .set_criteria(ConditionalFormatTextCriteria::BeginsWith) + .set_value("bc"); + + worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; + + let conditional_format = ConditionalFormatText::new() + .set_criteria(ConditionalFormatTextCriteria::EndsWith) + .set_value("z"); + + worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; + + let conditional_format = ConditionalFormatText::new() + .set_criteria(ConditionalFormatTextCriteria::EndsWith) + .set_value("yz"); + + worksheet.add_conditional_format(0, 0, 3, 0, &conditional_format)?; + + worksheet.assemble_xml_file(); + + let got = worksheet.writer.read_to_str(); + let got = xml_to_vec(got); + + let expected = xml_to_vec( + r#" + + + + + + + + + + + 10 + + + + + 20 + + + + + 30 + + + + + 40 + + + + + + LEFT(A1,1)="b" + + + LEFT(A1,2)="bc" + + + RIGHT(A1,1)="z" + + + RIGHT(A1,2)="yz" + + + + + "#, + ); + + assert_eq!(expected, got); + + Ok(()) + } + + #[test] + fn conditional_format_21() -> Result<(), XlsxError> { + let mut worksheet = Worksheet::new(); + worksheet.set_selected(true); + + worksheet.write(0, 0, 10)?; + worksheet.write(1, 0, 20)?; + worksheet.write(2, 0, 30)?; + worksheet.write(3, 0, 40)?; + + let conditional_format = ConditionalFormatCell::new() + .set_criteria(ConditionalFormatCellCriteria::GreaterThan) + .set_value(5) + .set_stop_if_true(true); + + worksheet.add_conditional_format(0, 0, 0, 0, &conditional_format)?; + + worksheet.assemble_xml_file(); + + let got = worksheet.writer.read_to_str(); + let got = xml_to_vec(got); + + let expected = xml_to_vec( + r#" + + + + + + + + + + + 10 + + + + + 20 + + + + + 30 + + + + + 40 + + + + + + 5 + + + + + "#, + ); + + assert_eq!(expected, got); + + Ok(()) + } + #[test] fn data_bar_01() -> Result<(), XlsxError> { let mut worksheet = Worksheet::new();