wip: new_sheet and cell styling with UserModel

This commit is contained in:
Jeremy Wall 2025-02-04 19:27:13 -05:00
parent 7a5bd63fde
commit d8b3191612
2 changed files with 33 additions and 32 deletions

View File

@ -3,10 +3,7 @@ use std::cmp::max;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use ironcalc::{ use ironcalc::{
base::{ base::{
expressions::types::Area, calc_result::Range, expressions::types::Area, types::{Border, Col, Fill, Font, Row, SheetData, Style, Worksheet}, worksheet::WorksheetDimension, Model, UserModel
types::{Border, Col, Fill, Font, Row, SheetData, Style, Worksheet},
worksheet::WorksheetDimension,
Model, UserModel,
}, },
export::save_xlsx_to_writer, export::save_xlsx_to_writer,
import::load_from_xlsx, import::load_from_xlsx,
@ -150,12 +147,13 @@ impl Book {
} }
pub fn new_sheet(&mut self, sheet_name: Option<&str>) -> Result<()> { 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"); self.model.new_sheet().map_err(|e| anyhow!(e))?;
//let (_, idx) = self.model.new_sheet(); let idx = self.model.get_selected_sheet();
//if let Some(name) = sheet_name { if let Some(name) = sheet_name {
// self.set_sheet_name(idx as usize, name)?; self.set_sheet_name(idx as usize, name)?;
//} }
//Ok(()) self.model.set_selected_sheet(self.current_sheet).map_err(|e| anyhow!(e))?;
Ok(())
} }
/// Get the sheet data for the current worksheet. /// 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<()> { pub fn set_cell_style(&mut self, style: &[(&str, &str)], area: &Area) -> Result<()> {
todo!() for (path, val) in style {
//self.model.set_cell_style(sheet, cell.row as i32, cell.col as i32, style) self.model.update_range_style(area, path, val)
// .map_err(|s| anyhow!("Unable to format cell {}", s))?; .map_err(|s| anyhow!("Unable to format cell {}", s))?;
//Ok(()) }
Ok(())
} }
pub fn set_col_style(&mut self, style: &Style, sheet: u32, col: usize) -> Result<()> { 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))? .map_err(|s| anyhow!("Invalid Worksheet: {}", s))?
.name) .name)
} }
pub(crate) fn get_sheet_by_idx_mut(&mut self, idx: usize) -> Result<&mut Worksheet> { 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?") todo!("Is there a clean way to do this with UserModel?")
//Ok(self //Ok(self

View File

@ -5,7 +5,7 @@ use crate::book::{AddressRange, Book};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers}; use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use ironcalc::base::Model; use ironcalc::base::{expressions::types::Area, Model};
use ratatui::{ use ratatui::{
buffer::Buffer, buffer::Buffer,
layout::{Constraint, Flex, Layout}, layout::{Constraint, Flex, Layout},
@ -494,25 +494,26 @@ impl<'ws> Workspace<'ws> {
Ok(None) Ok(None)
} }
Ok(Some(Cmd::ColorCell(color))) => { Ok(Some(Cmd::ColorCell(color))) => {
if let Some((start, end)) = self.state.range_select.get_range() { let sheet = self.book.current_sheet;
for ri in start.row..=end.row { let area = if let Some((start, end)) = self.state.range_select.get_range() {
for ci in start.col..=end.col { Area {
let address = Address { row: ri, col: ci }; sheet,
let sheet = self.book.current_sheet; row: start.row as i32,
let mut style = self.book.get_cell_style(sheet, &address) column: start.col as i32,
.expect("I think this should be impossible.").clone(); width: (end.col - start.col) as i32,
style.fill.bg_color = Some(color.to_string()); height: (end.row - start.row) as i32
self.book.set_cell_style(&style, sheet, &address)?;
}
} }
} else { } else {
let address = self.book.location.clone(); let address = self.book.location.clone();
let sheet = self.book.current_sheet; Area {
let mut style = self.book.get_cell_style(sheet, &address) sheet,
.expect("I think this should be impossible.").clone(); row: address.row as i32,
style.fill.bg_color = Some(color.to_string()); column: address.col as i32,
self.book.set_cell_style(&style, sheet, &address)?; width: 1,
} height: 1
}
};
self.book.set_cell_style(&[("fill.bg_color", color)], &area)?;
Ok(None) Ok(None)
} }
Ok(None) => { Ok(None) => {