1
- use crate :: { try_to_vec, CStringHelper , Worksheet , WorksheetCol , WorksheetRow , XlsxError } ;
1
+ use crate :: {
2
+ try_to_vec, CStringHelper , StringOrFloat , Worksheet , WorksheetCol , WorksheetRow , XlsxError ,
3
+ } ;
2
4
3
5
/// And/or operator conditions when using 2 filter rules with `filter_column2`.
4
6
#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
@@ -80,23 +82,31 @@ impl FilterCriteria {
80
82
}
81
83
}
82
84
85
+ /// Options for autofilter rules.
83
86
#[ derive( Debug , Clone , PartialEq , PartialOrd , Default ) ]
84
87
pub struct FilterRule {
88
+ /// The FilterCriteria to define the rule.
85
89
pub criteria : FilterCriteria ,
86
- pub value_string : Option < String > ,
87
- pub value : Option < f64 > ,
90
+ /// value to which the criteria applies.
91
+ pub value : StringOrFloat ,
88
92
}
89
93
90
94
impl FilterRule {
95
+ pub fn new < T : Into < StringOrFloat > > ( criteria : FilterCriteria , value : T ) -> Self {
96
+ FilterRule {
97
+ criteria,
98
+ value : value. into ( ) ,
99
+ }
100
+ }
101
+
91
102
pub ( crate ) fn into_internal (
92
103
& self ,
93
104
c_string_helper : & mut CStringHelper ,
94
105
) -> Result < libxlsxwriter_sys:: lxw_filter_rule , XlsxError > {
95
106
Ok ( libxlsxwriter_sys:: lxw_filter_rule {
96
107
criteria : self . criteria . into_internal ( ) as u8 ,
97
- value_string : c_string_helper. add_opt ( self . value_string . as_deref ( ) ) ?
98
- as * mut std:: ffi:: c_char ,
99
- value : self . value . unwrap_or_default ( ) ,
108
+ value_string : c_string_helper. add_opt ( self . value . to_str ( ) ) ? as * mut std:: ffi:: c_char ,
109
+ value : self . value . to_f64 ( ) . unwrap_or_default ( ) ,
100
110
} )
101
111
}
102
112
}
@@ -131,7 +141,7 @@ impl<'a> Worksheet<'a> {
131
141
132
142
/// This function can be used to filter columns in a autofilter range based on single rule conditions.
133
143
///
134
- /// The `col` parameter is a zero indexed column number and must refer to a column in an existing autofilter created with ` autofilter`.
144
+ /// The `col` parameter is a zero indexed column number and must refer to a column in an existing autofilter created with [`Worksheet:: autofilter`] .
135
145
/// It isn't sufficient to just specify the filter condition. You must also hide any rows that don't match the filter condition.
136
146
pub fn filter_column (
137
147
& mut self ,
@@ -235,11 +245,7 @@ mod test {
235
245
// ------------
236
246
let mut worksheet1 = workbook. add_worksheet ( Some ( "Sheet 1" ) ) ?;
237
247
create_sheet ( & mut worksheet1) ?;
238
- let worksheet1_criteria = FilterRule {
239
- criteria : FilterCriteria :: GreaterThan ,
240
- value : Some ( 10. ) ,
241
- value_string : None ,
242
- } ;
248
+ let worksheet1_criteria = FilterRule :: new ( FilterCriteria :: GreaterThan , 10.0 ) ;
243
249
worksheet1. filter_column ( 0 , & worksheet1_criteria) ?;
244
250
let mut hidden_row = RowColOptions :: new ( true , 0 , false ) ;
245
251
for i in 1 ..=10 {
@@ -248,16 +254,8 @@ mod test {
248
254
// ------------
249
255
let mut worksheet2 = workbook. add_worksheet ( Some ( "Sheet 2" ) ) ?;
250
256
create_sheet ( & mut worksheet2) ?;
251
- let worksheet2_criteria1 = FilterRule {
252
- criteria : FilterCriteria :: GreaterThanOrEqualTo ,
253
- value : Some ( 3. ) ,
254
- value_string : None ,
255
- } ;
256
- let worksheet2_criteria2 = FilterRule {
257
- criteria : FilterCriteria :: LessThan ,
258
- value : Some ( 5.5 ) ,
259
- value_string : None ,
260
- } ;
257
+ let worksheet2_criteria1 = FilterRule :: new ( FilterCriteria :: GreaterThanOrEqualTo , 3.0 ) ;
258
+ let worksheet2_criteria2 = FilterRule :: new ( FilterCriteria :: LessThan , 5.5 ) ;
261
259
worksheet2. filter_column2 (
262
260
1 ,
263
261
& worksheet2_criteria1,
0 commit comments