From 2cd0bc454fbe11a9da260b7a15a028684ae572af Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Tue, 8 Apr 2025 23:05:23 -0400 Subject: [PATCH] wip: convert a sheet into clipboard format --- src/book/mod.rs | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index eccb9ba..6c9019c 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -132,27 +132,19 @@ impl Book { Ok(()) } - /// Construct a payload of (html, csv_text) for the address range. - pub fn range_to_clipboard_content(&self, range: AddressRange) -> Result<(String, String), anyhow::Error> { - use html::tables; - let rows = self.get_rows_for_range(&range).unwrap_or_default(); - let mut html = tables::Table::builder(); - let mut writer = csv::Writer::from_writer(vec![]); - for row in rows { - let mut html_row = tables::TableRow::builder(); - writer.write_record(&row)?; - for cell in row { - let mut html_cell = tables::TableCell::builder(); - html_cell.text(cell); - html_row.push(html_cell.build()); - } - html.push(html_row.build()); - } - - let csv_content = writer.into_inner().expect("Failed to get the csv content"); - Ok((html.build().to_string(), String::from_utf8_lossy(&csv_content).to_string())) + /// Construct a payload of (html, csv_text) for a sheet. + pub fn sheeet_to_clipboard_content(&self, sheet: u32) -> Result<(String, String), anyhow::Error> { + let rows = self.get_export_rows_for_sheet(sheet)?; + rows_to_clipboard_content(rows) } + /// Construct a payload of (html, csv_text) for the address range. + pub fn range_to_clipboard_content(&self, range: AddressRange) -> Result<(String, String), anyhow::Error> { + let rows = self.get_rows_for_range(&range).unwrap_or_default(); + rows_to_clipboard_content(rows) + } + + /// Get rows for current sheet to export. pub fn get_export_rows(&self) -> Result>> { let sheet = self.location.sheet; Ok(self.get_export_rows_for_sheet(sheet)?) @@ -730,6 +722,25 @@ impl Book { } } +fn rows_to_clipboard_content(rows: Vec>) -> std::result::Result<(String, String), anyhow::Error> { + use html::tables; + let mut html = tables::Table::builder(); + let mut writer = csv::Writer::from_writer(vec![]); + for row in rows { + let mut html_row = tables::TableRow::builder(); + writer.write_record(&row)?; + for cell in row { + let mut html_cell = tables::TableCell::builder(); + html_cell.text(cell); + html_row.push(html_cell.build()); + } + html.push(html_row.build()); + } + + let csv_content = writer.into_inner().expect("Failed to get the csv content"); + Ok((html.build().to_string(), String::from_utf8_lossy(&csv_content).to_string())) +} + fn calculate_area(sheet: u32, start: &Address, end: &Address) -> Area { let area = Area { sheet,