Skip to content

Commit

Permalink
wip: feat: method to save sheet rows to csv file
Browse files Browse the repository at this point in the history
  • Loading branch information
zaphar committed Mar 5, 2025
1 parent 473ba9c commit 8cd93cb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
16 changes: 3 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
24 changes: 22 additions & 2 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,23 @@ impl Book {
)?))
}

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 get_export_rows(&self) -> Result<Vec<Vec<String>>> {
let sheet = self.location.sheet;
Ok(self.export_rows_for_sheet(sheet)?)
Ok(self.get_export_rows_for_sheet(sheet)?)
}

pub fn export_rows_for_sheet(&self, sheet: u32) -> Result<Vec<Vec<String>>, anyhow::Error> {
pub fn get_export_rows_for_sheet(&self, sheet: u32) -> Result<Vec<Vec<String>>, anyhow::Error> {
let worksheet = self
.model
.get_model()
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit 8cd93cb

Please sign in to comment.