excel_cli/utils/
helpers.rs1pub fn index_to_col_name(index: usize) -> String {
2 let mut col_name = String::new();
3 let mut n = index;
4
5 while n > 0 {
6 let remainder = (n - 1) % 26;
7 col_name.insert(0, (b'A' + remainder as u8) as char);
8 n = (n - 1) / 26;
9 }
10
11 if col_name.is_empty() {
12 col_name.push('A');
13 }
14
15 col_name
16}
17
18pub fn col_name_to_index(name: &str) -> Option<usize> {
19 let mut result = 0;
20
21 for c in name.chars() {
22 if !c.is_ascii_alphabetic() {
23 return None;
24 }
25
26 let val = (c.to_ascii_uppercase() as u8 - b'A' + 1) as usize;
27 result = result * 26 + val;
28 }
29
30 Some(result)
31}
32
33pub fn cell_reference(cell: (usize, usize)) -> String {
35 format!("{}{}", index_to_col_name(cell.1), cell.0)
36}