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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use std::fs::File;
use std::io::BufReader;
use std::collections::HashMap;
use std::borrow::Cow;
use zip::read::{ZipFile, ZipArchive};
use zip::result::ZipError;
use quick_xml::reader::Reader as XmlReader;
use quick_xml::events::Event;
use quick_xml::events::attributes::{Attribute, Attributes};
use {Metadata, DataType, Reader, Range, Cell};
use vba::VbaProject;
use errors::*;
/// A struct representing xml zipped excel file
/// Xlsx, Xlsm, Xlam
pub struct Xlsx {
zip: ZipArchive<File>,
/// Shared strings
strings: Vec<String>,
/// Sheets paths
sheets: Vec<(String, String)>,
}
impl Xlsx {
fn read_shared_strings(&mut self) -> Result<()> {
let mut xml = match xml_reader(&mut self.zip, "xl/sharedStrings.xml") {
None => return Ok(()),
Some(x) => x?,
};
let mut buf = Vec::new();
loop {
match xml.read_event(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name() == b"si" => {
if let Some(s) = read_string(&mut xml, e.name())? {
self.strings.push(s);
}
}
Ok(Event::End(ref e)) if e.local_name() == b"sst" => break,
Ok(Event::Eof) => return Err("unexpected end of xml (no </sst>)".into()),
_ => (),
}
buf.clear();
}
Ok(())
}
fn read_workbook(&mut self,
relationships: &HashMap<Vec<u8>, String>)
-> Result<Vec<(String, String)>> {
let mut xml = match xml_reader(&mut self.zip, "xl/workbook.xml") {
None => return Ok(Vec::new()),
Some(x) => x?,
};
let mut defined_names = Vec::new();
let mut buf = Vec::new();
let mut val_buf = Vec::new();
loop {
match xml.read_event(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name() == b"sheet" => {
let mut name = String::new();
let mut path = String::new();
for a in e.attributes() {
let a = a?;
match a {
Attribute { key: b"name", .. } => {
name = a.unescape_and_decode_value(&xml)?;
}
Attribute {
key: b"r:id",
value: v,
} => {
let r = &relationships[&*v][..];
// target may have pre-prended "/xl/" or "xl/" path;
// strip if present
path = if r.starts_with("/xl/") {
r[1..].to_string()
} else if r.starts_with("xl/") {
r.to_string()
} else {
format!("xl/{}", r)
};
}
_ => (),
}
}
self.sheets.push((name, path));
}
Ok(Event::Start(ref e)) if e.local_name() == b"definedName" => {
if let Some(a) = e.attributes()
.filter_map(|a| a.ok())
.find(|a| a.key == b"name") {
let name = a.unescape_and_decode_value(&xml)?;
val_buf.clear();
let value = xml.read_text(b"definedName", &mut val_buf)?;
defined_names.push((name, value));
}
}
Ok(Event::End(ref e)) if e.local_name() == b"workbook" => break,
Ok(Event::Eof) => return Err("unexpected end of xml (no </workbook>)".into()),
Err(e) => return Err(e.into()),
_ => (),
}
buf.clear();
}
Ok(defined_names)
}
fn read_relationships(&mut self) -> Result<HashMap<Vec<u8>, String>> {
let mut xml = match xml_reader(&mut self.zip, "xl/_rels/workbook.xml.rels") {
None => return Err("Cannot find relationships file".into()),
Some(x) => x?,
};
let mut relationships = HashMap::new();
let mut buf = Vec::new();
loop {
buf.clear();
match xml.read_event(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name() == b"Relationship" => {
let mut id = Vec::new();
let mut target = String::new();
for a in e.attributes() {
match a? {
Attribute {
key: b"Id",
value: v,
} => id.extend_from_slice(v),
Attribute {
key: b"Target",
value: v,
} => target = xml.decode(v).into_owned(),
_ => (),
}
}
relationships.insert(id, target);
}
Ok(Event::End(ref e)) if e.local_name() == b"Relationships" => break,
Ok(Event::Eof) => return Err("unexpected end of xml (no </Relationships>)".into()),
Err(e) => return Err(e.into()),
_ => (),
}
}
Ok(relationships)
}
}
impl Reader for Xlsx {
fn new(f: File) -> Result<Self> {
Ok(Xlsx {
zip: ZipArchive::new(f)?,
strings: Vec::new(),
sheets: Vec::new(),
})
}
fn has_vba(&mut self) -> bool {
self.zip.by_name("xl/vbaProject.bin").is_ok()
}
fn vba_project(&mut self) -> Result<Cow<VbaProject>> {
let mut f = self.zip.by_name("xl/vbaProject.bin")?;
let len = f.size() as usize;
VbaProject::new(&mut f, len).map(|v| Cow::Owned(v))
}
fn initialize(&mut self) -> Result<Metadata> {
self.read_shared_strings()?;
let relationships = self.read_relationships()?;
let defined_names = self.read_workbook(&relationships)?;
Ok(Metadata {
sheets: self.sheets
.iter()
.map(|&(ref s, _)| s.clone())
.collect(),
defined_names: defined_names,
})
}
fn read_worksheet_range(&mut self, name: &str) -> Result<Range> {
let &(_, ref path) = self.sheets
.iter()
.find(|&&(ref n, _)| n == name)
.ok_or_else(|| ErrorKind::WorksheetName(name.to_string()))?;
let mut xml = match xml_reader(&mut self.zip, path) {
None => return Err(format!("Cannot find {} path", path).into()),
Some(x) => x?,
};
let mut cells = Vec::new();
let mut buf = Vec::new();
'xml: loop {
match xml.read_event(&mut buf) {
Err(e) => return Err(e.into()),
Ok(Event::Start(ref e)) => {
match e.local_name() {
b"dimension" => {
for a in e.attributes() {
if let Attribute {
key: b"ref",
value: rdim,
} = a? {
let (start, end) = get_dimension(rdim)?;
cells.reserve(((end.0 - start.0 + 1) * (end.1 - start.1 + 1)) as
usize);
continue 'xml;
}
}
return Err(format!("Expecting dimension, got {:?}", e).into());
}
b"sheetData" => read_sheet_data(&mut xml, &self.strings, &mut cells)?,
_ => (),
}
}
Ok(Event::End(ref e)) if e.local_name() == b"worksheet" => break,
Ok(Event::Eof) => return Err("unexpected end of xml (no </worksheet>)".into()),
_ => (),
}
buf.clear();
}
Ok(Range::from_sparse(cells))
}
}
fn xml_reader<'a>(zip: &'a mut ZipArchive<File>,
path: &str)
-> Option<Result<XmlReader<BufReader<ZipFile<'a>>>>> {
match zip.by_name(path) {
Ok(f) => {
let mut r = XmlReader::from_reader(BufReader::new(f));
r.check_end_names(false)
.trim_text(false)
.check_comments(false)
.expand_empty_elements(true);
Some(Ok(r))
}
Err(ZipError::FileNotFound) => None,
Err(e) => return Some(Err(e.into())),
}
}
/// read sheetData node
fn read_sheet_data(xml: &mut XmlReader<BufReader<ZipFile>>,
strings: &[String],
cells: &mut Vec<Cell>)
-> Result<()> {
/// read the contents of a <v> cell
fn read_value<'a>(v: String, strings: &[String], atts: Attributes<'a>) -> Result<DataType> {
match get_attribute(atts, b"t")? {
Some(b"s") => {
// shared string
let idx: usize = v.parse()?;
Ok(DataType::String(strings[idx].clone()))
}
Some(b"b") => {
// boolean
Ok(DataType::Bool(v != "0"))
}
Some(b"e") => {
// error
Ok(DataType::Error(v.parse()?))
}
Some(b"d") => {
// date
// TODO: create a DataType::Date
// currently just return as string (ISO 8601)
Ok(DataType::String(v))
}
Some(b"str") => {
// see http://officeopenxml.com/SScontentOverview.php
// str - refers to formula cells
// * <c .. t='v' .. > indicates calculated value (this case)
// * <c .. t='f' .. > to the formula string (ignored case
// TODO: Fully support a DataType::Formula representing both Formula string &
// last calculated value?
//
// NB: the result of a formula may not be a numeric value (=A3&" "&A4).
// We do try an initial parse as Float for utility, but fall back to a string
// representation if that fails
v.parse()
.map(DataType::Float)
.map_err(Error::from)
.or_else::<Error, _>(|_| Ok(DataType::String(v)))
}
Some(b"n") => {
// n - number
v.parse().map(DataType::Float).map_err(Error::from)
}
None => {
// If type is not known, we try to parse as Float for utility, but fall back to
// String if this fails.
v.parse()
.map(DataType::Float)
.map_err(Error::from)
.or_else::<Error, _>(|_| Ok(DataType::String(v)))
}
Some(b"is") => {
// this case should be handled in outer loop over cell elements, in which
// case read_inline_str is called instead. Case included here for completeness.
return Err("called read_value on a cell of type inlineStr".into());
}
Some(t) => return Err(format!("unknown cell 't' attribute={:?}", t).into()),
}
}
/// search through an Element's attributes for the named one
fn get_attribute<'a>(atts: Attributes<'a>, n: &'a [u8]) -> Result<Option<&'a [u8]>> {
for a in atts {
match a {
Ok(Attribute { key: k, value: v }) if k == n => return Ok(Some(v)),
Err(qe) => return Err(qe.into()),
_ => {} // ignore other attributes
}
}
Ok(None)
}
let mut buf = Vec::new();
/// main content of read_sheet_data
loop {
match xml.read_event(&mut buf) {
Err(e) => return Err(e.into()),
Ok(Event::Start(ref c_element)) if c_element.local_name() == b"c" => {
let pos = get_attribute(c_element.attributes(), b"r")
.and_then(|o| o.ok_or_else(|| "Cell missing 'r' attribute tag".into()))
.and_then(get_row_column)?;
loop {
let mut buf = Vec::new();
match xml.read_event(&mut buf) {
Err(e) => return Err(e.into()),
Ok(Event::Start(ref e)) => {
debug!("e: {:?}", e);
match e.local_name() {
b"is" => {
// inlineStr
if let Some(s) = read_string(xml, e.name())? {
cells.push(Cell::new(pos, DataType::String(s)));
}
break;
}
b"v" => {
// value
let v = xml.read_text(e.name(), &mut Vec::new())?;
cells.push(Cell::new(pos,
read_value(v,
strings,
c_element.attributes())?));
break;
}
b"f" => {} // ignore f nodes
n => {
return Err(format!("not a 'v', 'f', or 'is' node: {:?}", n)
.into())
}
}
}
Ok(Event::End(ref e)) if e.local_name() == b"c" => break,
Ok(Event::Eof) => return Err("unexpected end of xml (no </c>)".into()),
o => debug!("ignored Event: {:?}", o),
}
}
}
Ok(Event::End(ref e)) if e.local_name() == b"sheetData" => return Ok(()),
Ok(Event::Eof) => return Err("unexpected end of xml (no </sheetData>)".into()),
_ => (),
}
buf.clear();
}
}
/// converts a text representation (e.g. "A6:G67") of a dimension into integers
/// - top left (row, column),
/// - bottom right (row, column)
fn get_dimension(dimension: &[u8]) -> Result<((u32, u32), (u32, u32))> {
let parts: Vec<_> = dimension
.split(|c| *c == b':')
.map(|s| get_row_column(s))
.collect::<Result<Vec<_>>>()?;
match parts.len() {
0 => Err("dimension cannot be empty".into()),
1 => Ok((parts[0], parts[0])),
2 => Ok((parts[0], parts[1])),
len => Err(format!("range dimension has 0 or 1 ':', got {}", len).into()),
}
}
/// converts a text range name into its position (row, column) (0 based index)
fn get_row_column(range: &[u8]) -> Result<(u32, u32)> {
let (mut row, mut col) = (0, 0);
let mut pow = 1;
let mut readrow = true;
for c in range.iter().rev() {
match *c {
c @ b'0'...b'9' => {
if readrow {
row += ((c - b'0') as u32) * pow;
pow *= 10;
} else {
return Err(format!("Numeric character are only allowed \
at the end of the range: {:x}",
c)
.into());
}
}
c @ b'A'...b'Z' => {
if readrow {
pow = 1;
readrow = false;
}
col += ((c - b'A') as u32 + 1) * pow;
pow *= 26;
}
c @ b'a'...b'z' => {
if readrow {
pow = 1;
readrow = false;
}
col += ((c - b'a') as u32 + 1) * pow;
pow *= 26;
}
_ => return Err(format!("Expecting alphanumeric character, got {:x}", c).into()),
}
}
Ok((row - 1, col - 1))
}
/// attempts to read either a simple or richtext string
fn read_string(xml: &mut XmlReader<BufReader<ZipFile>>, closing: &[u8]) -> Result<Option<String>> {
let mut buf = Vec::new();
let mut rich_buffer: Option<String> = None;
loop {
match xml.read_event(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name() == b"r" => {
if rich_buffer.is_none() {
// use a buffer since richtext has multiples <r> and <t> for the same cell
rich_buffer = Some(String::new());
}
}
Ok(Event::End(ref e)) if e.local_name() == closing => {
return Ok(rich_buffer);
}
Ok(Event::Start(ref e)) if e.local_name() == b"t" => {
let value = xml.read_text(e.name(), &mut Vec::new())?;
if let Some(ref mut s) = rich_buffer {
s.push_str(&value);
} else {
// consume any remaining events up to expected closing tag
xml.read_to_end(closing, &mut Vec::new())?;
return Ok(Some(value));
}
}
Ok(Event::Eof) => return Err("unexpected end of xml".into()),
Err(e) => return Err(e.into()),
_ => (),
}
buf.clear();
}
}
#[test]
fn test_dimensions() {
assert_eq!(get_row_column(b"A1").unwrap(), (0, 0));
assert_eq!(get_row_column(b"C107").unwrap(), (106, 2));
assert_eq!(get_dimension(b"C2:D35").unwrap(), ((1, 2), (34, 3)));
}