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
use super::{Book,Sheet,Cell,Style};
use file_common::*;
use super::quick_xml::reader::Reader;
use super::quick_xml::events::{Event};
use super::tempdir::TempDir;
use super::{Result};
use super::read_style::StyleContent;
use std::collections::HashMap;
const CONTENT_XML: &'static str = "content.xml";
pub fn read(dir: &TempDir, style_content: &StyleContent) -> Result<Book> {
let mut date_style_map = HashMap::new();
let mut style_map_for_date: HashMap<String, String> = HashMap::new();
let path = dir.path().join(CONTENT_XML);
let mut reader = Reader::from_file(path)?;
reader.trim_text(true);
let mut book = Book::new();
let mut buf = Vec::new();
let mut sheet = Sheet::new("");
let mut row: usize = 0;
let mut column: usize = 0;
let mut cell_type: String = String::from("");
let mut float_value: f64 = 0.0;
let mut str_value: String = String::from("");
let mut date_value: String = String::from("");
let mut table_style_name: String = String::from("");
loop {
match reader.read_event(&mut buf) {
Ok(Event::Start(ref e)) => {
match e.name() {
b"table:table" => {
for a in e.attributes().with_checks(false) {
match a {
Ok(ref attr) if attr.key == b"table:name" => {
sheet.set_name(get_attribute_value(attr)?);
},
Ok(_) => {},
Err(_) => {},
}
}
},
b"table:table-cell" => {
for a in e.attributes().with_checks(false) {
match a {
Ok(ref attr) if attr.key == b"table:style-name" => {
table_style_name = get_attribute_value(attr)?;
},
Ok(ref attr) if attr.key == b"office:value-type" => {
cell_type = get_attribute_value(attr)?;
},
Ok(ref attr) if attr.key == b"office:value" => {
let value = get_attribute_value(attr)?;
float_value = value.parse::<f64>().unwrap();
},
Ok(ref attr) if attr.key == b"office:date-value" => {
date_value = get_attribute_value(attr)?;
},
Ok(_) => {},
Err(_) => {},
}
}
},
b"number:date-style" => {
for a in e.attributes().with_checks(false) {
match a {
Ok(ref attr) if attr.key == b"style:name" => {
date_style_map.insert(
get_attribute_value(attr)?,
super::read_number_date_style(&mut reader)?);
},
Ok(_) => {},
Err(_) => {},
}
}
},
_ => (),
}
},
Ok(Event::End(ref e)) => {
match e.name() {
b"table:table" => {
row = 0;
column = 0;
book.add_sheet(sheet);
sheet = Sheet::new("");
},
b"table:table-row" => {
row = row + 1;
column = 0;
},
b"table:table-cell" => {
match cell_type.as_str() {
"string" => {
let cell = Cell::str(str_value.clone());
sheet.add_cell(cell, row, column);
},
"float" => {
let cell = Cell::float(float_value);
sheet.add_cell(cell, row, column);
},
"date" => {
let format = match style_map_for_date.get(&table_style_name) {
Some(value) => value.clone(),
None => String::from(""),
};
let cell = Cell::date_with_style(date_value.clone(), Style::new(format));
sheet.add_cell(cell, row, column);
},
_ => {},
}
cell_type = String::from("");
column = column + 1;
},
_ => (),
}
}
Ok(Event::Empty(ref e)) => {
match e.name() {
b"table:table-cell" => {
if 0 == e.attributes().count() {
column = column + 1;
} else {
for a in e.attributes().with_checks(false) {
match a {
Ok(ref attr) if attr.key == b"table:number-columns-repeated" => {
let value = get_attribute_value(attr)?;
column = column + value.parse::<usize>().unwrap();
},
Ok(_) => {},
Err(_) => {},
}
}
}
},
b"style:style" => {
let mut style_name = String::from("");
let mut data_style_name = String::from("");
for a in e.attributes().with_checks(false) {
match a {
Ok(ref attr) if attr.key == b"style:name" => {
style_name = get_attribute_value(attr)?;
},
Ok(ref attr) if attr.key == b"style:data-style-name" => {
data_style_name = get_attribute_value(attr)?;
},
Ok(_) => {},
Err(_) => {},
}
}
if data_style_name != "" {
if let Some(format) = date_style_map.get(&data_style_name) {
style_map_for_date.insert(style_name.clone(), format.clone());
} else if let Some(format) = style_content.date_style_map.get(&data_style_name) {
style_map_for_date.insert(style_name.clone(), format.clone());
}
}
},
_ => (),
}
}
Ok(Event::Text(e)) => str_value = e.unescape_and_decode(&reader).unwrap(),
Ok(Event::Eof) => break,
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
_ => (),
}
// if we don't keep a borrow elsewhere, we can clear the buffer to keep memory usage low
buf.clear();
}
Ok(book)
}