edit_xlsx/api/format/fill.rs
1//!
2//! This module contains the [`FormatFill`] struct, which used to set the fill of a format.
3//! [`FormatFill`] is used for fields that having both foreground color and background color,
4//! mainly applied in the [`Row`], [`Column`] and [`Cell`]'s [`Format`] fill color.
5//! # Examples
6//!
7//! Use [`FormatFill`] to fill columns' color
8//! ```
9//! use edit_xlsx::{Format, FormatColor, FormatFill, Workbook, WorkSheetCol, Write};
10//! let mut workbook = Workbook::from_path("./examples/xlsx/accounting.xlsx").unwrap();
11//! let worksheet = workbook.get_worksheet_mut_by_name("worksheet").unwrap();
12//! let mut white = Format::default();
13//! white.fill.fg_color = FormatColor::RGB(255, 255, 255);
14//! white.fill.bg_color = FormatColor::RGB(0, 0, 200);
15//! white.fill.pattern_type = "lightGrid".to_string();
16//! worksheet.set_columns_width_with_format("A:XFD", 8.12, &white).unwrap();
17//! workbook.save_as("./examples/fill_fill_columns_color.xlsx").unwrap();
18//! ```
19//!
20//! Use [`FormatFill`] to fill row's color
21//!
22//! ```
23//! use edit_xlsx::{Format, FormatColor, FormatFill, Row, Workbook, WorkSheetCol, WorkSheetRow, Write};
24//! let mut workbook = Workbook::from_path("./examples/xlsx/accounting.xlsx").unwrap();
25//! let worksheet = workbook.get_worksheet_mut_by_name("worksheet").unwrap();
26//! let mut white = Format::default();
27//! white.fill.fg_color = FormatColor::RGB(255, 255, 255);
28//! white.fill.bg_color = FormatColor::RGB(200, 0, 0);
29//! white.fill.pattern_type = "lightGrid".to_string();
30//! worksheet.set_row_height_with_format(1, 15.0, &white).unwrap();
31//! worksheet.set_row_height_with_format(2, 15.0, &white).unwrap();
32//! worksheet.set_row_height_with_format(3, 15.0, &white).unwrap();
33//! workbook.save_as("./examples/fill_fill_rows_color.xlsx").unwrap();
34//! ```
35//!
36//! Use [`FormatFill`] to fill cell's color
37//!
38//! ```
39//! use edit_xlsx::{Format, FormatColor, FormatFill, Workbook, Write};
40//! let mut workbook = Workbook::from_path("./examples/xlsx/accounting.xlsx").unwrap();
41//! let worksheet = workbook.get_worksheet_mut_by_name("worksheet").unwrap();
42//! let mut yellow = Format::default();
43//! yellow.fill.fg_color = FormatColor::RGB(255, 255, 0);
44//! yellow.fill.bg_color = FormatColor::RGB(0, 255, 0);
45//! yellow.fill.pattern_type = "lightGrid".to_string();
46//! worksheet.write_with_format("B6", "Yellow fg and green bg", &yellow).unwrap();
47//! workbook.save_as("./examples/fill_fill_cells_color.xlsx").unwrap();
48//! ```
49
50
51use crate::FormatColor;
52use crate::xml::common::FromFormat;
53use crate::xml::style::color::Color;
54use crate::xml::style::fill::Fill;
55use crate::{Row, Column, Cell, Format};
56
57///
58/// [`FormatFill`] is used for fields that having both foreground color and background color,
59/// It's mainly applied in the [`Row`], [`Column`] and [`Cell`]'s [`Format`] fill color.
60/// # Fields
61/// | field | type | meaning |
62/// | ------------ | ----------- | ------------------------------------------------------------ |
63/// | `pattern_type` | [`String`] | The color filling method, the contents of which can be referred to the [official documentation](https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.patternvalues?view=openxml-3.0.1) |
64/// | `fg_color` | [`FormatColor`] | The foreground color of filling |
65/// | `bg_color` | [`FormatColor`] | The background color of filling |
66///
67#[derive(Clone, Debug, PartialEq)]
68pub struct FormatFill {
69 pub pattern_type: String,
70 pub fg_color: FormatColor,
71 pub bg_color: FormatColor
72}
73
74impl Default for FormatFill {
75 ///
76 /// Method [`FormatFill::default()`] creates a blank [`FormatFill`] with no foreground color,
77 /// a blank background color, and a `none` pattern_type.
78 ///
79 /// When using [`Format::default()`], its `fill` field will be filled by this method.
80 fn default() -> Self {
81 FormatFill {
82 pattern_type: "none".to_string(),
83 fg_color: FormatColor::default(),
84 bg_color: FormatColor::Index(65),
85 }
86 }
87}
88
89impl FromFormat<FormatFill> for Fill {
90 fn set_attrs_by_format(&mut self, format: &FormatFill) {
91 self.pattern_fill.fg_color = Color::from_format(&format.fg_color);
92 self.pattern_fill.bg_color = Color::from_format(&format.bg_color);
93 self.pattern_fill.pattern_type = String::from(&format.pattern_type);
94 }
95
96 fn set_format(&self, format: &mut FormatFill) {
97 format.fg_color = self.pattern_fill.fg_color.get_format();
98 format.bg_color = self.pattern_fill.bg_color.get_format();
99 format.pattern_type = self.pattern_fill.pattern_type.to_string();
100 }
101}