Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
DESKTOP-U434MT0\hiro committed Sep 23, 2024
1 parent 647c3a2 commit 03f078d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/helper/formula.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,66 @@ pub(crate) fn render(formula_token_list: &[FormulaToken]) -> String {
result
}

pub fn adjustment_formula_coordinate(
token_list: &mut [FormulaToken],
offset_col_num: &i32,
offset_row_num: &i32,
) {
for token in token_list.into_iter() {
if token.get_token_type() == &FormulaTokenTypes::Operand
&& token.get_token_sub_type() == &FormulaTokenSubTypes::Range
{
let (sheet_name, range) = split_address(token.get_value());
let mut coordinate_list_new: Vec<String> = Vec::new();
let coordinate_list = get_split_range(range);
let mut has_error = false;
for coordinate in &coordinate_list {
let cell = index_from_coordinate(coordinate);
if cell.0.is_some() {
let mut col_num = cell.0.unwrap();
let mut row_num = cell.1.unwrap();
let is_lock_col = cell.2.unwrap();
let is_lock_row = cell.3.unwrap();
if !is_lock_col {
let calc_col_num = col_num as i32 + offset_col_num;
if calc_col_num < 1 {
has_error = true;
break;
} else {
col_num = calc_col_num as u32;
}
}
if !is_lock_row {
let calc_row_num = row_num as i32 + offset_row_num;
if calc_row_num < 1 {
has_error = true;
break;
} else {
row_num = calc_row_num as u32;
}
}
let new_corrdinate = coordinate_from_index_with_lock(
&col_num,
&row_num,
&is_lock_col,
&is_lock_row,
);
coordinate_list_new.push(new_corrdinate);
} else {
coordinate_list_new.push(coordinate.to_string());
}
}
if has_error {
token.set_value("#REF!");
token.set_token_sub_type(FormulaTokenSubTypes::Error);
} else {
let new_value = join_address(sheet_name, &get_join_range(&coordinate_list_new));
token.set_value(new_value);
}
}
}
}

pub fn adjustment_insert_formula_coordinate(
token_list: &mut [FormulaToken],
root_col_num: &u32,
Expand Down
25 changes: 25 additions & 0 deletions src/structs/cell.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use hashbrown::HashMap;
use helper::coordinate::*;
use helper::formula::*;
use helper::number_format::*;
use quick_xml::events::{BytesStart, Event};
Expand Down Expand Up @@ -68,6 +69,30 @@ impl Cell {
&mut self.coordinate
}

/// Change the coordinate.
/// Change the formula address as well.
pub fn set_coordinate<T>(&mut self, coordinate: T) -> &mut Self
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();

let formula = self.cell_value.get_formula();
if formula != "" {
let org_col_num = self.coordinate.get_col_num();
let org_row_num = self.coordinate.get_row_num();
let offset_col_num = col as i32 - *org_col_num as i32;
let offset_row_num = row as i32 - *org_row_num as i32;
let mut tokens = parse_to_tokens(format!("={}", formula));
adjustment_formula_coordinate(&mut tokens, &offset_col_num, &offset_row_num);
let result_formula = render(tokens.as_ref());
self.cell_value.set_formula(result_formula);
}
self.coordinate.set_col_num(col);
self.coordinate.set_row_num(row);
self
}

pub fn get_hyperlink(&self) -> Option<&Hyperlink> {
self.hyperlink.as_ref()
}
Expand Down
2 changes: 2 additions & 0 deletions src/structs/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ impl Coordinate {
self
}

/// Change coordinates
/// Formula is not updated.
pub fn set_coordinate<S: AsRef<str>>(&mut self, value: S) -> &mut Self {
let (c, r, cl, rl) = index_from_coordinate(value.as_ref());

Expand Down
22 changes: 22 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,28 @@ fn issue_185() {
);
}

#[test]
fn issue_187() {
let path = std::path::Path::new("./tests/test_files/issue_187.xlsx");
let mut book = umya_spreadsheet::reader::xlsx::read(path).unwrap();
let mut sheet = book.get_sheet_mut(&0).unwrap();

let mut cell = sheet.get_cell("C4").unwrap().clone();
cell.set_coordinate("B5");
sheet.set_cell(cell);

let mut cell = sheet.get_cell("C4").unwrap().clone();
cell.set_coordinate("D6");
sheet.set_cell(cell);

let mut cell = sheet.get_cell("C4").unwrap().clone();
cell.set_coordinate("C7");
sheet.set_cell(cell);

let path = std::path::Path::new("./tests/result_files/issue_187.xlsx");
let _ = umya_spreadsheet::writer::xlsx::write(&book, path);
}

#[test]
fn issue_188() {
let path = std::path::Path::new("./tests/test_files/issue_188.xlsx");
Expand Down
Binary file added tests/test_files/issue_187.xlsx
Binary file not shown.

0 comments on commit 03f078d

Please sign in to comment.