wip: feat: method to save sheet rows to csv file

This commit is contained in:
Jeremy Wall 2025-03-04 19:23:45 -05:00
parent 473ba9c665
commit 8cd93cb6b0
3 changed files with 28 additions and 18 deletions

16
Cargo.lock generated
View File

@ -482,9 +482,9 @@ dependencies = [
[[package]]
name = "csv"
version = "1.3.0"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac574ff4d437a7b5ad237ef331c17ccca63c46479e5b5453eb8e10bb99a759fe"
checksum = "acdc4883a9c96732e4733212c01447ebd805833b7275a73ca3ee080fd77afdaf"
dependencies = [
"csv-core",
"itoa",
@ -501,16 +501,6 @@ dependencies = [
"memchr",
]
[[package]]
name = "csvx"
version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92081efd8b1d03f5a1bf242876cfdd8fa2bf9fe521ddb2d31f8747dfa2dd2cb7"
dependencies = [
"csv",
"thiserror",
]
[[package]]
name = "deranged"
version = "0.3.11"
@ -1507,7 +1497,7 @@ dependencies = [
"clap",
"colorsys",
"crossterm",
"csvx",
"csv",
"futures",
"ironcalc",
"ratatui",

View File

@ -9,7 +9,6 @@ edition = "2021"
anyhow = { version = "1.0.91", features = ["backtrace"] }
clap = { version = "4.5.20", features = ["derive"] }
crossterm = { version = "0.28.1", features = ["event-stream", "serde"] }
csvx = "0.1.17"
# this revision introduces a way to get the Model back out of the UserModel
ironcalc = { git = "https://github.com/ironcalc/IronCalc" }
futures = "0.3.31"
@ -21,3 +20,4 @@ slice-utils = { git = "https://dev.zaphar.net/zaphar/slice-cursor-rs.git" }
serde_json = "1.0.133"
colorsys = "0.6.7"
tui-markdown = { version = "0.3.1", features = [] }
csv = "1.3.1"

View File

@ -119,12 +119,23 @@ impl Book {
)?))
}
pub fn get_export_rows(&self) -> Result<Vec<Vec<String>>> {
let sheet = self.location.sheet;
Ok(self.export_rows_for_sheet(sheet)?)
pub fn csv_for_sheet<W>(&self, sheet: u32, sink: W) -> Result<()>
where W: std::io::Write,
{
let rows = self.get_export_rows_for_sheet(sheet)?;
let mut writer = csv::Writer::from_writer(sink);
for row in rows {
writer.write_record(row)?;
}
Ok(())
}
pub fn export_rows_for_sheet(&self, sheet: u32) -> Result<Vec<Vec<String>>, anyhow::Error> {
pub fn get_export_rows(&self) -> Result<Vec<Vec<String>>> {
let sheet = self.location.sheet;
Ok(self.get_export_rows_for_sheet(sheet)?)
}
pub fn get_export_rows_for_sheet(&self, sheet: u32) -> Result<Vec<Vec<String>>, anyhow::Error> {
let worksheet = self
.model
.get_model()
@ -170,6 +181,15 @@ impl Book {
Ok(Self::from_model(load_from_xlsx(path, locale, tz)?))
}
/// Save a sheet in the book to a csv file
pub fn save_sheet_to_csv(&self, sheet: u32, path: &str) -> Result<()> {
let file_path = std::path::Path::new(path);
let file = std::fs::File::create(file_path)?;
let writer = std::io::BufWriter::new(file);
self.csv_for_sheet(sheet, writer)?;
Ok(())
}
/// Save book to an xlsx file.
pub fn save_to_xlsx(&mut self, path: &str) -> Result<()> {
// TODO(zaphar): Currently overwrites. Should we prompt in this case?