Skip to content

Commit

Permalink
fix: fix builtin number format error
Browse files Browse the repository at this point in the history
  • Loading branch information
zitsen committed Apr 6, 2022
1 parent 8640761 commit a8c763e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 48 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
[package]
name = "xlsx2csv"
version = "0.4.3-alpha.0"
authors = ["Huo Linhe <[email protected]>"]
description = "Excel-like sheets to CSV converter"
edition = "2018"
homepage = "https://github.com/zitsen/xlsx2csv.rs"
license = "MIT OR Apache-2.0"
name = "xlsx2csv"
repository = "https://github.com/zitsen/xlsx2csv.rs"
version = "0.4.3-alpha.0"
description = "Excel-like sheets to CSV converter"

[package.metadata.release]
post-release-commit-message = "chore(dev): start next development iteration {{version}}"
pre-release-commit-message = "chore(release): release {{version}}"

[dependencies]
calamine = {version = "0.18", features = ["dates"]}
# calamine = {version = "0.18", features = ["dates"]}
clap = { version = "3", features = ["derive"] }
csv = "1"
ooxml = "0.2.4"
ooxml = "0.2.5"
pbr = "1"
regex = "1"
structopt = "0.3.17"
93 changes: 51 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// use calamine::Reader;
// use calamine::{open_workbook_auto, Sheets};

use clap::Parser;
use std::fmt;
use std::path::PathBuf;
use structopt::StructOpt;

use regex::RegexBuilder;

Expand Down Expand Up @@ -149,7 +149,7 @@ impl std::str::FromStr for Delimiter {
/// ```
/// xlsx2csv input.xlsx -I '\S{3,}' -X 'Sheet'
/// ```
#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
struct Opt {
/// Input Excel-like files, supports: .xls .xlsx .xlsb .xlsm .ods
xlsx: PathBuf,
Expand All @@ -158,74 +158,83 @@ struct Opt {
/// If not setted, output first sheet to stdout.
output: Vec<PathBuf>,
/// List sheet names by id.
#[structopt(short, long, conflicts_with_all = &["output", "select", "use_sheet_names"])]
#[clap(short, long, conflicts_with_all = &["output", "select", "use_sheet_names"])]
list: bool,
/// Use first line as header, which means use first line to select columns
#[structopt(short, long)]
#[clap(short, long)]
use_header: bool,
/// Select sheet by name or id in output, only used when output to stdout.
#[structopt(short, long, conflicts_with = "output")]
#[clap(short, long, conflicts_with = "output")]
select: Option<SheetSelector>,
/// Use sheet names as output filename prefix (in current dir or --workdir).
#[structopt(short, long, alias = "sheet", conflicts_with = "output")]
#[clap(short, long, alias = "sheet", conflicts_with = "output")]
use_sheet_names: bool,
/// Output files location if `--use-sheet-names` setted
#[structopt(short, long, conflicts_with = "output", requires = "use-sheet-names")]
#[clap(short, long, conflicts_with = "output", requires = "use-sheet-names")]
workdir: Option<PathBuf>,
/// A regex pattern for matching sheetnames to include, used with '-u'.
#[structopt(short = "I", long, requires = "use-sheet-names")]
#[clap(short = 'I', long, requires = "use-sheet-names")]
include: Option<String>,
/// A regex pattern for matching sheetnames to exclude, used with '-u'.
#[structopt(short = "X", long, requires = "use-sheet-names")]
#[clap(short = 'X', long, requires = "use-sheet-names")]
exclude: Option<String>,
/// Regex case insensitivedly.
///
/// When this flag is provided, the include and exclude patterns will be searched case insensitively. used with '-u'.
#[structopt(short = "i", long, requires = "use-sheet-names")]
#[clap(short = 'i', long, requires = "use-sheet-names")]
ignore_case: bool,
/// Delimiter for output.
///
/// If `use-sheet-names` setted, it will control the output filename extension: , -> csv, \t -> tsv
#[structopt(short, long, default_value = ",")]
#[clap(short, long, default_value = ",")]
delimiter: Delimiter,
}

fn worksheet_to_csv<W>(workbook: &ooxml::document::Workbook,
sheet: &str, wtr: &mut csv::Writer<W>, header: bool) where W: std::io::Write {
let worksheet = workbook
.get_worksheet_by_name(&sheet)
.expect("worksheet name error");
let mut iter = worksheet.rows();
if header {
let header = iter.next();
if header.is_none() {
return;
}
let header = header.unwrap();
let size = header.into_iter().position(|cell| cell.is_empty()).expect("find header row size");
fn worksheet_to_csv<W>(
workbook: &ooxml::document::Workbook,
sheet: &str,
wtr: &mut csv::Writer<W>,
header: bool,
) where
W: std::io::Write,
{
let worksheet = workbook
.get_worksheet_by_name(&sheet)
.expect("worksheet name error");
let mut iter = worksheet.rows();
if header {
let header = iter.next();
if header.is_none() {
return;
}
let header = header.unwrap();
let size = header
.into_iter()
.position(|cell| cell.is_empty())
.expect("find header row size");

for row in worksheet.rows() {
let cols: Vec<String> = row
.into_iter()
.take(size)
.map(|cell| cell.to_string().unwrap_or_default())
.collect();
wtr.write_record(&cols).unwrap();
}
} else {
for row in worksheet.rows() {
let cols: Vec<String> = row
.into_iter()
.map(|cell| cell.to_string().unwrap_or_default())
.collect();
wtr.write_record(&cols).unwrap();
}
for row in worksheet.rows() {
let cols: Vec<String> = row
.into_iter()
.take(size)
.map(|cell| cell.to_string().unwrap_or_default())
.collect();
wtr.write_record(&cols).unwrap();
}
} else {
for row in worksheet.rows() {
let cols: Vec<String> = row
.into_iter()
.map(|cell| cell.to_string().unwrap_or_default())
.collect();
wtr.write_record(&cols).unwrap();
}
wtr.flush().unwrap();
}
wtr.flush().unwrap();
}

fn main() {
let opt = Opt::from_args();
let opt = Opt::parse();
let xlsx = ooxml::document::SpreadsheetDocument::open(opt.xlsx).expect("open xlsx file");
let workbook = xlsx.get_workbook();
//let mut workbook: Sheets = open_workbook_auto(&opt.xlsx).expect("open file");
Expand Down

0 comments on commit a8c763e

Please sign in to comment.