rust_xlsxwriter 0.96.0

A Rust library for writing Excel 2007 xlsx files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
// filter - A module for representing autofilter conditions.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2026, John McNamara, jmcnamara@cpan.org

#![warn(missing_docs)]

/// The `FilterCondition` struct is used to define autofilter rules.
///
/// Autofilter rules are associated with ranges created using
/// [`autofilter()`](crate::Worksheet::autofilter()).
///
/// Excel supports two main types of filter conditions. The first, and most
/// common, is a list filter where the user selects the items to filter from a
/// list of all the values in the column range:
///
/// <img src="https://rustxlsxwriter.github.io/images/autofilter_list.png">
///
/// The other main type of filter is a custom filter where the user can specify
/// 1 or 2 conditions like ">= 4000" and "<= 6000":
///
/// <img src="https://rustxlsxwriter.github.io/images/autofilter_custom.png">
///
/// In Excel these are mutually exclusive and you will need to choose one or the
/// other via the [`FilterCondition`]
/// [`add_list_filter()`](FilterCondition::add_list_filter) and
/// [`add_custom_filter()`](FilterCondition::add_custom_filter) methods.
///
///
///
/// # Examples
///
/// The following example demonstrates setting an autofilter with a list filter
/// condition.
///
/// ```
/// # // This code is available in examples/doc_worksheet_filter_column1.rs
/// #
/// # use rust_xlsxwriter::{FilterCondition, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     let mut workbook = Workbook::new();
/// #
/// #     // Add a worksheet with some sample data to filter.
/// #     let worksheet = workbook.add_worksheet();
/// #     worksheet.write_string(0, 0, "Region")?;
/// #     worksheet.write_string(1, 0, "East")?;
/// #     worksheet.write_string(2, 0, "West")?;
/// #     worksheet.write_string(3, 0, "East")?;
/// #     worksheet.write_string(4, 0, "North")?;
/// #     worksheet.write_string(5, 0, "South")?;
/// #     worksheet.write_string(6, 0, "West")?;
/// #
/// #     worksheet.write_string(0, 1, "Sales")?;
/// #     worksheet.write_number(1, 1, 3000)?;
/// #     worksheet.write_number(2, 1, 8000)?;
/// #     worksheet.write_number(3, 1, 5000)?;
/// #     worksheet.write_number(4, 1, 4000)?;
/// #     worksheet.write_number(5, 1, 7000)?;
/// #     worksheet.write_number(6, 1, 9000)?;
/// #
/// #     // Set the autofilter.
/// #     worksheet.autofilter(0, 0, 6, 1)?;
///
///     // Set a filter condition to only show cells matching "East" in the first
///     // column.
///     let filter_condition = FilterCondition::new().add_list_filter("East");
///     worksheet.filter_column(0, &filter_condition)?;
///
/// #     workbook.save("worksheet.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// Output file:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/worksheet_filter_column1.png">
///
///
/// The following example demonstrates setting an autofilter with multiple list
/// filter conditions.
///
/// ```
/// # // This code is available in examples/doc_worksheet_filter_column2.rs
/// #
/// # use rust_xlsxwriter::{FilterCondition, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     let mut workbook = Workbook::new();
/// #
/// #     // Add a worksheet with some sample data to filter.
/// #     let worksheet = workbook.add_worksheet();
/// #     worksheet.write_string(0, 0, "Region")?;
/// #     worksheet.write_string(1, 0, "East")?;
/// #     worksheet.write_string(2, 0, "West")?;
/// #     worksheet.write_string(3, 0, "East")?;
/// #     worksheet.write_string(4, 0, "North")?;
/// #     worksheet.write_string(5, 0, "South")?;
/// #     worksheet.write_string(6, 0, "West")?;
/// #
/// #     worksheet.write_string(0, 1, "Sales")?;
/// #     worksheet.write_number(1, 1, 3000)?;
/// #     worksheet.write_number(2, 1, 8000)?;
/// #     worksheet.write_number(3, 1, 5000)?;
/// #     worksheet.write_number(4, 1, 4000)?;
/// #     worksheet.write_number(5, 1, 7000)?;
/// #     worksheet.write_number(6, 1, 9000)?;
/// #
/// #     // Set the autofilter.
/// #     worksheet.autofilter(0, 0, 6, 1)?;
///
///     // Set a filter condition to only show cells matching "East", "West" or
///     // "South" in the first column.
///     let filter_condition = FilterCondition::new()
///         .add_list_filter("East")
///         .add_list_filter("West")
///         .add_list_filter("South");
///     worksheet.filter_column(0, &filter_condition)?;
///
/// #     workbook.save("worksheet.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// Output file:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/worksheet_filter_column2.png">
///
///
/// The following example demonstrates setting an autofilter with a list filter
/// for blank cells.
///
/// ```
/// # // This code is available in examples/doc_worksheet_filter_column3.rs
/// #
/// # use rust_xlsxwriter::{FilterCondition, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     let mut workbook = Workbook::new();
/// #
/// #     // Add a worksheet with some sample data to filter.
/// #     let worksheet = workbook.add_worksheet();
/// #     worksheet.write_string(0, 0, "Region")?;
/// #     worksheet.write_string(1, 0, "")?;
/// #     worksheet.write_string(2, 0, "West")?;
/// #     worksheet.write_string(3, 0, "East")?;
/// #     worksheet.write_string(4, 0, "North")?;
/// #
/// #     worksheet.write_string(6, 0, "West")?;
/// #
/// #     worksheet.write_string(0, 1, "Sales")?;
/// #     worksheet.write_number(1, 1, 3000)?;
/// #     worksheet.write_number(2, 1, 8000)?;
/// #     worksheet.write_number(3, 1, 5000)?;
/// #     worksheet.write_number(4, 1, 4000)?;
/// #     worksheet.write_number(5, 1, 7000)?;
/// #     worksheet.write_number(6, 1, 9000)?;
/// #
/// #     // Set the autofilter.
/// #     worksheet.autofilter(0, 0, 6, 1)?;
///
///     // Set a filter condition to only show cells matching blanks.
///     let filter_condition = FilterCondition::new().add_list_blanks_filter();
///
///     worksheet.filter_column(0, &filter_condition)?;
///
/// #     workbook.save("worksheet.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// Output file:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/worksheet_filter_column3.png">
///
///
/// The following example demonstrates setting an autofilter with different list
/// filter conditions in separate columns.
///
/// ```
/// # // This code is available in examples/doc_worksheet_filter_column4.rs
/// #
/// # use rust_xlsxwriter::{FilterCondition, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     let mut workbook = Workbook::new();
/// #
/// #     // Add a worksheet with some sample data to filter.
/// #     let worksheet = workbook.add_worksheet();
/// #     worksheet.write_string(0, 0, "Region")?;
/// #     worksheet.write_string(1, 0, "East")?;
/// #     worksheet.write_string(2, 0, "West")?;
/// #     worksheet.write_string(3, 0, "East")?;
/// #     worksheet.write_string(4, 0, "North")?;
/// #     worksheet.write_string(5, 0, "South")?;
/// #     worksheet.write_string(6, 0, "West")?;
/// #
/// #     worksheet.write_string(0, 1, "Sales")?;
/// #     worksheet.write_number(1, 1, 3000)?;
/// #     worksheet.write_number(2, 1, 8000)?;
/// #     worksheet.write_number(3, 1, 5000)?;
/// #     worksheet.write_number(4, 1, 4000)?;
/// #     worksheet.write_number(5, 1, 7000)?;
/// #     worksheet.write_number(6, 1, 9000)?;
/// #
/// #     // Set the autofilter.
/// #     worksheet.autofilter(0, 0, 6, 1)?;
///
///     // Set a filter condition for 2 separate columns.
///     let filter_condition1 = FilterCondition::new().add_list_filter("East");
///     worksheet.filter_column(0, &filter_condition1)?;
///
///     let filter_condition2 = FilterCondition::new().add_list_filter(3000);
///     worksheet.filter_column(1, &filter_condition2)?;
///
/// #     workbook.save("worksheet.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// Output file:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/worksheet_filter_column4.png">
///
///
/// The following example demonstrates setting an autofilter for a custom number
/// filter.
///
/// ```
/// # // This code is available in examples/doc_worksheet_filter_column5.rs
/// #
/// # use rust_xlsxwriter::{FilterCondition, FilterCriteria, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     let mut workbook = Workbook::new();
/// #
/// #     // Add a worksheet with some sample data to filter.
/// #     let worksheet = workbook.add_worksheet();
/// #     worksheet.write_string(0, 0, "Region")?;
/// #     worksheet.write_string(1, 0, "East")?;
/// #     worksheet.write_string(2, 0, "West")?;
/// #     worksheet.write_string(3, 0, "East")?;
/// #     worksheet.write_string(4, 0, "North")?;
/// #     worksheet.write_string(5, 0, "South")?;
/// #     worksheet.write_string(6, 0, "West")?;
/// #
/// #     worksheet.write_string(0, 1, "Sales")?;
/// #     worksheet.write_number(1, 1, 3000)?;
/// #     worksheet.write_number(2, 1, 8000)?;
/// #     worksheet.write_number(3, 1, 5000)?;
/// #     worksheet.write_number(4, 1, 4000)?;
/// #     worksheet.write_number(5, 1, 7000)?;
/// #     worksheet.write_number(6, 1, 9000)?;
/// #
/// #     // Set the autofilter.
/// #     worksheet.autofilter(0, 0, 6, 1)?;
///
///     // Set a custom number filter.
///     let filter_condition =
///         FilterCondition::new().add_custom_filter(FilterCriteria::GreaterThan, 4000);
///     worksheet.filter_column(1, &filter_condition)?;
///
/// #     workbook.save("worksheet.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// Output file:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/worksheet_filter_column5.png">
///
///
/// The following example demonstrates setting an autofilter for two custom
/// number filters to create a "between" condition.
///
/// ```
/// # // This code is available in examples/doc_worksheet_filter_column6.rs
/// #
/// # use rust_xlsxwriter::{FilterCondition, FilterCriteria, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     let mut workbook = Workbook::new();
/// #
/// #     // Add a worksheet with some sample data to filter.
/// #     let worksheet = workbook.add_worksheet();
/// #     worksheet.write_string(0, 0, "Region")?;
/// #     worksheet.write_string(1, 0, "East")?;
/// #     worksheet.write_string(2, 0, "West")?;
/// #     worksheet.write_string(3, 0, "East")?;
/// #     worksheet.write_string(4, 0, "North")?;
/// #     worksheet.write_string(5, 0, "South")?;
/// #     worksheet.write_string(6, 0, "West")?;
/// #
/// #     worksheet.write_string(0, 1, "Sales")?;
/// #     worksheet.write_number(1, 1, 3000)?;
/// #     worksheet.write_number(2, 1, 8000)?;
/// #     worksheet.write_number(3, 1, 5000)?;
/// #     worksheet.write_number(4, 1, 4000)?;
/// #     worksheet.write_number(5, 1, 7000)?;
/// #     worksheet.write_number(6, 1, 9000)?;
/// #
/// #     // Set the autofilter.
/// #     worksheet.autofilter(0, 0, 6, 1)?;
///
///     // Set two custom number filters in a "between" configuration.
///     let filter_condition = FilterCondition::new()
///         .add_custom_filter(FilterCriteria::GreaterThanOrEqualTo, 4000)
///         .add_custom_filter(FilterCriteria::LessThanOrEqualTo, 8000);
///     worksheet.filter_column(1, &filter_condition)?;
///
/// #     workbook.save("worksheet.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// Output file:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/worksheet_filter_column6.png">
///
///
/// The following example demonstrates setting an autofilter to show all the
/// non-blank values in a column. This can be done in 2 ways: by adding a filter
/// for each district string/number in the column or since that may be difficult
/// to figure out programmatically you can set a custom filter. Excel uses both
/// of these methods depending on the data being filtered.
///
/// ```
/// # // This code is available in examples/doc_worksheet_filter_column7.rs
/// #
/// # use rust_xlsxwriter::{FilterCondition, FilterCriteria, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     let mut workbook = Workbook::new();
/// #
/// #     // Add a worksheet with some sample data to filter.
/// #     let worksheet = workbook.add_worksheet();
/// #     worksheet.write_string(0, 0, "Region")?;
/// #     worksheet.write_string(1, 0, "")?;
/// #     worksheet.write_string(2, 0, "West")?;
/// #     worksheet.write_string(3, 0, "East")?;
/// #     worksheet.write_string(4, 0, "")?;
/// #     worksheet.write_string(5, 0, "")?;
/// #     worksheet.write_string(6, 0, "West")?;
/// #
/// #     worksheet.write_string(0, 1, "Sales")?;
/// #     worksheet.write_number(1, 1, 3000)?;
/// #     worksheet.write_number(2, 1, 8000)?;
/// #     worksheet.write_number(3, 1, 5000)?;
/// #     worksheet.write_number(4, 1, 4000)?;
/// #     worksheet.write_number(5, 1, 7000)?;
/// #     worksheet.write_number(6, 1, 9000)?;
/// #
/// #     // Set the autofilter.
/// #     worksheet.autofilter(0, 0, 6, 1)?;
///
///     // Filter non-blanks by filtering on all the unique non-blank
///     // strings/numbers in the column.
///     let filter_condition = FilterCondition::new()
///         .add_list_filter("East")
///         .add_list_filter("West")
///         .add_list_filter("North")
///         .add_list_filter("South");
///     worksheet.filter_column(0, &filter_condition)?;
///
///     // Or you can add a simpler custom filter to get the same result.
///
///     // Set a custom number filter of `!= " "` to filter non blanks.
///     let filter_condition =
///         FilterCondition::new().add_custom_filter(FilterCriteria::NotEqualTo, " ");
///     worksheet.filter_column(0, &filter_condition)?;
///
/// #     workbook.save("worksheet.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// Output file:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/worksheet_filter_column7.png">
///
#[derive(Clone)]
pub struct FilterCondition {
    pub(crate) is_list_filter: bool,
    pub(crate) apply_logical_or: bool,
    pub(crate) should_match_blanks: bool,
    pub(crate) list: Vec<FilterData>,
    pub(crate) custom1: Option<FilterData>,
    pub(crate) custom2: Option<FilterData>,
}

#[allow(clippy::new_without_default)]
impl FilterCondition {
    /// Create a new `FilterCondition` struct to define autofilter rules
    /// associated with an [`autofilter()`](crate::Worksheet::autofilter())
    /// range and the the [`filter_column`](crate::Worksheet::filter_column)
    /// method.
    ///
    /// See the examples above.
    ///
    pub fn new() -> FilterCondition {
        FilterCondition {
            is_list_filter: true,
            apply_logical_or: true,
            should_match_blanks: false,
            list: vec![],
            custom1: None,
            custom2: None,
        }
    }

    /// Add a list filter condition.
    ///
    /// Add a list filter to a column in an autofilter range. This method can be
    /// called multiple times to add multiple "equal to" filter conditions with
    /// a boolean "or". So in the example below the the equivalent Rust
    /// expression for the filter condition would be: `value == "East" || value
    /// == "West" || value == "South"`.
    ///
    /// # Parameters
    ///
    /// - `value`: The value can be a `&str`,`f64` or `i32` type for which the
    ///   [`IntoFilterData`] trait is implemented.
    ///
    /// # Examples
    ///
    /// The following example demonstrates setting an autofilter with multiple
    /// list filter conditions.
    ///
    /// ```
    /// # // This code is available in examples/doc_worksheet_filter_column2.rs
    /// #
    /// # use rust_xlsxwriter::{FilterCondition, Workbook, XlsxError};
    /// #
    /// # fn main() -> Result<(), XlsxError> {
    /// #     let mut workbook = Workbook::new();
    /// #
    /// #     // Add a worksheet with some sample data to filter.
    /// #     let worksheet = workbook.add_worksheet();
    /// #     worksheet.write_string(0, 0, "Region")?;
    /// #     worksheet.write_string(1, 0, "East")?;
    /// #     worksheet.write_string(2, 0, "West")?;
    /// #     worksheet.write_string(3, 0, "East")?;
    /// #     worksheet.write_string(4, 0, "North")?;
    /// #     worksheet.write_string(5, 0, "South")?;
    /// #     worksheet.write_string(6, 0, "West")?;
    /// #
    /// #     worksheet.write_string(0, 1, "Sales")?;
    /// #     worksheet.write_number(1, 1, 3000)?;
    /// #     worksheet.write_number(2, 1, 8000)?;
    /// #     worksheet.write_number(3, 1, 5000)?;
    /// #     worksheet.write_number(4, 1, 4000)?;
    /// #     worksheet.write_number(5, 1, 7000)?;
    /// #     worksheet.write_number(6, 1, 9000)?;
    /// #
    /// #     // Set the autofilter.
    /// #     worksheet.autofilter(0, 0, 6, 1)?;
    /// #
    ///     // Set a filter condition to only show cells matching "East", "West" or
    ///     // "South" in the first column.
    ///     let filter_condition = FilterCondition::new()
    ///         .add_list_filter("East")
    ///         .add_list_filter("West")
    ///         .add_list_filter("South");
    ///     worksheet.filter_column(0, &filter_condition)?;
    /// #
    /// #     workbook.save("worksheet.xlsx")?;
    /// #
    /// #     Ok(())
    /// # }
    /// ```
    ///
    /// Output file:
    ///
    /// <img
    /// src="https://rustxlsxwriter.github.io/images/worksheet_filter_column2.png">
    ///
    pub fn add_list_filter<T>(mut self, value: T) -> FilterCondition
    where
        T: IntoFilterData,
    {
        self.list
            .push(value.new_filter_data(FilterCriteria::EqualTo));
        self.is_list_filter = true;
        self
    }

    /// Add a list filter to filter on blanks.
    ///
    /// Add a filter condition to a list filter to show blank cells. For
    /// autofilters Excel treats empty or whitespace only cells as "Blank".
    ///
    /// Filtering non-blanks can be done in two ways. See the second example
    /// below.
    ///
    /// # Examples
    ///
    /// The following example demonstrates setting an autofilter with a list
    /// filter for blank cells.
    ///
    /// ```
    /// # // This code is available in examples/doc_worksheet_filter_column3.rs
    /// #
    /// # use rust_xlsxwriter::{FilterCondition, Workbook, XlsxError};
    /// #
    /// # fn main() -> Result<(), XlsxError> {
    /// #     let mut workbook = Workbook::new();
    /// #
    /// #     // Add a worksheet with some sample data to filter.
    /// #     let worksheet = workbook.add_worksheet();
    /// #     worksheet.write_string(0, 0, "Region")?;
    /// #     worksheet.write_string(1, 0, "")?;
    /// #     worksheet.write_string(2, 0, "West")?;
    /// #     worksheet.write_string(3, 0, "East")?;
    /// #     worksheet.write_string(4, 0, "North")?;
    /// #
    /// #     worksheet.write_string(6, 0, "West")?;
    /// #
    /// #     worksheet.write_string(0, 1, "Sales")?;
    /// #     worksheet.write_number(1, 1, 3000)?;
    /// #     worksheet.write_number(2, 1, 8000)?;
    /// #     worksheet.write_number(3, 1, 5000)?;
    /// #     worksheet.write_number(4, 1, 4000)?;
    /// #     worksheet.write_number(5, 1, 7000)?;
    /// #     worksheet.write_number(6, 1, 9000)?;
    /// #
    /// #     // Set the autofilter.
    /// #     worksheet.autofilter(0, 0, 6, 1)?;
    /// #
    ///     // Set a filter condition to only show cells matching blanks.
    ///     let filter_condition = FilterCondition::new().add_list_blanks_filter();
    ///
    ///     worksheet.filter_column(0, &filter_condition)?;
    /// #
    /// #     workbook.save("worksheet.xlsx")?;
    /// #
    /// #     Ok(())
    /// # }
    /// ```
    ///
    /// Output file:
    ///
    /// <img
    /// src="https://rustxlsxwriter.github.io/images/worksheet_filter_column3.png">
    ///
    ///
    /// The following example demonstrates does the opposite of the previous
    /// example, it sets an autofilter to show all the non-blank values in a
    /// column. This can be done in two ways: by adding a filter for each district
    /// string/number in the column or since that may be difficult to figure out
    /// programmatically you can set a custom filter. Excel uses both of these
    /// methods depending on the data being filtered.
    ///
    /// ```
    /// # // This code is available in examples/doc_worksheet_filter_column7.rs
    /// #
    /// # use rust_xlsxwriter::{FilterCondition, FilterCriteria, Workbook, XlsxError};
    /// #
    /// # fn main() -> Result<(), XlsxError> {
    /// #     let mut workbook = Workbook::new();
    /// #
    /// #     // Add a worksheet with some sample data to filter.
    /// #     let worksheet = workbook.add_worksheet();
    /// #     worksheet.write_string(0, 0, "Region")?;
    /// #     worksheet.write_string(1, 0, "")?;
    /// #     worksheet.write_string(2, 0, "West")?;
    /// #     worksheet.write_string(3, 0, "East")?;
    /// #     worksheet.write_string(4, 0, "")?;
    /// #     worksheet.write_string(5, 0, "")?;
    /// #     worksheet.write_string(6, 0, "West")?;
    /// #
    /// #     worksheet.write_string(0, 1, "Sales")?;
    /// #     worksheet.write_number(1, 1, 3000)?;
    /// #     worksheet.write_number(2, 1, 8000)?;
    /// #     worksheet.write_number(3, 1, 5000)?;
    /// #     worksheet.write_number(4, 1, 4000)?;
    /// #     worksheet.write_number(5, 1, 7000)?;
    /// #     worksheet.write_number(6, 1, 9000)?;
    /// #
    /// #     // Set the autofilter.
    /// #     worksheet.autofilter(0, 0, 6, 1)?;
    /// #
    ///     // Filter non-blanks by filtering on all the unique non-blank
    ///     // strings/numbers in the column.
    ///     let filter_condition = FilterCondition::new()
    ///         .add_list_filter("East")
    ///         .add_list_filter("West")
    ///         .add_list_filter("North")
    ///         .add_list_filter("South");
    ///     worksheet.filter_column(0, &filter_condition)?;
    ///
    ///     // Or you can add a simpler custom filter to get the same result.
    ///
    ///     // Set a custom number filter of `!= " "` to filter non blanks.
    ///     let filter_condition =
    ///         FilterCondition::new().add_custom_filter(FilterCriteria::NotEqualTo, " ");
    ///     worksheet.filter_column(0, &filter_condition)?;
    /// #
    /// #     workbook.save("worksheet.xlsx")?;
    /// #
    /// #     Ok(())
    /// # }
    /// ```
    ///
    /// Output file:
    ///
    /// <img
    /// src="https://rustxlsxwriter.github.io/images/worksheet_filter_column7.png">
    ///
    pub fn add_list_blanks_filter(mut self) -> FilterCondition {
        self.should_match_blanks = true;
        self.is_list_filter = true;
        self
    }

    /// Add a custom filter condition.
    ///
    /// Add a custom filter to a column in an autofilter range. Excel only
    /// allows two custom conditions, so this method can only be called twice.
    ///
    /// When two conditions are specified, like the example below, the logical
    /// operator defaults to "and", as in Excel. However, you can use the
    /// [`add_custom_boolean_or`](FilterCondition::add_custom_boolean_or) method
    /// below to get an "or" logical condition.
    ///
    /// # Parameters
    ///
    /// - `value`: The value can be a `&str`,`f64` or `i32` type for which the
    ///   [`IntoFilterData`] trait is implemented.
    /// - `criteria`: The criteria/operator to use in the filter as defined by
    ///   the [`FilterCriteria`] struct.
    ///
    /// # Examples
    ///
    /// The following example demonstrates setting an autofilter for two custom
    /// number filters to create a "between" condition.
    ///
    /// ```
    /// # // This code is available in examples/doc_worksheet_filter_column6.rs
    /// #
    /// # use rust_xlsxwriter::{FilterCondition, FilterCriteria, Workbook, XlsxError};
    /// #
    /// # fn main() -> Result<(), XlsxError> {
    /// #     let mut workbook = Workbook::new();
    /// #
    /// #     // Add a worksheet with some sample data to filter.
    /// #     let worksheet = workbook.add_worksheet();
    /// #     worksheet.write_string(0, 0, "Region")?;
    /// #     worksheet.write_string(1, 0, "East")?;
    /// #     worksheet.write_string(2, 0, "West")?;
    /// #     worksheet.write_string(3, 0, "East")?;
    /// #     worksheet.write_string(4, 0, "North")?;
    /// #     worksheet.write_string(5, 0, "South")?;
    /// #     worksheet.write_string(6, 0, "West")?;
    /// #
    /// #     worksheet.write_string(0, 1, "Sales")?;
    /// #     worksheet.write_number(1, 1, 3000)?;
    /// #     worksheet.write_number(2, 1, 8000)?;
    /// #     worksheet.write_number(3, 1, 5000)?;
    /// #     worksheet.write_number(4, 1, 4000)?;
    /// #     worksheet.write_number(5, 1, 7000)?;
    /// #     worksheet.write_number(6, 1, 9000)?;
    /// #
    /// #     // Set the autofilter.
    /// #     worksheet.autofilter(0, 0, 6, 1)?;
    /// #
    ///     // Set two custom number filters in a "between" configuration.
    ///     let filter_condition = FilterCondition::new()
    ///         .add_custom_filter(FilterCriteria::GreaterThanOrEqualTo, 4000)
    ///         .add_custom_filter(FilterCriteria::LessThanOrEqualTo, 8000);
    ///     worksheet.filter_column(1, &filter_condition)?;
    /// #
    /// #     workbook.save("worksheet.xlsx")?;
    /// #
    /// #     Ok(())
    /// # }
    /// ```
    ///
    /// Output file:
    ///
    /// <img
    /// src="https://rustxlsxwriter.github.io/images/worksheet_filter_column6.png">
    ///
    pub fn add_custom_filter<T>(mut self, criteria: FilterCriteria, value: T) -> FilterCondition
    where
        T: IntoFilterData,
    {
        if self.custom1.is_none() {
            self.custom1 = Some(value.new_filter_data(criteria));
        } else if self.custom2.is_none() {
            self.custom2 = Some(value.new_filter_data(criteria));
            self.apply_logical_or = false;
        } else {
            eprintln!("Excel only allows 2 custom filter conditions.");
        }

        self.is_list_filter = false;
        self
    }

    /// Add an "or" logical condition for two custom filters.
    ///
    /// When two conditions are specified, like the example above, the logical
    /// operator defaults to "and", as in Excel. However, you can use the
    /// [`add_custom_boolean_or`](FilterCondition::add_custom_boolean_or) method
    /// to get an "or" logical condition.
    ///
    pub fn add_custom_boolean_or(mut self) -> FilterCondition {
        self.apply_logical_or = true;
        self.is_list_filter = false;
        self
    }
}

/// The `FilterCriteria` enum defines logical filter criteria used in an
/// autofilter.
///
/// These filter criteria are used with the [`FilterCondition`]
/// [`add_custom_filter()`](FilterCondition::add_custom_filter) method.
///
/// Currently only Excel's string and number filter operations are supported.
/// The numeric style criteria such as `>=` can also be applied to strings (like
/// in Rust) but the string operations like `BeginsWith` are only applied to
/// strings in Excel.
///
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum FilterCriteria {
    /// Show numbers or strings that are equal to the filter value.
    EqualTo,

    /// Show numbers or strings that are not equal to the filter value.
    NotEqualTo,

    /// Show numbers or strings that are greater than the filter value.
    GreaterThan,

    /// Show numbers or strings that are greater than or equal to the filter value.
    GreaterThanOrEqualTo,

    /// Show numbers or strings that are less than the filter value.
    LessThan,

    /// Show numbers or strings that are less than or equal to the filter value.
    LessThanOrEqualTo,

    /// Show strings that begin with the filter string value.
    BeginsWith,

    /// Show strings that do not begin with the filter string value.
    DoesNotBeginWith,

    /// Show strings that end with the filter string value.
    EndsWith,

    /// Show strings that do not end with the filter string value.
    DoesNotEndWith,

    /// Show strings that contain with the filter string value.
    Contains,

    /// Show strings that do not contain with the filter string value.
    DoesNotContain,
}

#[allow(clippy::match_same_arms)]
impl FilterCriteria {
    pub(crate) fn operator(self) -> String {
        match self {
            FilterCriteria::EqualTo => String::new(),
            FilterCriteria::LessThan => "lessThan".to_string(),
            FilterCriteria::NotEqualTo => "notEqual".to_string(),
            FilterCriteria::GreaterThan => "greaterThan".to_string(),
            FilterCriteria::LessThanOrEqualTo => "lessThanOrEqual".to_string(),
            FilterCriteria::GreaterThanOrEqualTo => "greaterThanOrEqual".to_string(),
            FilterCriteria::EndsWith => String::new(),
            FilterCriteria::Contains => String::new(),
            FilterCriteria::BeginsWith => String::new(),
            FilterCriteria::DoesNotEndWith => "notEqual".to_string(),
            FilterCriteria::DoesNotContain => "notEqual".to_string(),
            FilterCriteria::DoesNotBeginWith => "notEqual".to_string(),
        }
    }
}

/// The `FilterData` struct represents data types used in Excel's filters.
///
/// The `FilterData` struct is a simple data type to allow a generic mapping
/// between Rust's string and number types and similar types used in Excel's
/// filters.
#[derive(Clone)]
pub struct FilterData {
    pub(crate) data_type: FilterDataType,
    pub(crate) string: String,
    pub(crate) number: f64,
    pub(crate) criteria: FilterCriteria,
}

impl FilterData {
    /// Function to turn a string reference and [`FilterCriteria`] into a
    /// [`FilterData`] instance.
    ///
    /// This is used in conjunction with the [`IntoFilterData`] trait.
    ///
    /// # Parameters
    ///
    /// - `value`: The value as a string ref.
    /// - `criteria`: The criteria/operator to use in the filter as defined by
    ///   the [`FilterCriteria`] struct.
    ///
    pub fn new_string_and_criteria(value: &str, criteria: FilterCriteria) -> FilterData {
        FilterData {
            data_type: FilterDataType::String,
            string: value.to_string(),
            number: 0.0,
            criteria,
        }
    }

    /// Function to turn a [`f64`] value and [`FilterCriteria`] into a
    /// [`FilterData`] instance.
    ///
    /// This is used in conjunction with the [`IntoFilterData`] trait.
    ///
    /// # Parameters
    ///
    /// - `value`: The `f64` number value.
    /// - `criteria`: The criteria/operator to use in the filter as defined by
    ///   the [`FilterCriteria`] struct.
    ///
    pub fn new_number_and_criteria(value: f64, criteria: FilterCriteria) -> FilterData {
        // Store number but also convert it to a string since Excel makes string
        // comparisons to "numbers stored as strings".
        FilterData {
            data_type: FilterDataType::Number,
            string: value.to_string(),
            number: value,
            criteria,
        }
    }

    // Excel stores some of the string operators as simple regex patterns.
    pub(crate) fn value(&self) -> String {
        match self.criteria {
            FilterCriteria::EndsWith | FilterCriteria::DoesNotEndWith => {
                format!("*{}", self.string)
            }
            FilterCriteria::Contains | FilterCriteria::DoesNotContain => {
                format!("*{}*", self.string)
            }
            FilterCriteria::BeginsWith | FilterCriteria::DoesNotBeginWith => {
                format!("{}*", self.string)
            }
            // For everything else, including numbers, we just use the string value.
            _ => self.string.clone(),
        }
    }
}

/// Trait to map different Rust types into Excel data types used in filters.
///
/// Currently only string and number like types are supported.
///
/// See the [`FilterData::new_string_and_criteria()`] and
/// [`FilterData::new_number_and_criteria()`] functions for constructing
///
pub trait IntoFilterData {
    /// Types/objects supporting this trait must be able to convert to
    /// [`FilterData`] instances in `new_filter_data()`. `FilterData` struct.
    ///
    fn new_filter_data(&self, criteria: FilterCriteria) -> FilterData;
}

impl IntoFilterData for f64 {
    fn new_filter_data(&self, criteria: FilterCriteria) -> FilterData {
        FilterData::new_number_and_criteria(*self, criteria)
    }
}

impl IntoFilterData for i32 {
    fn new_filter_data(&self, criteria: FilterCriteria) -> FilterData {
        FilterData::new_number_and_criteria(f64::from(*self), criteria)
    }
}

impl IntoFilterData for &str {
    fn new_filter_data(&self, criteria: FilterCriteria) -> FilterData {
        FilterData::new_string_and_criteria(self, criteria)
    }
}

#[derive(Clone, PartialEq, Eq)]
pub(crate) enum FilterDataType {
    String,
    Number,
}