Skip to content

Commit

Permalink
add cell address.
Browse files Browse the repository at this point in the history
  • Loading branch information
pt committed Apr 17, 2024
1 parent fbf8574 commit e65a456
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/xml/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod cell_ref;
pub(crate) mod cell;

extern crate proc_macro;
use std::hash::Hash;
Expand Down
3 changes: 3 additions & 0 deletions src/xml/common/cell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub(crate) mod cell_address;
pub(crate) mod cell_address_range;
pub(crate) mod cell_reference;
41 changes: 41 additions & 0 deletions src/xml/common/cell/cell_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::utils::col_helper;

#[derive(Default, Debug, Copy, Clone)]
pub(crate) struct CellAddress {
row: u32,
col: u32,
}

impl CellAddress {
fn new(row: u32, col: u32) -> CellAddress {
CellAddress {
row,
col,
}
}
fn from_str(refer: &str) -> CellAddress {
let mut coord = CellAddress::default();
let mut row = String::new();
let mut col = String::new();
for c in refer.chars() {
match c {
'A'..='Z' | 'a'..='z' => {
col.push(c);
},
'0'..='9' => {
row.push(c);
},
_ => {},
}
}
coord.row = row.parse().unwrap_or_default();
coord.col = col_helper::to_col(&col);
coord
}
fn to_string(&self) -> String {
let mut refer = String::new();
refer.push_str(&col_helper::to_col_name(self.col));
refer.push_str(&self.row.to_string());
refer
}
}
51 changes: 51 additions & 0 deletions src/xml/common/cell/cell_address_range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::utils::col_helper;

#[derive(Default, Debug, Copy, Clone)]
struct CellAddressRange {
first_row: u32,
first_col: u32,
last_row: u32,
last_col: u32,
}

impl CellAddressRange {
fn new(first_row: u32, first_col: u32, last_row: u32, last_col: u32) -> CellAddressRange {
CellAddressRange {
first_row,
first_col,
last_row,
last_col,
}
}

fn from_str(refer: &str) -> CellAddressRange {
let mut cell_address_range = CellAddressRange::default();
let cell_addresses: Vec<&str> = refer.split(":").collect();
match cell_addresses.len() {
1 => {
(cell_address_range.first_row, cell_address_range.first_col) =
col_helper::to_loc(cell_addresses[0]);
(cell_address_range.last_row, cell_address_range.last_col) =
(cell_address_range.first_row, cell_address_range.first_col)
},
2 => {
(cell_address_range.first_row, cell_address_range.first_col) =
col_helper::to_loc(cell_addresses[0]);
(cell_address_range.last_row, cell_address_range.last_col) =
col_helper::to_loc(cell_addresses[1]);
},
_ => {}
}
cell_address_range
}

fn to_string(&self) -> String {
todo!()
// return the string like A1:B2 format
}

fn to_cell_address_string(&self) -> String {
todo!()
// just return the start address string like A1 format
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use crate::utils::col_helper;

#[derive(Default, Debug, Copy, Clone)]
pub(crate) struct Coordinate {
pub(crate) struct CellReference {
row: u32,
col: u32,
abs_row: bool,
abs_col: bool,
}

impl Coordinate {
fn new(row: u32, col: u32, abs_row: bool, abs_col: bool) -> Coordinate {
Coordinate {
impl CellReference {
fn new(row: u32, col: u32, abs_row: bool, abs_col: bool) -> CellReference {
CellReference {
row,
col,
abs_row,
abs_col,
}
}
fn from_str(refer: &str) -> Coordinate {
let mut coord = Coordinate::default();
fn from_str(refer: &str) -> CellReference {
let mut coord = CellReference::default();
let mut row = String::new();
let mut col = String::new();
let mut dollar = None;
Expand Down Expand Up @@ -56,8 +56,4 @@ impl Coordinate {
refer.push_str(&self.row.to_string());
refer
}
}

pub(crate) struct CellReference {
coords: Vec<Coordinate>
}

0 comments on commit e65a456

Please sign in to comment.