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
//!
//! This module contains the [`FormatAlign`] struct, which used to edit the align style of the [`Cell`],
//! including set vertical and horizontal align, reading order and indent.
//!
//! # Examples
//!
//! Set vertical and horizontal align
//! ```
//! use edit_xlsx::{Cell, Format, FormatAlign, FormatAlignType, Workbook, Write};
//! // Set cell text alignment to top left
//! let format = Format::default()
//! .set_align(FormatAlignType::Top)
//! .set_align(FormatAlignType::Left);
//! // Or use
//! // let mut format = Format::default();
//! // let mut format_align = FormatAlign::default();
//! // format_align.vertical = Some(FormatAlignType::Top);
//! // format_align.horizontal = Some(FormatAlignType::Left);
//! // format.align = format_align;
//! let mut workbook = Workbook::from_path("./examples/xlsx/accounting.xlsx").unwrap();
//! let worksheet = workbook.get_worksheet_mut_by_name("worksheet").unwrap();
//! for row in 6..=15 {
//! for col in 3..=12 {
//! worksheet.write_with_format((row, col), 20.0, &format).unwrap()
//! }
//! }
//! workbook.save_as("./examples/align.xlsx").unwrap();
//! ```
//!
//! Set reading order
//! ```
//! use edit_xlsx::{Cell, Format, FormatAlign, FormatAlignType, Workbook, Write};
//! let format = Format::default()
//! .set_reading_order(2);
//! let mut workbook = Workbook::from_path("./examples/xlsx/accounting.xlsx").unwrap();
//! let worksheet = workbook.get_worksheet_mut_by_name("worksheet").unwrap();
//! for row in 6..=15 {
//! for col in 3..=12 {
//! worksheet.write_with_format((row, col), "نص عربي / English text", &format).unwrap();
//! }
//! }
//! workbook.save_as("./examples/reading_order.xlsx").unwrap();
//! ```
//!
//! Set indent
//! ```
//! use edit_xlsx::{Cell, Format, FormatAlign, FormatAlignType, Workbook, Write};
//! let mut format = Format::default()
//! .set_align(FormatAlignType::Top)
//! .set_align(FormatAlignType::Left);
//! let mut workbook = Workbook::from_path("./examples/xlsx/accounting.xlsx").unwrap();
//! let worksheet = workbook.get_worksheet_mut_by_name("worksheet").unwrap();
//! for col in 3..=12 {
//! let mut indent = 0;
//! for row in 6..=15 {
//! let format = format.clone().set_indent(indent);
//! worksheet.write_with_format((row, col), 10.0, &format).unwrap();
//! indent += 1;
//! }
//! }
//! workbook.save_as("./examples/indent.xlsx").unwrap();
//! ```
use crate::Cell;
use crate::xml::common::FromFormat;
///
/// [`FormatAlign`] struct, which used to edit the align style of the [`Cell`],
/// including set vertical and horizontal align(by [`FormatAlignType`]), reading order and indent.
/// # Fields
/// | field | type | meaning |
/// | ------------ | ----------- | ------------------------------------------------------------ |
/// | `horizontal` | [`Option<FormatAlignType>`] | The [`Cell`]'s horizontal alignment.<br> It's Some value can be [`FormatAlignType`] Left, Right, Center |
/// | `vertical` | [`Option<FormatAlignType>`] | The [`Cell`]'s vertical alignment.<br> It's Some value can be [`FormatAlignType`] Top, Bottom, VerticalCenter |
/// | `reading_order` | u8 | The [`Cell`]'s reading order<br>1: left to right<br>2: right to left|
/// | `indent` | u8 | The [`Cell`]'s indent |
#[derive(Clone, Debug, PartialEq)]
pub struct FormatAlign {
pub horizontal: Option<FormatAlignType>,
pub vertical: Option<FormatAlignType>,
pub reading_order: u8,
pub indent: u8,
}
impl Default for FormatAlign {
fn default() -> Self {
Self {
horizontal: None,
vertical: None,
reading_order: 1,
indent: 0,
}
}
}
/// [`FormatAlignType`] determines the position of the alignment.
///
/// # Fields:
/// | unit | meaning|
/// | ---- | ---- |
/// | `Top` |top-align|
/// | `Center` |horizontal center-align|
/// | `Bottom` |bottom-align|
/// | `Left` |left-align|
/// | `VerticalCenter` |vertical center-align|
/// | `Right` |right-align|
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum FormatAlignType {
Top,
Center,
Bottom,
Left,
VerticalCenter,
Right,
}
impl Default for FormatAlignType {
fn default() -> Self {
FormatAlignType::Center
}
}
impl FormatAlignType {
pub(crate) fn to_str(&self) -> &str {
match self {
FormatAlignType::Top => "top",
FormatAlignType::Center => "center",
FormatAlignType::Bottom => "bottom",
FormatAlignType::Left => "left",
FormatAlignType::VerticalCenter => "center",
FormatAlignType::Right => "right",
}
}
pub(crate) fn from_str(format_align_type: Option<&String>, is_horizontal: bool) -> Option<FormatAlignType> {
if let Some(format_align_type) = format_align_type {
let format_align_type = format_align_type.as_str();
Some(match format_align_type {
"top" => FormatAlignType::Top,
"center" => if is_horizontal { FormatAlignType::Center } else { FormatAlignType::VerticalCenter },
"bottom" => FormatAlignType::Bottom,
"left" => FormatAlignType::Left,
"right" => FormatAlignType::Right,
_ => FormatAlignType::default()
})
} else {
None
}
}
}
impl FromFormat<FormatAlignType> for String {
fn set_attrs_by_format(&mut self, format: &FormatAlignType) {
*self = format.to_str().to_string();
}
fn set_format(&self, format: &mut FormatAlignType) {
todo!()
}
}