Skip to content

Commit

Permalink
Added additional functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
CHRISCARLON committed Jun 24, 2024
1 parent d693eb8 commit 7c389ba
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 63 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// src/lib.rs

pub mod local_file_functions;
pub mod remote_file_functions;
87 changes: 56 additions & 31 deletions src/local_file_functions.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,65 @@
// src/local_file_functions.rs

use calamine::{open_workbook, Reader, Xlsx};
use calamine::{open_workbook, Reader, Xlsx, DataType};
use comfy_table::{Table, Cell, Color, Attribute};
use std::path::Path;

pub fn display_basic_info<P: AsRef<Path>>(path: P) -> Result<(), Box<dyn std::error::Error>> {
let mut workbook: Xlsx<_> = open_workbook(path)?;

if let Some(Ok(range)) = workbook.worksheet_range_at(0) {
let mut table = Table::new();
table.load_preset(comfy_table::presets::UTF8_FULL);
table.set_header(vec![
Cell::new("Column Headers")
.add_attribute(Attribute::Bold)
.fg(Color::Green)
]);

let row_count = range.rows().count();

let column_count = if let Some(file) = range.rows().next() {
let count = file.len();
for (index, cell) in file.iter().enumerate() {
table.add_row(vec![
format!("Column {}: {}", index + 1, cell.to_string())
]);
}
count

let sheet_names = workbook.sheet_names().to_vec();

for sheet_name in sheet_names {
if let Some(Ok(range)) = workbook.worksheet_range_at(0) {
let mut table = Table::new();
table.load_preset(comfy_table::presets::UTF8_FULL);
table.set_header(vec![
Cell::new("Column Headers")
.add_attribute(Attribute::Bold)
.fg(Color::Green),
Cell::new("Data Type")
.add_attribute(Attribute::Bold)
.fg(Color::DarkRed)
]);

let row_count = range.rows().count();
let column_count = if let Some(header_row) = range.rows().next() {
let count = header_row.len();

for (index, cell) in header_row.iter().enumerate() {
let data_type = if cell.is_empty() {
"Empty"
} else if cell.is_int() {
"Integer"
} else if cell.is_float() {
"Float"
} else if cell.is_string() {
"String"
} else if cell.is_bool() {
"Boolean"
} else if cell.is_error() {
"Error"
} else {
"Unknown"
};

table.add_row(vec![
Cell::new(format!("Column {}: {}", index + 1, cell.to_string())),
Cell::new(data_type)
]);
}
count
} else {
0
};

println!("Sheet Name: {}", sheet_name);
println!("Total number of columns: {}", column_count);
println!("Total number of rows: {}", row_count);
println!("{table}");
} else {
0
};

println!("\nTotal number of columns: {}", column_count);
println!("\nTotal number of rows: {}", row_count);
println!("{table}");
Ok(())
} else {
Err("Cannot read sheet".into())
println!("Cannot read sheet: {}", sheet_name);
}
}

Ok(())
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// src/main.rs

mod local_file_functions;
mod remote_file_functions;
use local_file_functions::display_basic_info;
use remote_file_functions::display_remote_basic_info;
use clap::Parser;
use std::path::PathBuf;

/// A program to display Excel column headers
/// A program to display basic Excel file information
#[derive(Parser, Debug)]
#[command(author = "Chris Carlon", version = "0.1", about = "Displays Basic Info About Excel Files", long_about = None)]
struct Args {
Expand Down
88 changes: 57 additions & 31 deletions src/remote_file_functions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use calamine::{Xlsx, Reader};
use calamine::{Xlsx, Reader, DataType};
use comfy_table::{Table, Cell, Color, Attribute};
use reqwest::blocking::get;
use std::io::Cursor;
Expand All @@ -7,38 +7,64 @@ pub fn display_remote_basic_info(url: &str) -> Result<(), Box<dyn std::error::Er
// Download the file into memory
let response = get(url)?;
let content = response.bytes()?;

// Create Xlsx object from memory
let mut workbook: Xlsx<_> = Xlsx::new(Cursor::new(content))?;

if let Some(Ok(range)) = workbook.worksheet_range_at(0) {
let mut table = Table::new();
table.load_preset(comfy_table::presets::UTF8_FULL);
table.set_header(vec![
Cell::new("Column Headers")
.add_attribute(Attribute::Bold)
.fg(Color::Green)
]);

let row_count = range.rows().count();

let column_count = if let Some(file) = range.rows().next() {
let count = file.len();
for (index, cell) in file.iter().enumerate() {
table.add_row(vec![
format!("Column {}: {}", index + 1, cell.to_string())
]);
}
count
let sheet_names = workbook.sheet_names().to_vec();

for sheet_name in sheet_names {
if let Some(Ok(range)) = workbook.worksheet_range_at(0) {
let mut table = Table::new();
table.load_preset(comfy_table::presets::UTF8_FULL);
table.set_header(vec![
Cell::new("Column Headers")
.add_attribute(Attribute::Bold)
.fg(Color::Green),
Cell::new("Data Type")
.add_attribute(Attribute::Bold)
.fg(Color::DarkRed)
]);

let row_count = range.rows().count();
let column_count = if let Some(header_row) = range.rows().next() {
let count = header_row.len();

for (index, cell) in header_row.iter().enumerate() {
let data_type = if cell.is_empty() {
"Empty"
} else if cell.is_int() {
"Integer"
} else if cell.is_float() {
"Float"
} else if cell.is_string() {
"String"
} else if cell.is_bool() {
"Boolean"
} else if cell.is_error() {
"Error"
} else {
"Unknown"
};

table.add_row(vec![
Cell::new(format!("Column {}: {}", index + 1, cell.to_string())),
Cell::new(data_type)
]);
}
count
} else {
0
};

println!("Sheet Name: {}", sheet_name);
println!("Total number of columns: {}", column_count);
println!("Total number of rows: {}", row_count);
println!("{table}");
} else {
0
};

println!("\nTotal number of columns: {}", column_count);
println!("\nTotal number of rows: {}", row_count);
println!("{table}");
Ok(())
} else {
Err("Cannot read sheet".into())
println!("Cannot read sheet: {}", sheet_name);
}
}
}

Ok(())
}

0 comments on commit 7c389ba

Please sign in to comment.