edit_xlsx/api/format/align.rs
1//!
2//! This module contains the [`FormatAlign`] struct, which used to edit the align style of the [`Cell`],
3//! including set vertical and horizontal align, reading order and indent.
4//!
5//! # Examples
6//!
7//! Set vertical and horizontal align
8//! ```
9//! use edit_xlsx::{Cell, Format, FormatAlign, FormatAlignType, Workbook, Write};
10//! // Set cell text alignment to top left
11//! let format = Format::default()
12//! .set_align(FormatAlignType::Top)
13//! .set_align(FormatAlignType::Left);
14//! // Or use
15//! // let mut format = Format::default();
16//! // let mut format_align = FormatAlign::default();
17//! // format_align.vertical = Some(FormatAlignType::Top);
18//! // format_align.horizontal = Some(FormatAlignType::Left);
19//! // format.align = format_align;
20//! let mut workbook = Workbook::from_path("./examples/xlsx/accounting.xlsx").unwrap();
21//! let worksheet = workbook.get_worksheet_mut_by_name("worksheet").unwrap();
22//! for row in 6..=15 {
23//! for col in 3..=12 {
24//! worksheet.write_with_format((row, col), 20.0, &format).unwrap()
25//! }
26//! }
27//! workbook.save_as("./examples/align.xlsx").unwrap();
28//! ```
29//!
30//! Set reading order
31//! ```
32//! use edit_xlsx::{Cell, Format, FormatAlign, FormatAlignType, Workbook, Write};
33//! let format = Format::default()
34//! .set_reading_order(2);
35//! let mut workbook = Workbook::from_path("./examples/xlsx/accounting.xlsx").unwrap();
36//! let worksheet = workbook.get_worksheet_mut_by_name("worksheet").unwrap();
37//! for row in 6..=15 {
38//! for col in 3..=12 {
39//! worksheet.write_with_format((row, col), "نص عربي / English text", &format).unwrap();
40//! }
41//! }
42//! workbook.save_as("./examples/reading_order.xlsx").unwrap();
43//! ```
44//!
45//! Set indent
46//! ```
47//! use edit_xlsx::{Cell, Format, FormatAlign, FormatAlignType, Workbook, Write};
48//! let mut format = Format::default()
49//! .set_align(FormatAlignType::Top)
50//! .set_align(FormatAlignType::Left);
51//! let mut workbook = Workbook::from_path("./examples/xlsx/accounting.xlsx").unwrap();
52//! let worksheet = workbook.get_worksheet_mut_by_name("worksheet").unwrap();
53//! for col in 3..=12 {
54//! let mut indent = 0;
55//! for row in 6..=15 {
56//! let format = format.clone().set_indent(indent);
57//! worksheet.write_with_format((row, col), 10.0, &format).unwrap();
58//! indent += 1;
59//! }
60//! }
61//! workbook.save_as("./examples/indent.xlsx").unwrap();
62//! ```
63use crate::Cell;
64use crate::xml::common::FromFormat;
65
66///
67/// [`FormatAlign`] struct, which used to edit the align style of the [`Cell`],
68/// including set vertical and horizontal align(by [`FormatAlignType`]), reading order and indent.
69/// # Fields
70/// | field | type | meaning |
71/// | ------------ | ----------- | ------------------------------------------------------------ |
72/// | `horizontal` | [`Option<FormatAlignType>`] | The [`Cell`]'s horizontal alignment.<br> It's Some value can be [`FormatAlignType`] Left, Right, Center |
73/// | `vertical` | [`Option<FormatAlignType>`] | The [`Cell`]'s vertical alignment.<br> It's Some value can be [`FormatAlignType`] Top, Bottom, VerticalCenter |
74/// | `reading_order` | u8 | The [`Cell`]'s reading order<br>1: left to right<br>2: right to left|
75/// | `indent` | u8 | The [`Cell`]'s indent |
76#[derive(Clone, Debug, PartialEq)]
77pub struct FormatAlign {
78 pub horizontal: Option<FormatAlignType>,
79 pub vertical: Option<FormatAlignType>,
80 pub reading_order: u8,
81 pub indent: u8,
82}
83
84impl Default for FormatAlign {
85 fn default() -> Self {
86 Self {
87 horizontal: None,
88 vertical: None,
89 reading_order: 1,
90 indent: 0,
91 }
92 }
93}
94
95/// [`FormatAlignType`] determines the position of the alignment.
96///
97/// # Fields:
98/// | unit | meaning|
99/// | ---- | ---- |
100/// | `Top` |top-align|
101/// | `Center` |horizontal center-align|
102/// | `Bottom` |bottom-align|
103/// | `Left` |left-align|
104/// | `VerticalCenter` |vertical center-align|
105/// | `Right` |right-align|
106#[derive(Copy, Clone, Debug, PartialEq)]
107pub enum FormatAlignType {
108 Top,
109 Center,
110 Bottom,
111 Left,
112 VerticalCenter,
113 Right,
114}
115
116impl Default for FormatAlignType {
117 fn default() -> Self {
118 FormatAlignType::Center
119 }
120}
121
122impl FormatAlignType {
123 pub(crate) fn to_str(&self) -> &str {
124 match self {
125 FormatAlignType::Top => "top",
126 FormatAlignType::Center => "center",
127 FormatAlignType::Bottom => "bottom",
128 FormatAlignType::Left => "left",
129 FormatAlignType::VerticalCenter => "center",
130 FormatAlignType::Right => "right",
131 }
132 }
133
134 pub(crate) fn from_str(format_align_type: Option<&String>, is_horizontal: bool) -> Option<FormatAlignType> {
135 if let Some(format_align_type) = format_align_type {
136 let format_align_type = format_align_type.as_str();
137 Some(match format_align_type {
138 "top" => FormatAlignType::Top,
139 "center" => if is_horizontal { FormatAlignType::Center } else { FormatAlignType::VerticalCenter },
140 "bottom" => FormatAlignType::Bottom,
141 "left" => FormatAlignType::Left,
142 "right" => FormatAlignType::Right,
143 _ => FormatAlignType::default()
144 })
145 } else {
146 None
147 }
148 }
149}
150
151impl FromFormat<FormatAlignType> for String {
152 fn set_attrs_by_format(&mut self, format: &FormatAlignType) {
153 *self = format.to_str().to_string();
154 }
155
156 fn set_format(&self, format: &mut FormatAlignType) {
157 todo!()
158 }
159}