xlsxwriter 0.6.1

Write xlsx file with number, formula, string, formatting, autofilter, merged cells, data validation and more.
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
use super::{set_max_value, set_min_value, set_value, ConditionalFormat, ConditionalFormatTypes};
use crate::{CStringHelper, Format, StringOrFloat, XlsxError};

/// The Cell type is the most common conditional formatting type. It is used when a format is
/// applied to a cell based on a simple criterion.
///
/// See [`ConditionalFormat`] to learn more.
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum ConditionalFormatCellCriteria {
    /// Format cells equal to a value.
    /// See usage at [`ConditionalFormat::cell_equal_to`]
    EqualTo(StringOrFloat),
    /// Format cells not equal to a value.
    /// See usage at [`ConditionalFormat::cell_not_equal_to`]
    NotEqualTo(StringOrFloat),
    /// Format cells greater than a value.
    GreaterThan(StringOrFloat),
    /// Format cells less than a value.
    LessThan(StringOrFloat),
    /// Format cells greater than or equal to a value.
    GreaterThanOrEqualTo(StringOrFloat),
    /// Format cells less than or equal to a value.
    LessThanOrEqualTo(StringOrFloat),
    /// Format cells between two values.
    Between {
        min: StringOrFloat,
        max: StringOrFloat,
    },
    /// Format cells that is not between two values.
    NotBetween {
        min: StringOrFloat,
        max: StringOrFloat,
    },
}

impl ConditionalFormatCellCriteria {
    pub(crate) fn to_internal_value(
        &self,
        c_string_helper: &mut CStringHelper,
        conditional_format: &mut libxlsxwriter_sys::lxw_conditional_format,
    ) -> Result<(), XlsxError> {
        conditional_format.type_ =
            libxlsxwriter_sys::lxw_conditional_format_types_LXW_CONDITIONAL_TYPE_CELL as u8;
        match self {
            ConditionalFormatCellCriteria::EqualTo(val) => {
                conditional_format.criteria =
                    libxlsxwriter_sys::lxw_conditional_criteria_LXW_CONDITIONAL_CRITERIA_EQUAL_TO
                        as u8;
                set_value(conditional_format, val, c_string_helper)?;
            }
            ConditionalFormatCellCriteria::NotEqualTo(val) => {
                conditional_format.criteria =
                libxlsxwriter_sys::lxw_conditional_criteria_LXW_CONDITIONAL_CRITERIA_NOT_EQUAL_TO
                    as u8;
                set_value(conditional_format, val, c_string_helper)?;
            }
            ConditionalFormatCellCriteria::GreaterThan(val) => {
                conditional_format.criteria =
                libxlsxwriter_sys::lxw_conditional_criteria_LXW_CONDITIONAL_CRITERIA_GREATER_THAN
                    as u8;
                set_value(conditional_format, val, c_string_helper)?;
            }
            ConditionalFormatCellCriteria::LessThan(val) => {
                conditional_format.criteria =
                    libxlsxwriter_sys::lxw_conditional_criteria_LXW_CONDITIONAL_CRITERIA_LESS_THAN
                        as u8;
                set_value(conditional_format, val, c_string_helper)?;
            }
            ConditionalFormatCellCriteria::GreaterThanOrEqualTo(val) => {
                conditional_format.criteria =
                libxlsxwriter_sys::lxw_conditional_criteria_LXW_CONDITIONAL_CRITERIA_GREATER_THAN_OR_EQUAL_TO
                    as u8;
                set_value(conditional_format, val, c_string_helper)?;
            }
            ConditionalFormatCellCriteria::LessThanOrEqualTo(val) => {
                conditional_format.criteria =
                libxlsxwriter_sys::lxw_conditional_criteria_LXW_CONDITIONAL_CRITERIA_LESS_THAN_OR_EQUAL_TO
                    as u8;
                set_value(conditional_format, val, c_string_helper)?;
            }
            ConditionalFormatCellCriteria::Between {
                min: min_val,
                max: max_val,
            } => {
                conditional_format.criteria =
                    libxlsxwriter_sys::lxw_conditional_criteria_LXW_CONDITIONAL_CRITERIA_BETWEEN
                        as u8;
                set_min_value(conditional_format, min_val, c_string_helper)?;
                set_max_value(conditional_format, max_val, c_string_helper)?;
            }
            ConditionalFormatCellCriteria::NotBetween {
                min: min_val,
                max: max_val,
            } => {
                conditional_format.criteria =
                    libxlsxwriter_sys::lxw_conditional_criteria_LXW_CONDITIONAL_CRITERIA_NOT_BETWEEN
                        as u8;
                set_min_value(conditional_format, min_val, c_string_helper)?;
                set_max_value(conditional_format, max_val, c_string_helper)?;
            }
        };
        Ok(())
    }
}

impl ConditionalFormat {
    /// Format cells equal to a value.
    ///
    /// ```rust
    /// # use xlsxwriter::prelude::*;
    /// # use xlsxwriter::worksheet::conditional_format::*;
    /// # fn main() -> Result<(), XlsxError> {
    /// # let workbook = Workbook::new("test-conditional_format-cell_equal_to.xlsx")?;
    /// # let mut worksheet = workbook.add_worksheet(None)?;
    /// # for i in 1..=10 {
    /// # worksheet.write_number(i, 0, i.into(), None)?;
    /// # }
    /// # worksheet.write_string(0, 0, "Equal to 3", None)?;
    /// worksheet.conditional_format_range(
    ///     1, 0, 10, 0,
    ///     &ConditionalFormat::cell_equal_to(3.0, Format::new().set_bg_color(FormatColor::Yellow)),
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn cell_equal_to<V: Into<StringOrFloat>>(value: V, format: &Format) -> ConditionalFormat {
        ConditionalFormat::ConditionType {
            criteria: ConditionalFormatTypes::Cell(ConditionalFormatCellCriteria::EqualTo(
                value.into(),
            )),
            format: format.clone(),
        }
    }

    /// Format cells not equal to a value.
    ///
    /// ```rust
    /// # use xlsxwriter::prelude::*;
    /// # use xlsxwriter::worksheet::conditional_format::*;
    /// # fn main() -> Result<(), XlsxError> {
    /// # let workbook = Workbook::new("test-conditional_format-cell_not_equal_to.xlsx")?;
    /// # let mut worksheet = workbook.add_worksheet(None)?;
    /// # for i in 1..=10 {
    /// # worksheet.write_number(i, 0, i.into(), None)?;
    /// # }
    /// # worksheet.write_string(0, 0, "Not equal to 3", None)?;
    /// worksheet.conditional_format_range(
    ///     1, 0, 10, 0,
    ///     &ConditionalFormat::cell_not_equal_to(3.0, Format::new().set_bg_color(FormatColor::Yellow)),
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn cell_not_equal_to<V: Into<StringOrFloat>>(
        value: V,
        format: &Format,
    ) -> ConditionalFormat {
        ConditionalFormat::ConditionType {
            criteria: ConditionalFormatTypes::Cell(ConditionalFormatCellCriteria::NotEqualTo(
                value.into(),
            )),
            format: format.clone(),
        }
    }

    /// Format cells greater than a value.
    ///
    /// ```rust
    /// # use xlsxwriter::prelude::*;
    /// # use xlsxwriter::worksheet::conditional_format::*;
    /// # fn main() -> Result<(), XlsxError> {
    /// # let workbook = Workbook::new("test-conditional_format-cell_greater_than.xlsx")?;
    /// # let mut worksheet = workbook.add_worksheet(None)?;
    /// # for i in 1..=10 {
    /// # worksheet.write_number(i, 0, i.into(), None)?;
    /// # }
    /// # worksheet.write_string(0, 0, "Greater than 3", None)?;
    /// worksheet.conditional_format_range(
    ///     1, 0, 10, 0,
    ///     &ConditionalFormat::cell_greater_than(3.0, Format::new().set_bg_color(FormatColor::Yellow)),
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn cell_greater_than<V: Into<StringOrFloat>>(
        value: V,
        format: &Format,
    ) -> ConditionalFormat {
        ConditionalFormat::ConditionType {
            criteria: ConditionalFormatTypes::Cell(ConditionalFormatCellCriteria::GreaterThan(
                value.into(),
            )),
            format: format.clone(),
        }
    }

    /// Format cells less than a value.
    ///
    /// ```rust
    /// # use xlsxwriter::prelude::*;
    /// # use xlsxwriter::worksheet::conditional_format::*;
    /// # fn main() -> Result<(), XlsxError> {
    /// # let workbook = Workbook::new("test-conditional_format-cell_less_than.xlsx")?;
    /// # let mut worksheet = workbook.add_worksheet(None)?;
    /// # for i in 1..=10 {
    /// # worksheet.write_number(i, 0, i.into(), None)?;
    /// # }
    /// # worksheet.write_string(0, 0, "Less than 3", None)?;
    /// worksheet.conditional_format_range(
    ///     1, 0, 10, 0,
    ///     &ConditionalFormat::cell_less_than(3.0, Format::new().set_bg_color(FormatColor::Yellow)),
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn cell_less_than<V: Into<StringOrFloat>>(value: V, format: &Format) -> ConditionalFormat {
        ConditionalFormat::ConditionType {
            criteria: ConditionalFormatTypes::Cell(ConditionalFormatCellCriteria::LessThan(
                value.into(),
            )),
            format: format.clone(),
        }
    }

    /// Format cells greater than or equal to a value.
    ///
    /// ```rust
    /// # use xlsxwriter::prelude::*;
    /// # use xlsxwriter::worksheet::conditional_format::*;
    /// # fn main() -> Result<(), XlsxError> {
    /// # let workbook = Workbook::new("test-conditional_format-cell_greater_than_or_equal_to.xlsx")?;
    /// # let mut worksheet = workbook.add_worksheet(None)?;
    /// # for i in 1..=10 {
    /// # worksheet.write_number(i, 0, i.into(), None)?;
    /// # }
    /// # worksheet.write_string(0, 0, "Greater than or equal to 3", None)?;
    /// worksheet.conditional_format_range(
    ///     1, 0, 10, 0,
    ///     &ConditionalFormat::cell_greater_than_or_equal_to(3.0, Format::new().set_bg_color(FormatColor::Yellow)),
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn cell_greater_than_or_equal_to<V: Into<StringOrFloat>>(
        value: V,
        format: &Format,
    ) -> ConditionalFormat {
        ConditionalFormat::ConditionType {
            criteria: ConditionalFormatTypes::Cell(
                ConditionalFormatCellCriteria::GreaterThanOrEqualTo(value.into()),
            ),
            format: format.clone(),
        }
    }

    /// Format cells less than or equal to a value.
    ///
    /// ```rust
    /// # use xlsxwriter::prelude::*;
    /// # use xlsxwriter::worksheet::conditional_format::*;
    /// # fn main() -> Result<(), XlsxError> {
    /// # let workbook = Workbook::new("test-conditional_format-cell_less_than_or_equal_to.xlsx")?;
    /// # let mut worksheet = workbook.add_worksheet(None)?;
    /// # for i in 1..=10 {
    /// # worksheet.write_number(i, 0, i.into(), None)?;
    /// # }
    /// # worksheet.write_string(0, 0, "Less than or equal to 3", None)?;
    /// worksheet.conditional_format_range(
    ///     1, 0, 10, 0,
    ///     &ConditionalFormat::cell_less_than_or_equal_to(3.0, Format::new().set_bg_color(FormatColor::Yellow)),
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn cell_less_than_or_equal_to<V: Into<StringOrFloat>>(
        value: V,
        format: &Format,
    ) -> ConditionalFormat {
        ConditionalFormat::ConditionType {
            criteria: ConditionalFormatTypes::Cell(
                ConditionalFormatCellCriteria::LessThanOrEqualTo(value.into()),
            ),
            format: format.clone(),
        }
    }

    /// Format cells between two values.
    ///
    /// ```rust
    /// # use xlsxwriter::prelude::*;
    /// # use xlsxwriter::worksheet::conditional_format::*;
    /// # fn main() -> Result<(), XlsxError> {
    /// # let workbook = Workbook::new("test-conditional_format-cell_between.xlsx")?;
    /// # let mut worksheet = workbook.add_worksheet(None)?;
    /// # for i in 1..=10 {
    /// # worksheet.write_number(i, 0, i.into(), None)?;
    /// # }
    /// # worksheet.write_string(0, 0, "Between from 3 to 5", None)?;
    /// worksheet.conditional_format_range(
    ///     1, 0, 10, 0,
    ///     &ConditionalFormat::cell_between(3.0, 5.0, Format::new().set_bg_color(FormatColor::Yellow)),
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn cell_between<V1: Into<StringOrFloat>, V2: Into<StringOrFloat>>(
        min_value: V1,
        max_value: V2,
        format: &Format,
    ) -> ConditionalFormat {
        ConditionalFormat::ConditionType {
            criteria: ConditionalFormatTypes::Cell(ConditionalFormatCellCriteria::Between {
                min: min_value.into(),
                max: max_value.into(),
            }),
            format: format.clone(),
        }
    }

    /// Format cells not between two values.
    ///
    /// ```rust
    /// # use xlsxwriter::prelude::*;
    /// # use xlsxwriter::worksheet::conditional_format::*;
    /// # fn main() -> Result<(), XlsxError> {
    /// # let workbook = Workbook::new("test-conditional_format-cell_not_between.xlsx")?;
    /// # let mut worksheet = workbook.add_worksheet(None)?;
    /// # for i in 1..=10 {
    /// # worksheet.write_number(i, 0, i.into(), None)?;
    /// # }
    /// # worksheet.write_string(0, 0, "Not Between from 3 to 5", None)?;
    /// worksheet.conditional_format_range(
    ///     1, 0, 10, 0,
    ///     &ConditionalFormat::cell_not_between(3.0, 5.0, Format::new().set_bg_color(FormatColor::Yellow)),
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn cell_not_between<V1: Into<StringOrFloat>, V2: Into<StringOrFloat>>(
        min_value: V1,
        max_value: V2,
        format: &Format,
    ) -> ConditionalFormat {
        ConditionalFormat::ConditionType {
            criteria: ConditionalFormatTypes::Cell(ConditionalFormatCellCriteria::NotBetween {
                min: min_value.into(),
                max: max_value.into(),
            }),
            format: format.clone(),
        }
    }
}

#[cfg(test)]
mod test {
    use crate::worksheet::conditional_format::*;
    use crate::*;

    #[test]
    fn test_worksheet_conditional_format1() -> Result<(), XlsxError> {
        let workbook = Workbook::new("test-worksheet_conditional-format1.xlsx")?;
        let mut worksheet = workbook.add_worksheet(Some("Conditional format"))?;

        for i in 0..=10 {
            for j in 0..=10 {
                worksheet.write_number(i + 1, j, i.into(), None)?;
            }
        }

        worksheet.write_string(0, 0, "Equal to \"3\"", None)?;
        worksheet.conditional_format_range(
            1,
            0,
            11,
            0,
            &ConditionalFormat::cell_equal_to(3.0, Format::new().set_bg_color(FormatColor::Yellow)),
        )?;

        worksheet.write_string(0, 1, "Not Equal to \"3\"", None)?;
        worksheet.conditional_format_range(
            1,
            1,
            11,
            1,
            &ConditionalFormat::cell_not_equal_to(
                3.0,
                Format::new().set_bg_color(FormatColor::Yellow),
            ),
        )?;

        worksheet.write_string(0, 2, "Greater than \"3\"", None)?;
        worksheet.conditional_format_range(
            1,
            2,
            11,
            2,
            &ConditionalFormat::cell_greater_than(
                3.0,
                Format::new().set_bg_color(FormatColor::Yellow),
            ),
        )?;

        worksheet.write_string(0, 3, "Less than \"3\"", None)?;
        worksheet.conditional_format_range(
            1,
            3,
            11,
            3,
            &ConditionalFormat::cell_less_than(
                3.0,
                Format::new().set_bg_color(FormatColor::Yellow),
            ),
        )?;

        // Cell Type

        workbook.close()?;
        Ok(())
    }
}