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
178
179
180
use super::driver::*;
use super::XlsxError;
use crate::structs::raw::RawFile;
use crate::structs::Comment;
use crate::structs::Worksheet;
use crate::structs::{Table, TableColumn, TableStyleInfo};
use quick_xml::events::Event;
use quick_xml::Reader;
use std::result;
pub(crate) fn read(
worksheet: &mut Worksheet,
table_file: &RawFile,
) -> result::Result<(), XlsxError> {
let data = std::io::Cursor::new(table_file.get_file_data());
let mut reader = Reader::from_reader(data);
reader.config_mut().trim_text(false);
let mut buf = Vec::new();
let mut table = Table::default();
let mut table_column = TableColumn::default();
let mut string_value = String::new();
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Empty(ref e)) => match e.name().into_inner() {
b"tableColumn" => {
table_column = TableColumn::default();
for a in e.attributes().with_checks(false) {
match a {
Ok(ref attr) => match attr.key.0 {
b"name" => {
let attr_val = get_attribute_value(attr)?;
table_column.set_name(attr_val);
}
b"totalsRowLabel" => {
let attr_val = get_attribute_value(attr)?;
table_column.set_totals_row_label_str(&attr_val);
}
b"totalsRowFunction" => {
let attr_val = get_attribute_value(attr)?;
table_column.set_totals_row_function_str(&attr_val);
}
_ => {}
},
_ => {}
}
}
// add column to table (if it has a name)
if !table_column.get_name().is_empty() {
table.add_column(table_column);
table_column = TableColumn::default();
}
}
b"tableStyleInfo" => {
let mut name = String::new();
let mut show_first_col = false;
let mut show_last_col = false;
let mut show_row_stripes = false;
let mut show_col_stripes = false;
for a in e.attributes().with_checks(false) {
match a {
Ok(ref attr) => {
let attr_val = get_attribute_value(attr)?;
match attr.key.0 {
b"name" => {
name = attr_val;
}
b"showFirstColumn" => {
show_first_col = attr_val == "1";
}
b"showLastColumn" => {
show_last_col = attr_val == "1";
}
b"showRowStripes" => {
show_row_stripes = attr_val == "1";
}
b"showColumnStripes" => {
show_col_stripes = attr_val == "1";
}
_ => {}
}
}
_ => {}
}
}
if !name.is_empty() {
table.set_style_info(Some(TableStyleInfo::new(
&name,
show_first_col,
show_last_col,
show_row_stripes,
show_col_stripes,
)));
}
}
_ => (),
},
Ok(Event::Text(e)) => string_value = e.unescape().unwrap().to_string(),
Ok(Event::Start(ref e)) => match e.name().into_inner() {
b"table" => {
for a in e.attributes().with_checks(false) {
match a {
Ok(ref attr) => {
let attr_val = get_attribute_value(attr)?;
match attr.key.0 {
b"displayName" => {
table.set_display_name(&attr_val);
}
b"name" => {
table.set_name(&attr_val);
}
b"ref" => {
let area_coords: Vec<&str> = attr_val.split(':').collect();
if area_coords.len() == 2 {
table.set_area((area_coords[0], area_coords[1]));
}
}
b"totalsRowShown" => {
table.set_totals_row_shown_str(&attr_val);
}
b"totalsRowCount" => {
table.set_totals_row_count_str(&attr_val);
}
_ => {}
}
}
_ => {}
}
}
}
b"tableColumn" => {
table_column = TableColumn::default();
for a in e.attributes().with_checks(false) {
match a {
Ok(ref attr) => match attr.key.0 {
b"name" => {
let attr_val = get_attribute_value(attr)?;
table_column.set_name(attr_val);
}
b"totalsRowLabel" => {
let attr_val = get_attribute_value(attr)?;
table_column.set_totals_row_label_str(&attr_val);
}
b"totalsRowFunction" => {
let attr_val = get_attribute_value(attr)?;
table_column.set_totals_row_function_str(&attr_val);
}
_ => {}
},
_ => {}
}
}
}
_ => (),
},
Ok(Event::End(ref e)) => match e.name().into_inner() {
b"calculatedColumnFormula" => {
table_column.set_calculated_column_formula(string_value);
string_value = String::new();
}
b"tableColumn" => {
// add column to table (if it has a name)
if !table_column.get_name().is_empty() {
table.add_column(table_column);
table_column = TableColumn::default();
}
}
_ => (),
},
Ok(Event::Eof) => break,
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
_ => (),
}
buf.clear();
}
// add the table to the sheet (if a few sanity checks pass)
if table.is_ok() {
worksheet.add_table(table);
}
Ok(())
}