Skip to content

Commit 1617ce1

Browse files
authored
Merge pull request #6 from DHTMLX/additional-style-props
[dev] add zero xsf style
2 parents 529c4c5 + 57ceec0 commit 1617ce1

File tree

2 files changed

+48
-51
lines changed

2 files changed

+48
-51
lines changed

src/lib.rs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ fn get_styles_data(style_table: StyleTable) -> String {
251251
let fonts_children: Vec<Element> = style_table.fonts.iter().map(|ref font| {
252252
let mut font_element = Element::new("font");
253253
let mut font_children = vec!();
254-
match font.family {
254+
match font.name {
255255
Some(ref font) => {
256256
let mut name_el = Element::new("name");
257257
name_el.add_attr("val", font);
@@ -346,38 +346,35 @@ fn get_styles_data(style_table: StyleTable) -> String {
346346
.add_children(fills_children);
347347

348348
let mut cell_xfs = Element::new("cellXfs");
349-
let xfs_children: Vec<Element> = style_table.xfs.iter().map(|props| {
349+
let xfs_children: Vec<Element> = style_table.xfs.iter().map(|p| {
350350
let mut xf = Element::new("xf");
351351

352-
props.as_ref().map(|p| {
353-
p.font_id.map(|id|{
354-
xf
355-
.add_attr("applyFont", "1")
356-
.add_attr("fontId", id.to_string());
357-
});
358-
p.fill_id.map(|id| {
359-
xf
360-
.add_attr("applyFill", "1")
361-
.add_attr("fillId", id.to_string());
362-
});
363-
p.format_id.map(|id| {
364-
xf
365-
.add_attr("applyNumberFormat", "1")
366-
.add_attr("numFmtId", id.to_string());
367-
});
368-
p.border_id.map(|id| {
369-
xf
370-
.add_attr("applyBorder", "1")
371-
.add_attr("borderId", id.to_string());
372-
});
373-
374-
if p.align_h.is_some() || p.align_v.is_some() {
375-
let mut alignment = Element::new("alignment");
376-
p.align_h.as_ref().map(|v| alignment.add_attr("horizontal", v));
377-
p.align_v.as_ref().map(|v| alignment.add_attr("vertical", v));
378-
xf.add_attr("applyAlignment", "1").add_children(vec![alignment]);
379-
}
352+
p.font_id.map(|id|{
353+
xf
354+
.add_attr("applyFont", "1")
355+
.add_attr("fontId", id.to_string());
356+
});
357+
p.fill_id.map(|id| {
358+
xf
359+
.add_attr("applyFill", "1")
360+
.add_attr("fillId", id.to_string());
380361
});
362+
p.format_id.map(|id| {
363+
xf
364+
.add_attr("applyNumberFormat", "1")
365+
.add_attr("numFmtId", id.to_string());
366+
});
367+
p.border_id.map(|id| {
368+
xf
369+
.add_attr("applyBorder", "1")
370+
.add_attr("borderId", id.to_string());
371+
});
372+
if p.align_h.is_some() || p.align_v.is_some() {
373+
let mut alignment = Element::new("alignment");
374+
p.align_h.as_ref().map(|v| alignment.add_attr("horizontal", v));
375+
p.align_v.as_ref().map(|v| alignment.add_attr("vertical", v));
376+
xf.add_attr("applyAlignment", "1").add_children(vec![alignment]);
377+
}
381378

382379
xf
383380
}).collect();
@@ -503,7 +500,8 @@ fn get_sheet_data(cells: Vec<Vec<InnerCell>>, columns: &Option<Vec<Option<Column
503500
}
504501
match &cell.style {
505502
Some(ref v) => {
506-
cell_el.add_attr("s", v.to_string());
503+
// style index should be incremented as zero style was prepended
504+
cell_el.add_attr("s", (v+1).to_string());
507505
},
508506
None => ()
509507
}

src/style.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::xml::Element;
44

55
#[derive(PartialEq)]
66
pub struct Font {
7-
pub family: Option<String>,
7+
pub name: Option<String>,
88
pub size: Option<String>,
99
pub color: Option<String>,
1010
pub bold: bool,
@@ -65,12 +65,12 @@ pub struct StyleTable {
6565
pub fonts: Vec<Font>,
6666
pub fills: Vec<Fill>,
6767
pub borders: Vec<Border>,
68-
pub xfs: Vec<Option<XSFProps>>,
68+
pub xfs: Vec<XFSProps>,
6969
pub custom_formats: HashMap<String, u32>,
7070
next_custom_format: u32,
7171
}
7272

73-
pub struct XSFProps {
73+
pub struct XFSProps {
7474
pub font_id: Option<usize>,
7575
pub border_id: Option<usize>,
7676
pub fill_id: Option<usize>,
@@ -85,23 +85,22 @@ impl StyleTable {
8585
fonts: vec![Font::new()],
8686
fills: vec![Fill::new(None, "none"), Fill::new(None, "gray125")],
8787
borders: vec![Border::new()],
88-
xfs: vec![],
88+
xfs: vec![XFSProps::new()],
8989
custom_formats: HashMap::new(),
9090
next_custom_format: 164,
9191
};
92-
match css {
93-
Some(map) => {
94-
for style in map {
95-
table.add(style);
96-
}
92+
table.custom_formats.insert(String::from("General"), 164);
93+
94+
css.map(|map| {
95+
for style in map {
96+
table.add(style);
9797
}
98-
None => table.xfs.push(Some(XSFProps::new())),
99-
}
98+
});
10099

101100
table
102101
}
103102
pub fn add(&mut self, style: HashMap<String, String>) {
104-
let mut xsf_props: XSFProps = XSFProps::new();
103+
let mut xsf_props: XFSProps = XFSProps::new();
105104
let st = style_to_props(&style);
106105

107106
xsf_props.font_id = st
@@ -151,7 +150,7 @@ impl StyleTable {
151150
i as usize
152151
});
153152

154-
self.xfs.push(Some(xsf_props));
153+
self.xfs.push(xsf_props);
155154
}
156155
}
157156

@@ -167,7 +166,7 @@ impl Fill {
167166
impl Font {
168167
pub fn new() -> Font {
169168
Font {
170-
family: None,
169+
name: None,
171170
size: None,
172171
color: None,
173172
bold: false,
@@ -233,9 +232,9 @@ impl StyleProps {
233232
}
234233
}
235234

236-
impl XSFProps {
237-
pub fn new() -> XSFProps {
238-
XSFProps {
235+
impl XFSProps {
236+
pub fn new() -> XFSProps {
237+
XFSProps {
239238
font_id: None,
240239
fill_id: None,
241240
border_id: None,
@@ -260,7 +259,7 @@ fn style_to_props(styles: &HashMap<String, String>) -> StyleProps {
260259
"color" => font.color = color_to_argb(value),
261260
"fontWeight" => font.bold = value == "bold",
262261
"fontStyle" => font.italic = value == "italic",
263-
"fontFamily" => font.family = Some(value.to_string()),
262+
"fontFamily" => font.name = Some(value.to_string()),
264263
"textDecoration" => {
265264
font.underline = value.contains("underline");
266265
font.strike = value.contains("line-through");
@@ -435,7 +434,7 @@ fn str_to_border(v: &str, pos: BorderPosition) -> Option<BorderProps> {
435434
"dashed" => BorderStyle::Dashed,
436435
"solid" => {
437436
let mut st = BorderStyle::Thin;
438-
437+
439438
if size == 0.5 {
440439
st = BorderStyle::Thin
441440
} else if size == 1.0 {
@@ -486,7 +485,7 @@ fn style_to_props_test() {
486485
assert_eq!(font.size, Some(String::from("18")));
487486
assert_eq!(font.color, Some(String::from("FFFFFF00")));
488487
assert_eq!(font.bold, true);
489-
assert_eq!(font.family, Some(String::from("Calibri")));
488+
assert_eq!(font.name, Some(String::from("Calibri")));
490489
assert_eq!(font.italic, true);
491490
assert_eq!(font.underline, true);
492491
assert_eq!(st.fill.unwrap().color, Some(String::from("FFFF0000")));

0 commit comments

Comments
 (0)