Skip to content

Commit

Permalink
version 0.2.0; --sheetname feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckland committed Jul 24, 2018
1 parent 9ac1415 commit 1d1d83f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "markdown_tools"
version = "0.1.2"
version = "0.2.0"
authors = ["Ramon Buckland <[email protected]>"]
exclude = [
"doc/*",
Expand All @@ -22,4 +22,4 @@ serde_yaml = "0.7"
indexmap = "1.0.1"
linked-hash-map = "0.5.1"
docopt = "1.0.0"
calamine = "0.14"
calamine = "0.14"
5 changes: 5 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* `0.1.2`- 24 Jul 2018
- Fixed issue #1 where the `\r\n` return lines (pertinent inside MS XLS files in a Cell) would muck up the MD
as they were left with just a `\r`
* `0.2.0`- 25 Jul 2018
- Added the `--sheetname <name>` to the command line tool.
6 changes: 4 additions & 2 deletions src/bin/md_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const USAGE: &str = "
Markdown Tools
Usage:
md-tools table -t <type> <filename>
md-tools table -t <type> [-s <sheetname>] <filename>
md-tools (-h | --help)
md-tools --version
Expand All @@ -22,13 +22,15 @@ Options:
--version Show version.
-t --type <type> Input Type.
<filename> Input Filename.
-s --sheetname <sheetname> When a Spreadsheet, restrict to just one of the sheets.
";

#[derive(Debug, Deserialize)]
struct Args {
cmd_table: bool,
flag_type: FileType,
arg_filename: String,
flag_sheetname: Option<String>
}

#[derive(Debug, Deserialize)]
Expand All @@ -54,7 +56,7 @@ fn main() -> Result<(), String> {

let result = match args.flag_type {
FileType::YAML => read_yaml_file(args.arg_filename),
FileType::XLSX => spreadsheet_to_md(args.arg_filename),
FileType::XLSX => spreadsheet_to_md(args.arg_filename, args.flag_sheetname),
_ => Err(String::from("not implemented")),
};

Expand Down
11 changes: 8 additions & 3 deletions src/excel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ fn table_formatter(
}
}

pub fn spreadsheet_to_md(filename: String) -> Result<String, String> {
let results = read_excel(filename);
pub fn spreadsheet_to_md(filename: String, sheet_name: Option<String>) -> Result<String, String> {
let results = read_excel(filename, sheet_name);
if results.len() <= 1 {
Ok(table_formatter(results[0].clone(), false))
} else {
Expand All @@ -36,11 +36,16 @@ pub fn spreadsheet_to_md(filename: String) -> Result<String, String> {

pub fn read_excel(
filename: String,
sheet_name: Option<String>,
) -> Vec<Result<(String, Table<String, String>), (String, String)>> {
// opens a new workbook
let mut workbook = open_workbook_auto(filename).expect("Cannot open file");

let sheet_names = workbook.sheet_names().to_owned();
let sheet_names = if let Some(sheet_name) = sheet_name {
workbook.sheet_names().to_owned().into_iter().filter(|n| *n == sheet_name).to_owned().collect::<Vec<_>>()
} else {
workbook.sheet_names().to_owned()
};

let sheets: Vec<Result<(String, Table<String, String>), (String, String)>> = sheet_names
.iter()
Expand Down

0 comments on commit 1d1d83f

Please sign in to comment.