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
mod wb;
mod ws;
mod utils;
use std::fmt;
pub use wb::Workbook;
pub use ws::{Worksheet, ExcelValue};
pub use utils::{col2num, excel_number_to_date, num2col};
enum SheetNameOrNum {
Name(String),
Num(usize),
}
pub struct Config {
workbook_path: String,
tab: SheetNameOrNum,
nrows: Option<u32>,
want_help: bool,
}
pub enum ConfigError<'a> {
NeedPathAndTab(&'a str),
NeedTab,
RowsMustBeInt,
NeedNumRows,
UnknownFlag(&'a str),
}
impl<'a> fmt::Display for ConfigError<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ConfigError::NeedPathAndTab(exe) => write!(f, "need to provide path and tab when running '{}'. See usage below.", exe),
ConfigError::NeedTab => write!(f, "must also provide which tab you want to view in workbook"),
ConfigError::RowsMustBeInt => write!(f, "number of rows must be an integer value"),
ConfigError::NeedNumRows => write!(f, "must provide number of rows when using -n"),
ConfigError::UnknownFlag(flag) => write!(f, "unknown flag: {}", flag),
}
}
}
impl Config {
pub fn new(args: &[String]) -> Result<Config, ConfigError> {
if args.len() < 2 {
return Err(ConfigError::NeedPathAndTab(&args[0]))
} else if args.len() < 3 {
return match args[1].as_ref() {
"-h" | "--help" => Ok(Config {
workbook_path: "".to_owned(),
tab: SheetNameOrNum::Num(0),
nrows: None,
want_help: true,
}),
_ => Err(ConfigError::NeedTab)
}
}
let workbook_path = args[1].clone();
let tab = match args[2].parse::<usize>() {
Ok(num) => SheetNameOrNum::Num(num),
Err(_) => SheetNameOrNum::Name(args[2].clone())
};
let mut config = Config { workbook_path, tab, nrows: None, want_help: false };
let mut iter = args[3..].iter();
while let Some(flag) = iter.next() {
let flag = &flag[..];
match flag {
"-n" => {
if let Some(nrows) = iter.next() {
if let Ok(nrows) = nrows.parse::<u32>() {
config.nrows = Some(nrows)
} else {
return Err(ConfigError::RowsMustBeInt)
}
} else {
return Err(ConfigError::NeedNumRows)
}
},
_ => return Err(ConfigError::UnknownFlag(flag)),
}
}
Ok(config)
}
}
pub fn run(config: Config) -> Result<(), String> {
if config.want_help {
usage();
std::process::exit(0);
}
match crate::Workbook::new(&config.workbook_path) {
Ok(mut wb) => {
let sheets = wb.sheets();
let sheet = match config.tab {
SheetNameOrNum::Name(n) => sheets.get(&*n),
SheetNameOrNum::Num(n) => sheets.get(n),
};
if let Some(ws) = sheet {
let nrows = if let Some(nrows) = config.nrows {
nrows as usize
} else {
1048576
};
for row in ws.rows(&mut wb).take(nrows) {
println!("{}", row);
}
} else {
return Err("that sheet does not exist".to_owned())
}
Ok(())
},
Err(e) => Err(e)
}
}
pub fn usage() {
println!(concat!(
"\n",
"xlcat 0.1.6\n",
"Kevin Ryan <ktr@xlpro.tips>\n",
"\n",
"xlcat is like cat, but for Excel files (xlsx files to be precise). You simply\n",
"give it the path of the xlsx and the tab you want to view, and it prints the\n",
"data in that tab to your screen in a comma-delimited format.\n",
"\n",
"You can read about the project at https://xlpro.tips/posts/xlcat. The project\n",
"page is hosted at https://github.com/xlprotips/xl.\n",
"\n",
"USAGE:\n",
" xlcat PATH TAB [-n NUM] [-h | --help]\n",
"\n",
"ARGS:\n",
" PATH Where the xlsx file is located on your filesystem.\n",
" TAB Which tab in the xlsx you want to print to screen.\n",
"\n",
"OPTIONS:\n",
" -n <NUM> Limit the number of rows we print to <NUM>.\n",
));
}