-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into use-clippy
- Loading branch information
Showing
13 changed files
with
508 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"folders": [ | ||
{ | ||
"path": "../umya-spreadsheet" | ||
}, | ||
], | ||
"settings": { | ||
"debug.allowBreakpointsEverywhere": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use super::driver::*; | ||
use super::XlsxError; | ||
use quick_xml::events::Event; | ||
use quick_xml::Reader; | ||
use std::result; | ||
use structs::raw::RawFile; | ||
use structs::Comment; | ||
use structs::Worksheet; | ||
use structs::{Table, TableColumn, TableStyleInfo}; | ||
|
||
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.trim_text(false); | ||
let mut buf = Vec::new(); | ||
let mut table = Table::default(); | ||
loop { | ||
match reader.read_event_into(&mut buf) { | ||
Ok(Event::Empty(ref e)) => match e.name().into_inner() { | ||
b"tableColumn" => { | ||
let mut table_column = TableColumn::default(); | ||
for a in e.attributes().with_checks(false) { | ||
match a { | ||
Ok(ref attr) => match attr.key.0 { | ||
b"name" => { | ||
table_column.set_name(get_attribute_value(attr)?); | ||
} | ||
_ => {} | ||
}, | ||
_ => {} | ||
} | ||
} | ||
// add column to table (if it has a name) | ||
if !table_column.get_name().is_empty() { | ||
table.add_column(table_column); | ||
} | ||
} | ||
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_first_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::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])); | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
_ => (), | ||
}, | ||
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
use quick_xml::{ | ||
events::{BytesStart, Event}, | ||
Reader, | ||
}; | ||
|
||
use super::coordinate::*; | ||
use crate::helper::coordinate::*; | ||
//use reader::driver::*; | ||
|
||
#[derive(Clone, Default, Debug)] | ||
pub struct Table { | ||
name: String, | ||
area: (Coordinate, Coordinate), | ||
display_name: String, | ||
columns: Vec<TableColumn>, | ||
style_info: Option<TableStyleInfo>, | ||
} | ||
impl Table { | ||
pub fn new<T>(name: &str, area: (T, T)) -> Self | ||
where | ||
T: Into<CellCoordinates>, | ||
{ | ||
let coord_beg = Self::cell_coord_to_coord(area.0); | ||
let coord_end = Self::cell_coord_to_coord(area.1); | ||
let name = name.to_string(); | ||
Self { | ||
area: (coord_beg, coord_end), | ||
name: name.clone(), | ||
display_name: name, | ||
columns: Vec::<TableColumn>::default(), | ||
style_info: None, | ||
} | ||
} | ||
|
||
pub fn is_ok(&self) -> bool { | ||
if self.name.is_empty() || self.display_name.is_empty() { | ||
return false; | ||
} | ||
if self.area.0.get_col_num() == &0 | ||
|| self.area.0.get_row_num() == &0 | ||
|| self.area.1.get_col_num() == &0 | ||
|| self.area.1.get_row_num() == &0 | ||
|| self.area.0.get_col_num() > self.area.1.get_col_num() | ||
|| self.area.0.get_row_num() > self.area.1.get_row_num() | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
pub fn get_name(&self) -> &String { | ||
&self.name | ||
} | ||
|
||
pub fn set_name(&mut self, name: &str) { | ||
self.name = name.to_string(); | ||
if self.display_name.is_empty() { | ||
self.display_name = name.to_string(); | ||
} | ||
} | ||
|
||
pub fn get_display_name(&self) -> &String { | ||
&self.display_name | ||
} | ||
|
||
pub fn set_display_name(&mut self, display_name: &str) { | ||
self.display_name = display_name.to_string(); | ||
} | ||
|
||
pub fn get_area(&self) -> &(Coordinate, Coordinate) { | ||
&self.area | ||
} | ||
|
||
pub fn set_area<T>(&mut self, area: (T, T)) | ||
where | ||
T: Into<CellCoordinates>, | ||
{ | ||
let coord_beg = Self::cell_coord_to_coord(area.0); | ||
let coord_end = Self::cell_coord_to_coord(area.1); | ||
self.area = (coord_beg, coord_end); | ||
} | ||
|
||
pub fn add_column(&mut self, col: TableColumn) { | ||
self.columns.push(col); | ||
} | ||
|
||
pub fn get_columns(&self) -> &Vec<TableColumn> { | ||
&self.columns | ||
} | ||
|
||
pub fn has_style_info(&self) -> bool { | ||
self.style_info.is_some() | ||
} | ||
|
||
pub fn get_style_info(&self) -> Option<&TableStyleInfo> { | ||
self.style_info.as_ref() | ||
} | ||
|
||
pub fn set_style_info(&mut self, style_info: Option<TableStyleInfo>) { | ||
self.style_info = style_info; | ||
} | ||
|
||
fn cell_coord_to_coord<T>(cc: T) -> Coordinate | ||
where | ||
T: Into<CellCoordinates>, | ||
{ | ||
let cell_coord: CellCoordinates = cc.into(); | ||
let mut coord: Coordinate = Default::default(); | ||
coord.set_col_num(cell_coord.col); | ||
coord.set_row_num(cell_coord.row); | ||
coord | ||
} | ||
} | ||
|
||
#[derive(Clone, Default, Debug)] | ||
pub struct TableColumn { | ||
name: String, | ||
} | ||
impl TableColumn { | ||
pub fn new(name: &str) -> Self { | ||
Self { | ||
name: name.to_string(), | ||
} | ||
} | ||
|
||
pub fn get_name(&self) -> &String { | ||
&self.name | ||
} | ||
|
||
pub fn set_name(&mut self, name: String) { | ||
self.name = name; | ||
} | ||
} | ||
|
||
#[derive(Clone, Default, Debug)] | ||
pub struct TableStyleInfo { | ||
name: String, | ||
show_first_col: bool, | ||
show_last_col: bool, | ||
show_row_stripes: bool, | ||
show_col_stripes: bool, | ||
} | ||
impl TableStyleInfo { | ||
pub fn new( | ||
name: &str, | ||
show_first_col: bool, | ||
show_last_col: bool, | ||
show_row_stripes: bool, | ||
show_col_stripes: bool, | ||
) -> Self { | ||
Self { | ||
name: name.to_string(), | ||
show_first_col, | ||
show_last_col, | ||
show_row_stripes, | ||
show_col_stripes, | ||
} | ||
} | ||
|
||
pub fn get_name(&self) -> &String { | ||
&self.name | ||
} | ||
|
||
pub fn is_show_first_col(&self) -> bool { | ||
self.show_first_col | ||
} | ||
|
||
pub fn is_show_last_col(&self) -> bool { | ||
self.show_last_col | ||
} | ||
|
||
pub fn is_show_row_stripes(&self) -> bool { | ||
self.show_row_stripes | ||
} | ||
|
||
pub fn is_show_col_stripes(&self) -> bool { | ||
self.show_col_stripes | ||
} | ||
} |
Oops, something went wrong.