diff --git a/src/book/mod.rs b/src/book/mod.rs index 1ebb658..bcd1543 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -3,10 +3,7 @@ use std::cmp::max; use anyhow::{anyhow, Result}; use ironcalc::{ base::{ - expressions::types::Area, - types::{Border, Col, Fill, Font, Row, SheetData, Style, Worksheet}, - worksheet::WorksheetDimension, - Model, UserModel, + calc_result::Range, expressions::types::Area, types::{Border, Col, Fill, Font, Row, SheetData, Style, Worksheet}, worksheet::WorksheetDimension, Model, UserModel }, export::save_xlsx_to_writer, import::load_from_xlsx, @@ -150,12 +147,13 @@ impl Book { } pub fn new_sheet(&mut self, sheet_name: Option<&str>) -> Result<()> { - todo!("We need to figure out how to find the new sheet index so we can rename it"); - //let (_, idx) = self.model.new_sheet(); - //if let Some(name) = sheet_name { - // self.set_sheet_name(idx as usize, name)?; - //} - //Ok(()) + self.model.new_sheet().map_err(|e| anyhow!(e))?; + let idx = self.model.get_selected_sheet(); + if let Some(name) = sheet_name { + self.set_sheet_name(idx as usize, name)?; + } + self.model.set_selected_sheet(self.current_sheet).map_err(|e| anyhow!(e))?; + Ok(()) } /// Get the sheet data for the current worksheet. @@ -347,11 +345,12 @@ impl Book { } } - pub fn set_cell_style(&mut self, style: &Style, sheet: u32, cell: &Address) -> Result<()> { - todo!() - //self.model.set_cell_style(sheet, cell.row as i32, cell.col as i32, style) - // .map_err(|s| anyhow!("Unable to format cell {}", s))?; - //Ok(()) + pub fn set_cell_style(&mut self, style: &[(&str, &str)], area: &Area) -> Result<()> { + for (path, val) in style { + self.model.update_range_style(area, path, val) + .map_err(|s| anyhow!("Unable to format cell {}", s))?; + } + Ok(()) } pub fn set_col_style(&mut self, style: &Style, sheet: u32, col: usize) -> Result<()> { @@ -582,6 +581,7 @@ impl Book { .map_err(|s| anyhow!("Invalid Worksheet: {}", s))? .name) } + pub(crate) fn get_sheet_by_idx_mut(&mut self, idx: usize) -> Result<&mut Worksheet> { todo!("Is there a clean way to do this with UserModel?") //Ok(self diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 4ef356b..752ae75 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -5,7 +5,7 @@ use crate::book::{AddressRange, Book}; use anyhow::{anyhow, Result}; use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers}; -use ironcalc::base::Model; +use ironcalc::base::{expressions::types::Area, Model}; use ratatui::{ buffer::Buffer, layout::{Constraint, Flex, Layout}, @@ -494,25 +494,26 @@ impl<'ws> Workspace<'ws> { Ok(None) } Ok(Some(Cmd::ColorCell(color))) => { - if let Some((start, end)) = self.state.range_select.get_range() { - for ri in start.row..=end.row { - for ci in start.col..=end.col { - let address = Address { row: ri, col: ci }; - let sheet = self.book.current_sheet; - let mut style = self.book.get_cell_style(sheet, &address) - .expect("I think this should be impossible.").clone(); - style.fill.bg_color = Some(color.to_string()); - self.book.set_cell_style(&style, sheet, &address)?; - } + let sheet = self.book.current_sheet; + let area = if let Some((start, end)) = self.state.range_select.get_range() { + Area { + sheet, + row: start.row as i32, + column: start.col as i32, + width: (end.col - start.col) as i32, + height: (end.row - start.row) as i32 } } else { let address = self.book.location.clone(); - let sheet = self.book.current_sheet; - let mut style = self.book.get_cell_style(sheet, &address) - .expect("I think this should be impossible.").clone(); - style.fill.bg_color = Some(color.to_string()); - self.book.set_cell_style(&style, sheet, &address)?; - } + Area { + sheet, + row: address.row as i32, + column: address.col as i32, + width: 1, + height: 1 + } + }; + self.book.set_cell_style(&[("fill.bg_color", color)], &area)?; Ok(None) } Ok(None) => {