diff --git a/README.md b/README.md index db2dc3087..90fdf9ee1 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,24 @@ Programmed in Rust, you will be able to use it from a variety of programming lan We will build different _skins_: in the terminal, as a desktop application or use it in you own web application. +## Haiku Interlude + +Rust winds compile dreams +Cells whisper through iron grids +Sheets bloom into night + +Code flows like clear streams +Formulas find gentle homes +Numbers learn to sing + +Data sparks like dawn +Pivot tales weave patient threads +Insights stretch their wings + +Engines hum soft tunes +Concurrency keeps the beat +Models wake with light + # Building ```bash diff --git a/xlsx/examples/hello_calc.rs b/xlsx/examples/hello_calc.rs index f3478d2bf..49b34d600 100644 --- a/xlsx/examples/hello_calc.rs +++ b/xlsx/examples/hello_calc.rs @@ -23,6 +23,10 @@ fn main() -> Result<(), Box> { model.evaluate(); // saves to disk - save_to_xlsx(&model, "hello-calc.xlsx")?; + let output_path = "hello-calc.xlsx"; + if std::path::Path::new(output_path).exists() { + std::fs::remove_file(output_path)?; + } + save_to_xlsx(&model, output_path)?; Ok(()) } diff --git a/xlsx/examples/hello_styles.rs b/xlsx/examples/hello_styles.rs index 437c77b0f..2c40a321b 100644 --- a/xlsx/examples/hello_styles.rs +++ b/xlsx/examples/hello_styles.rs @@ -12,6 +12,10 @@ fn main() -> Result<(), Box> { model.set_cell_style(sheet, row, column, &style)?; // saves to disk - save_to_xlsx(&model, "hello-styles.xlsx")?; + let output_path = "hello-styles.xlsx"; + if std::path::Path::new(output_path).exists() { + std::fs::remove_file(output_path)?; + } + save_to_xlsx(&model, output_path)?; Ok(()) } diff --git a/xlsx/examples/widths_and_heights.rs b/xlsx/examples/widths_and_heights.rs index ebbfe8b7f..46bbcbfd5 100644 --- a/xlsx/examples/widths_and_heights.rs +++ b/xlsx/examples/widths_and_heights.rs @@ -14,6 +14,10 @@ fn main() -> Result<(), Box> { worksheet.set_row_height(row, row_height)?; // saves to disk - save_to_xlsx(&model, "widths-and-heights.xlsx")?; + let output_path = "widths-and-heights.xlsx"; + if std::path::Path::new(output_path).exists() { + std::fs::remove_file(output_path)?; + } + save_to_xlsx(&model, output_path)?; Ok(()) } diff --git a/xlsx/src/import/worksheets.rs b/xlsx/src/import/worksheets.rs index f3e0bd954..39f94f825 100644 --- a/xlsx/src/import/worksheets.rs +++ b/xlsx/src/import/worksheets.rs @@ -267,29 +267,36 @@ enum ParseReferenceError { // NB: Maybe use regexes for this? fn parse_reference(s: &str) -> Result { let bytes = s.as_bytes(); - let mut sheet_name = "".to_string(); - let mut column = "".to_string(); - let mut row = "".to_string(); + let mut sheet_name = String::new(); + let mut column = String::new(); + let mut row = String::new(); let mut state = "sheet"; // "sheet", "col", "row" for &byte in bytes { match state { "sheet" => { if byte == b'!' { - state = "col" + state = "col"; } else { sheet_name.push(byte as char); } } "col" => { + if byte == b'$' { + continue; + } if byte.is_ascii_alphabetic() { - column.push(byte as char); + column.push((byte as char).to_ascii_uppercase()); } else { state = "row"; - row.push(byte as char); + if byte != b'$' { + row.push(byte as char); + } } } _ => { - row.push(byte as char); + if byte != b'$' { + row.push(byte as char); + } } } } @@ -300,6 +307,31 @@ fn parse_reference(s: &str) -> Result { }) } +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_reference_supports_absolute_references() { + let reference = parse_reference("Sheet 1!$B$12").unwrap(); + assert_eq!(reference.sheet, "Sheet 1"); + assert_eq!(reference.column, 2); + assert_eq!(reference.row, 12); + } + + #[test] + fn parse_reference_supports_mixed_absolute_relative() { + let reference = parse_reference("Data!$C4").unwrap(); + assert_eq!(reference.sheet, "Data"); + assert_eq!(reference.column, 3); + assert_eq!(reference.row, 4); + let reference = parse_reference("Pivot!D$7").unwrap(); + assert_eq!(reference.sheet, "Pivot"); + assert_eq!(reference.column, 4); + assert_eq!(reference.row, 7); + } +} + fn from_a1_to_rc( formula: String, worksheets: &[String],