parent
2ecb864c6e
commit
d4a75ffb69
@ -0,0 +1 @@
|
||||
,sanchezn,pc-sanchezn,04.10.2025 13:10,file:///home/sanchezn/.config/libreoffice/4;
|
||||
@ -0,0 +1,18 @@
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Error {
|
||||
pub msg: String
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn new(msg: &str) -> Self {
|
||||
Error { msg: String::from(msg)}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Error: {}", self.msg)
|
||||
}
|
||||
}
|
||||
@ -1,83 +0,0 @@
|
||||
use umya_spreadsheet::{Spreadsheet, Worksheet};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Extractor<'a> {
|
||||
book: Spreadsheet,
|
||||
sheet: Option<&'a Worksheet>,
|
||||
pub separator: char,
|
||||
pub replacement: Option<char>,
|
||||
pub merge_fill: MergeFill,
|
||||
pub exclude_hidden: bool,
|
||||
}
|
||||
|
||||
impl<'a> Extractor<'a> {
|
||||
pub fn new(book: Spreadsheet) -> Self {
|
||||
Extractor {
|
||||
book,
|
||||
sheet: None,
|
||||
separator: ';',
|
||||
replacement: None,
|
||||
merge_fill: MergeFill::None,
|
||||
exclude_hidden: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_options(
|
||||
book: Spreadsheet,
|
||||
separator: char,
|
||||
replacement: Option<char>,
|
||||
) -> Self {
|
||||
Extractor {
|
||||
book,
|
||||
sheet: None,
|
||||
separator,
|
||||
replacement,
|
||||
merge_fill: MergeFill::None,
|
||||
exclude_hidden: true,
|
||||
}
|
||||
}
|
||||
|
||||
fn _is_row_hidden(&self, row_index: u32) -> Option<bool> {
|
||||
if let Some(row) = self.sheet.unwrap().get_row_dimension(&row_index) {
|
||||
if *row.get_hidden() {
|
||||
return Some(false);
|
||||
} else {
|
||||
return Some(true);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn _is_column_hidden(&self, column_index: u32) -> Option<bool> {
|
||||
if let Some(column) = self.sheet.unwrap().get_column_dimension_by_number(&column_index) {
|
||||
if *column.get_hidden() {
|
||||
return Some(false);
|
||||
} else {
|
||||
return Some(true);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn _is_cell_in_merge(&self, row_index: u32, column_index: u32) -> bool {
|
||||
let merges = self.sheet.unwrap().get_merge_cells();
|
||||
for merge in merges {
|
||||
if row_index >= *merge.get_coordinate_start_row().unwrap().get_num()
|
||||
&& row_index <= *merge.get_coordinate_end_row().unwrap().get_num()
|
||||
&& column_index >= *merge.get_coordinate_start_col().unwrap().get_num()
|
||||
&& column_index <= *merge.get_coordinate_end_col().unwrap().get_num()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum MergeFill {
|
||||
None,
|
||||
Horizontal,
|
||||
Vertical,
|
||||
Both,
|
||||
}
|
||||
@ -1,17 +1,67 @@
|
||||
use std::path::Path;
|
||||
use clap::Parser;
|
||||
use std::path::Path;
|
||||
use umya_spreadsheet::reader;
|
||||
|
||||
pub mod extractor;
|
||||
use extractor::Extractor;
|
||||
|
||||
pub mod arguments;
|
||||
use arguments::Arguments;
|
||||
|
||||
fn main() {
|
||||
let _args = Arguments::parse();
|
||||
pub mod error;
|
||||
use crate::error::Error;
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
let args = Arguments::parse();
|
||||
let book = reader::xlsx::read(Path::new(&args.file))
|
||||
.expect(format!("Can't open {}", args.file).as_str());
|
||||
|
||||
let sheet = match book.get_sheet(&0) {
|
||||
Some(sheet) => sheet,
|
||||
None => return Err(Error::new("cannot open sheet")),
|
||||
};
|
||||
|
||||
let book = reader::xlsx::read(Path::new("anafi.xlsx")).unwrap();
|
||||
let _extractor = Extractor::new(book);
|
||||
let (num_cols, num_rows) = sheet.get_highest_column_and_row();
|
||||
|
||||
for i in 1..=num_rows {
|
||||
if ! args.include_hidden_lines {
|
||||
match sheet.get_row_dimension(&i) {
|
||||
Some(dim) => {
|
||||
if *dim.get_hidden() {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
None => continue,
|
||||
}
|
||||
}
|
||||
let row = sheet.get_collection_by_row(&i);
|
||||
let row_len = row.len();
|
||||
let mut first = true;
|
||||
for cell in row {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
print!("{}", args.separator);
|
||||
}
|
||||
|
||||
let mut value = cell.get_formatted_value();
|
||||
if let Some(ref replacement) = args.replacement {
|
||||
value = value.replace(args.separator, replacement);
|
||||
} else {
|
||||
if value.contains(args.separator) {
|
||||
return Err(Error::new(
|
||||
format!(
|
||||
"Cell {} contains separator char",
|
||||
cell.get_coordinate().get_coordinate()
|
||||
)
|
||||
.as_str(),
|
||||
));
|
||||
}
|
||||
}
|
||||
print!("{}", value);
|
||||
}
|
||||
for _ in row_len..num_cols as usize {
|
||||
print!("{}", args.separator);
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
Binary file not shown.
Loading…
Reference in new issue