1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
//! # a1_notation
//!
//! A library for parsing to and from A1 spreadsheet notation. A1 notation uses letters A-Z for
//! columns and a one-based number for the row. So for example at position `(0, 0)` of a spreadsheet
//! (the top left corner) is "A1". `(1, 1)` is "B2", `(1, 0)` is "B1", etc.
//!
//! ## Instantiating `A1`s
//!
//! The most common need is to parse a string:
//!
//! ```
//! use a1_notation::{A1, Address, Column, RangeOrCell, Row};
//! use std::str::FromStr;
//!
//! let a1 = A1::from_str("A1").unwrap();
//! // it parses it into an instance:
//! assert_eq!(a1,
//! A1 {
//! sheet_name: None,
//! reference: RangeOrCell::Cell(Address {
//! column: Column { absolute: false, x: 0 },
//! row: Row { absolute: false, y: 0 },
//! }),
//! });
//! // and can display it back:
//! assert_eq!(&a1.to_string(), "A1");
//!
//! // you can also just call `a1_notation::new`:
//! let from_col_a_to_d = a1_notation::new("Foo!A:D").unwrap();
//! assert_eq!(from_col_a_to_d,
//! A1 {
//! sheet_name: Some("Foo".to_string()),
//! reference: RangeOrCell::ColumnRange {
//! from: Column { absolute: false, x: 0 },
//! to: Column { absolute: false, x: 3 },
//! },
//! });
//!
//! assert_eq!(&from_col_a_to_d.to_string(), "Foo!A:D");
//! ```
//!
//! If you have zero-based coordinates and want to represent them as A1, there are several `fn`s
//! for instantiating:
//!
//! ```
//! # use a1_notation::*;
//! // to create a reference to a specific cell:
//! assert_eq!(&a1_notation::cell(2, 2).to_string(), "C3");
//!
//! // a reference to an entire column
//! assert_eq!(&a1_notation::column(5).to_string(), "F:F");
//!
//! // or an entire row
//! assert_eq!(&a1_notation::row(5).to_string(), "6:6");
//!
//! // and finally a range between two cells:
//! assert_eq!(&a1_notation::range((0, 0), (4, 4)).to_string(), "A1:E5");
//! ```
//!
//! ## Contains
//!
//! Given all the various combinations or cells, ranges, row ranges, column ranges and
//! non-contiguous ranges you can calculate if one reference contains another.
//!
//! ```
//! # use a1_notation::*;
//! // a column contains any cell in that column:
//! let col_a = a1_notation::new("A:A").unwrap();
//! let a1 = a1_notation::new("A1").unwrap();
//! assert!(col_a.contains(&a1));
//!
//! // likewise, a row range contains anything between it:
//! let top_5_rows = a1_notation::new("1:5").unwrap();
//! let b2 = a1_notation::new("B2").unwrap();
//! assert!(top_5_rows.contains(&b2));
//!
//! // and a range between two points works as you'd expect (it forms a rectangle)
//! let c3_to_j20 = a1_notation::new("C3:J20").unwrap();
//! let d5 = a1_notation::new("D5").unwrap();
//! assert!(c3_to_j20.contains(&d5));
//! ```
//!
//! ## Into/From/AsRef impls
//!
//! As much as possible it implements `Into`/`From` and `AsRef` to convert between the various
//! structs. Generally you can go from more specific to less specific but not the other way
//! around. You typically should work with `A1` structs but you can also use these traits to work
//! with these lower level ones and cast them upwards.
//!
//! ```
//! # use a1_notation::*;
//! // an address can act as a column or row using AsRef:
//! let a1 = Address::new(0, 0);
//! assert_eq!(&Column::new(0), a1.as_ref());
//! assert_eq!(&Row::new(0), a1.as_ref());
//!
//! // addresses, columns and rows can `into()` "upwards" to an A1 or RangeOrCell
//! let col_b = Column::new(1);
//! assert_eq!(
//! RangeOrCell::ColumnRange {
//! from: Column::new(1),
//! to: Column::new(1),
//! },
//! col_b.into());
//!
//! assert_eq!(
//! A1 {
//! sheet_name: None,
//! reference: RangeOrCell::ColumnRange {
//! from: Column::new(1),
//! to: Column::new(1),
//! },
//! },
//! col_b.into());
//! ```
//!
//! ## Shifting
//!
//! You can move references (and ranges) around:
//!
//! ```
//! # use a1_notation::*;
//! // A1 -> D1 -> D3 -> C3
//! assert_eq!(
//! &a1_notation::cell(0, 0)
//! .shift_right(3)
//! .shift_down(2)
//! .shift_left(1)
//! .to_string(),
//! "C3");
//! ```
//!
//! ## Iterators
//!
//! You can iterate through the various types of ranges.
//!
//! ```
//! # use crate::*;
//! // a cell just emits itself (once)
//! assert_eq!(
//! a1_notation::cell(0, 0)
//! .iter().map(|r| r.to_string()).collect::<Vec<_>>(),
//! vec!["A1"]);
//!
//! // a column range iterates column-wise
//! assert_eq!(
//! a1_notation::new("D:G").unwrap()
//! .iter().map(|r| r.to_string()).collect::<Vec<_>>(),
//! vec!["D:D", "E:E", "F:F", "G:G"]);
//!
//! // and a row range goes row-wise
//! assert_eq!(
//! a1_notation::new("3:6").unwrap()
//! .iter().map(|r| r.to_string()).collect::<Vec<_>>(),
//! vec!["3:3", "4:4", "5:5", "6:6"]);
//!
//! // a grid-based range goes row-by-row
//! assert_eq!(
//! a1_notation::new("A1:C3").unwrap()
//! .iter().map(|r| r.to_string()).collect::<Vec<_>>(),
//! vec![
//! "A1", "B1", "C1",
//! "A2", "B2", "C2",
//! "A3", "B3", "C3",
//! ]);
//! ```
//!
//! ### A1 Reference Examples
//!
//! Here is a table illustrating A1 references:
//!
//! | **Reference** | **Meaning** |
//! |:----------------|:--------------------------|
//! | `"A1"` | Cell A1 |
//! | `"A1:B5"` | Cells A1 through B5 |
//! | `"C5:D9,G9:H16"`| A multiple-area selection |
//! | `"A:A"` | Column A |
//! | `"1:1"` | Row 1 |
//! | `"A:C"` | Columns A through C |
//! | `"1:5"` | Rows 1 through 5 |
//! | `"1:1,3:3,8:8"` | Rows 1, 3, and 8 |
//! | `"A:A,C:C,F:F"` | Columns A, C, and F |
//!
//
// TODO:
//
// * implement `IntoIterator` for `RangeOrCell`
//
use std::str::FromStr;
mod a1;
mod address;
mod column;
mod error;
mod range_or_cell;
mod row;
pub use a1::A1;
pub use address::Address;
pub use column::Column;
pub use error::Error;
pub use range_or_cell::RangeOrCell;
pub use row::Row;
pub type Result<T> = std::result::Result<T, Error>;
pub type Index = usize;
pub(crate) static ALPHA: [char; 26] = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];
/// Create an `A1` referencing a cell at a given address (x/y)
pub fn cell(x: Index, y: Index) -> A1 {
A1 {
sheet_name: None,
reference: RangeOrCell::Cell(Address::new(x, y)),
}
}
/// Parse an A1-style string into an `A1`
pub fn new(s: &str) -> Result<A1> {
A1::from_str(s)
}
/// Create a range between two cells
pub fn range<A: Into<Address>>(from: A, to: A) -> A1 {
A1 {
sheet_name: None,
reference: RangeOrCell::Range {
from: from.into(),
to: to.into(),
},
}
}
/// An entire column
pub fn column<C: Into<Column>>(x: C) -> A1 {
A1 {
sheet_name: None,
reference: RangeOrCell::column(x),
}
}
/// A range between two columns
pub fn column_range<R: Into<Column>>(xa: R, xb: R) -> A1 {
A1 {
sheet_name: None,
reference: RangeOrCell::column_range(xa, xb),
}
}
/// An entire row
pub fn row<R: Into<Row>>(y: R) -> A1 {
A1 {
sheet_name: None,
reference: RangeOrCell::row(y),
}
}
/// A range between two rows
pub fn row_range<R: Into<Row>>(ya: R, yb: R) -> A1 {
A1 {
sheet_name: None,
reference: RangeOrCell::row_range(ya, yb),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cell() {
let a1 = crate::cell(2, 3);
assert_eq!(a1.sheet_name, None);
assert_eq!(a1.reference, RangeOrCell::Cell(Address::new(2, 3)));
}
#[test]
fn new() {
let a1 = crate::new("Foo!A1").unwrap();
assert_eq!(a1.sheet_name, Some("Foo".to_string()));
assert_eq!(a1.reference, RangeOrCell::Cell(Address::new(0, 0)));
}
#[test]
fn range() {
let a1 = crate::range((0, 0), (22, 33));
assert_eq!(a1.sheet_name, None);
assert_eq!(
a1.reference,
RangeOrCell::Range {
from: Address::new(0, 0),
to: Address::new(22, 33),
}
);
}
#[test]
fn column() {
let a1 = crate::column(22);
assert_eq!(a1.sheet_name, None);
assert_eq!(
a1.reference,
RangeOrCell::ColumnRange {
from: Column::new(22),
to: Column::new(22),
}
);
}
#[test]
fn column_range() {
let a1 = crate::column_range(1, 420);
assert_eq!(a1.sheet_name, None);
assert_eq!(
a1.reference,
RangeOrCell::ColumnRange {
from: Column::new(1),
to: Column::new(420),
}
);
}
#[test]
fn row() {
let a1 = crate::row(11);
assert_eq!(a1.sheet_name, None);
assert_eq!(
a1.reference,
RangeOrCell::RowRange {
from: Row::new(11),
to: Row::new(11),
}
);
}
#[test]
fn row_range() {
let a1 = crate::row_range(0, 42);
assert_eq!(a1.sheet_name, None);
assert_eq!(
a1.reference,
RangeOrCell::RowRange {
from: Row::new(0),
to: Row::new(42),
}
);
}
}