use super::zipxml::{MAX_UNCOMPRESSED_SIZE, attr, read_part, uncompressed_size};
use crate::error::{Error, Result};
use crate::model::{CellValue, ColumnRun, Hyperlink, LinkTarget, Spreadsheet, Worksheet};
use crate::shared::date::{DateTime, Epoch, to_serial};
use crate::shared::odf_formula;
use crate::style::{
Border, BorderStyle, Color, HorizontalAlign, NumberFormat, Pattern, Style, StyleId, Underline,
VerticalAlign,
};
use crate::{CellRef, Col, Range, Row};
use quick_xml::Reader;
use quick_xml::events::Event;
use std::collections::HashMap;
use std::io::{BufReader, Read, Seek};
const MIMETYPE: &str = "application/vnd.oasis.opendocument.spreadsheet";
pub fn read_ods(path: impl AsRef<std::path::Path>) -> Result<Spreadsheet> {
let file = std::fs::File::open(path).map_err(|e| Error::Ods(e.to_string()))?;
read_ods_from(BufReader::new(file))
}
pub fn read_ods_from<R: Read + Seek>(source: R) -> Result<Spreadsheet> {
let mut zip = zip::ZipArchive::new(source).map_err(|e| Error::Ods(e.to_string()))?;
let total = uncompressed_size(&mut zip);
if total > MAX_UNCOMPRESSED_SIZE {
return Err(Error::Ods(format!(
"package expands to {total} bytes, over the {MAX_UNCOMPRESSED_SIZE} limit"
)));
}
match read_part(&mut zip, "mimetype") {
Ok(kind) if kind.trim() == MIMETYPE => {}
Ok(kind) => return Err(Error::Ods(format!("not a spreadsheet: mimetype {kind:?}"))),
Err(e) => return Err(Error::Ods(e)),
}
let content = read_part(&mut zip, "content.xml").map_err(Error::Ods)?;
read_content(&content)
}
fn read_content(xml: &str) -> Result<Spreadsheet> {
let mut reader = Reader::from_str(xml);
reader.config_mut().trim_text(false);
let mut state = ContentReader::default();
loop {
match reader.read_event() {
Err(e) => return Err(Error::Ods(format!("content.xml: {e}"))),
Ok(Event::Eof) => break,
Ok(ev @ (Event::Start(_) | Event::Empty(_))) => {
let closed = matches!(ev, Event::Empty(_));
let (Event::Start(e) | Event::Empty(e)) = ev else {
continue;
};
let name = e.local_name().as_ref().to_owned();
state.start(&name, &e);
if closed {
state.end(&name);
}
}
Ok(Event::GeneralRef(r)) if state.in_text > 0 => {
super::zipxml::push_entity(&mut state.cell.text, &r);
}
Ok(Event::Text(t)) => {
if state.in_text > 0 {
state.cell.text.push_str(&t.xml10_content());
}
}
Ok(Event::End(e)) => state.end(e.local_name().as_ref()),
Ok(_) => {}
}
}
state.finish()
}
const MAX_SHEET_COPIES: u64 = 4_000_000;
struct ContentReader {
book: Spreadsheet,
sheet: Option<Worksheet>,
row: u64,
col: u64,
cell: CellState,
copies: u64,
in_text: usize,
names: Vec<(String, String)>,
styles: HashMap<String, StyleId>,
building: Option<(String, Style)>,
pending: Vec<(u64, u64, StyleId)>,
}
impl Default for ContentReader {
fn default() -> Self {
Self {
book: Spreadsheet::empty(),
sheet: None,
row: 1,
col: 1,
cell: CellState::default(),
copies: 0,
in_text: 0,
names: Vec::new(),
styles: HashMap::new(),
building: None,
pending: Vec::new(),
}
}
}
impl ContentReader {
fn start(&mut self, name: &str, e: &quick_xml::events::BytesStart<'_>) {
let _ = self.start_grid(name, e) || self.start_style(name, e) || self.start_text(name, e);
}
fn start_grid(&mut self, name: &str, e: &quick_xml::events::BytesStart<'_>) -> bool {
match name {
"table" => {
self.close_sheet();
let title = attr(e, "name").unwrap_or_else(|| "Sheet".to_owned());
self.sheet = Some(
Worksheet::new(unique_title(&self.book, &title))
.unwrap_or_else(|_| Worksheet::default()),
);
self.row = 1;
}
"table-column" => {
if let Some(ws) = self.sheet.as_mut() {
let repeat = repeat_of(e, "number-columns-repeated");
column_run(ws, e, repeat);
}
}
"table-row" => {
self.col = 1;
let repeat = repeat_of(e, "number-rows-repeated");
if let Some(ws) = self.sheet.as_mut() {
row_properties(ws, e, self.row, repeat);
}
self.cell.row_repeat = repeat;
}
"table-cell" | "covered-table-cell" => {
self.cell = CellState {
row_repeat: self.cell.row_repeat,
repeat: repeat_of(e, "number-columns-repeated"),
value_type: attr(e, "value-type"),
value: attr(e, "value"),
date: attr(e, "date-value"),
time: attr(e, "time-value"),
boolean: attr(e, "boolean-value"),
string: attr(e, "string-value"),
formula: attr(e, "formula"),
columns_spanned: repeat_of(e, "number-columns-spanned"),
rows_spanned: repeat_of(e, "number-rows-spanned"),
matrix: attr(e, "number-matrix-columns-spanned").map(|across| {
(
across.parse().unwrap_or(1),
repeat_of(e, "number-matrix-rows-spanned"),
)
}),
style: attr(e, "style-name").and_then(|n| self.styles.get(&n).copied()),
..CellState::default()
};
}
_ => return false,
}
true
}
fn start_style(&mut self, name: &str, e: &quick_xml::events::BytesStart<'_>) -> bool {
match name {
"style" => {
if attr(e, "family").as_deref() == Some("table-cell")
&& let Some(named) = attr(e, "name")
{
self.building = Some((named, Style::default()));
}
}
"table-cell-properties" => {
if let Some((_, style)) = self.building.as_mut() {
cell_properties(style, e);
}
}
"paragraph-properties" => {
if let Some((_, style)) = self.building.as_mut() {
style.alignment.horizontal = horizontal_of(e);
}
}
"text-properties" => {
if let Some((_, style)) = self.building.as_mut() {
text_properties(style, e);
}
}
_ => return false,
}
true
}
fn start_text(&mut self, name: &str, e: &quick_xml::events::BytesStart<'_>) -> bool {
match name {
"p" | "span" => self.in_text += 1,
"a" => {
if let Some(href) = attr(e, "href") {
self.cell.link = Some(href);
}
self.in_text += 1;
}
"s" if self.in_text > 0 => self.cell.text.push(' '),
"tab" if self.in_text > 0 => self.cell.text.push('\t'),
"line-break" if self.in_text > 0 => self.cell.text.push('\n'),
"named-range" | "named-expression" => {
let target = attr(e, "cell-range-address")
.or_else(|| attr(e, "expression"))
.unwrap_or_default();
if let Some(named) = attr(e, "name") {
self.names.push((named, target));
}
}
_ => return false,
}
true
}
fn end(&mut self, name: &str) {
match name {
"table" => self.close_sheet(),
"table-row" => {
self.pending.clear();
self.row += self.cell.row_repeat.max(1);
self.cell.row_repeat = 1;
}
"table-cell" | "covered-table-cell" => {
if let Some(ws) = self.sheet.as_mut() {
place(
ws,
&mut self.book.styles,
&self.cell,
self.row,
self.col,
&mut self.pending,
&mut self.copies,
);
}
self.col += self.cell.repeat.max(1);
self.cell = CellState {
row_repeat: self.cell.row_repeat,
..CellState::default()
};
}
"style" => {
if let Some((named, style)) = self.building.take() {
let id = self.book.styles.intern(style);
self.styles.insert(named, id);
}
}
"p" => {
self.in_text = self.in_text.saturating_sub(1);
if self.in_text == 0 {
self.cell.text.push('\n');
}
}
"span" | "a" => self.in_text = self.in_text.saturating_sub(1),
_ => {}
}
}
fn close_sheet(&mut self) {
if let Some(done) = self.sheet.take() {
let _ = self.book.add_sheet(done);
}
self.copies = 0;
}
fn finish(mut self) -> Result<Spreadsheet> {
self.close_sheet();
if self.book.sheets().is_empty() {
return Err(Error::Ods("file declares no sheets".into()));
}
for (name, target) in self.names {
self.book.defined_names.push(crate::model::DefinedName {
name,
sheet: None,
formula: odf_formula::to_a1(&target),
hidden: false,
});
}
Ok(self.book)
}
}
#[derive(Default)]
struct CellState {
repeat: u64,
row_repeat: u64,
value_type: Option<String>,
value: Option<String>,
date: Option<String>,
time: Option<String>,
boolean: Option<String>,
string: Option<String>,
formula: Option<String>,
columns_spanned: u64,
rows_spanned: u64,
matrix: Option<(u64, u64)>,
style: Option<StyleId>,
text: String,
link: Option<String>,
}
fn place(
sheet: &mut Worksheet,
styles: &mut crate::style::StyleTable,
cell: &CellState,
row: u64,
col: u64,
pending: &mut Vec<(u64, u64, StyleId)>,
copies: &mut u64,
) {
let (value, format) = value_of(cell);
let has_value = !matches!(value, CellValue::Empty);
if !has_value && cell.link.is_none() {
if let Some(style) = cell.style.filter(|s| *s != StyleId::default()) {
pending.push((col, cell.repeat.max(1), style));
}
return;
}
let row_repeat = cell.row_repeat.max(1);
for (at_col, repeat, style) in pending.drain(..) {
for r in 0..row_repeat {
let Ok(row) = Row::from_one_based(row + r) else {
break;
};
if *copies >= MAX_SHEET_COPIES {
break;
}
for c in 0..repeat {
let Ok(column) = Col::from_one_based(at_col + c) else {
break;
};
if *copies >= MAX_SHEET_COPIES {
break;
}
*copies += u64::from(c > 0 || r > 0);
sheet.entry(CellRef::new(column, row)).style = style;
}
}
}
let repeat = cell.repeat.max(1);
let style = match (format, cell.style) {
(Some(code), _) => Some(styles.intern(Style {
number_format: NumberFormat::Custom(code),
..Style::default()
})),
(None, named) => named,
};
for r in 0..row_repeat {
let Ok(row) = Row::from_one_based(row + r) else {
return;
};
if *copies >= MAX_SHEET_COPIES {
return;
}
for c in 0..repeat {
let Ok(col) = Col::from_one_based(col + c) else {
break;
};
if *copies >= MAX_SHEET_COPIES {
break;
}
*copies += u64::from(c > 0 || r > 0);
let at = CellRef::new(col, row);
if has_value {
sheet.set(at, value.clone());
}
if let Some(style) = style {
sheet.entry(at).style = style;
}
if let Some(href) = &cell.link {
sheet.hyperlinks.push(link_of(at, href));
}
if c == 0 && r == 0 && (cell.columns_spanned > 1 || cell.rows_spanned > 1) {
let end = CellRef::new(
Col::from_one_based(
u64::from(col.one_based()) + cell.columns_spanned.max(1) - 1,
)
.unwrap_or(col),
Row::from_one_based(u64::from(row.one_based()) + cell.rows_spanned.max(1) - 1)
.unwrap_or(row),
);
sheet.merges.push(Range::new(at, end));
}
if let Some((across, down)) = cell.matrix.filter(|_| c == 0 && r == 0) {
let end = CellRef::new(
Col::from_one_based(u64::from(col.one_based()) + across.max(1) - 1)
.unwrap_or(col),
Row::from_one_based(u64::from(row.one_based()) + down.max(1) - 1)
.unwrap_or(row),
);
sheet.array_formulas.push(Range::new(at, end));
}
}
}
}
fn value_of(cell: &CellState) -> (CellValue, Option<String>) {
let shown = cell.text.trim_end_matches('\n');
let kind = cell.value_type.as_deref().unwrap_or("");
let (value, format) = match kind {
"float" | "currency" => (number(cell, shown), None),
"percentage" => (
number(cell, shown),
Some(percentage_format(shown).to_owned()),
),
"boolean" => (
CellValue::Bool(cell.boolean.as_deref() == Some("true")),
None,
),
"date" => match cell.date.as_deref().and_then(iso_serial) {
Some((serial, has_time)) => (
CellValue::Number(serial),
Some(if has_time {
"yyyy-mm-dd hh:mm:ss".to_owned()
} else {
"yyyy-mm-dd".to_owned()
}),
),
None => (CellValue::text(shown), None),
},
"time" => match cell.time.as_deref().and_then(duration_serial) {
Some(serial) => (
CellValue::Number(serial),
Some(if (0.0..1.0).contains(&serial) {
"hh:mm:ss".to_owned()
} else {
"[hh]:mm:ss".to_owned()
}),
),
None => (CellValue::text(shown), None),
},
"string" => (
CellValue::text(cell.string.as_deref().unwrap_or(shown)),
None,
),
_ if shown.is_empty() => (CellValue::Empty, None),
_ => (CellValue::text(shown), None),
};
match &cell.formula {
Some(formula) => (
CellValue::Formula {
formula: odf_formula::to_a1(formula),
cached: (!matches!(value, CellValue::Empty)).then(|| Box::new(value)),
},
format,
),
None => (value, format),
}
}
fn number(cell: &CellState, shown: &str) -> CellValue {
cell.value
.as_deref()
.and_then(|v| v.parse::<f64>().ok())
.or_else(|| shown.parse::<f64>().ok())
.map_or(CellValue::Empty, CellValue::Number)
}
fn percentage_format(shown: &str) -> &'static str {
match shown.split_once('.') {
None => "0%",
Some((_, fraction)) => {
let places = fraction.trim_end_matches('%').chars().count();
if places <= 1 { "0.0%" } else { "0.00%" }
}
}
}
fn iso_serial(iso: &str) -> Option<(f64, bool)> {
let (date, time) = match iso.split_once('T') {
Some((d, t)) => (d, Some(t)),
None => (iso, None),
};
let mut parts = date.split('-');
let year: i32 = parts.next()?.parse().ok()?;
let month: u32 = parts.next()?.parse().ok()?;
let day: u32 = parts.next()?.parse().ok()?;
let mut dt = DateTime::date(year, month, day);
if let Some(time) = time {
let mut clock = time.trim_end_matches('Z').split(':');
dt.hour = clock.next()?.parse().ok()?;
dt.minute = clock.next().and_then(|m| m.parse().ok()).unwrap_or(0);
dt.second = clock.next().and_then(|s| s.parse().ok()).unwrap_or(0.0);
}
let serial = to_serial(dt, Epoch::Windows1900).ok()?;
Some((serial, time.is_some()))
}
fn duration_serial(duration: &str) -> Option<f64> {
let (sign, body) = match duration.strip_prefix('-') {
Some(rest) => (-1.0, rest),
None => (1.0, duration),
};
let body = body.strip_prefix("PT")?;
let (mut hours, mut minutes, mut seconds) = (0.0f64, 0.0f64, 0.0f64);
let mut digits = String::new();
for c in body.chars() {
match c {
'H' => {
hours = digits.parse().ok()?;
digits.clear();
}
'M' => {
minutes = digits.parse().ok()?;
digits.clear();
}
'S' => {
seconds = digits.parse().ok()?;
digits.clear();
}
_ => digits.push(c),
}
}
Some(sign * (hours / 24.0 + minutes / 1440.0 + seconds / 86400.0))
}
fn link_of(at: CellRef, href: &str) -> Hyperlink {
let target = match href.strip_prefix('#') {
Some(place) => LinkTarget::Inside(odf_formula::to_a1(place)),
None => LinkTarget::Outside(href.to_owned()),
};
Hyperlink {
range: Range::new(at, at),
target,
display: None,
tooltip: None,
}
}
fn column_run(sheet: &mut Worksheet, e: &quick_xml::events::BytesStart<'_>, repeat: u64) {
let first = Col::from_one_based(next_column(sheet)).unwrap_or_default();
let last = Col::from_one_based(next_column(sheet) + repeat.max(1) - 1).unwrap_or(first);
let mut run = ColumnRun::new(first, last);
run.hidden = attr(e, "visibility").is_some_and(|v| v != "visible");
if run.is_meaningful() {
sheet.columns.push(run);
} else {
sheet.columns.push(ColumnRun::new(first, last));
}
}
fn next_column(sheet: &Worksheet) -> u64 {
sheet
.columns
.last()
.map_or(1, |run| u64::from(run.last.one_based()) + 1)
}
fn row_properties(
sheet: &mut Worksheet,
e: &quick_xml::events::BytesStart<'_>,
row: u64,
repeat: u64,
) {
let hidden = attr(e, "visibility").is_some_and(|v| v != "visible");
if !hidden {
return;
}
for r in 0..repeat.max(1) {
let Ok(row) = Row::from_one_based(row + r) else {
return;
};
sheet.rows.entry(row).or_default().hidden = true;
}
}
fn repeat_of(e: &quick_xml::events::BytesStart<'_>, name: &str) -> u64 {
attr(e, name).and_then(|v| v.parse().ok()).unwrap_or(1)
}
fn unique_title(book: &Spreadsheet, wanted: &str) -> String {
if book.sheet_by_name(wanted).is_none() {
return wanted.to_owned();
}
for n in 2..1000 {
let candidate = format!("{wanted} ({n})");
if book.sheet_by_name(&candidate).is_none() {
return candidate;
}
}
wanted.to_owned()
}
fn cell_properties(style: &mut Style, e: &quick_xml::events::BytesStart<'_>) {
if let Some(colour) = attr(e, "background-color").as_deref().and_then(colour_of) {
style.fill.pattern = Pattern::Solid;
style.fill.foreground = colour;
}
if let Some(all) = attr(e, "border") {
let border = border_of(&all);
style.borders.top = border.clone();
style.borders.right = border.clone();
style.borders.bottom = border.clone();
style.borders.left = border;
}
for (name, side) in [
("border-top", &mut style.borders.top),
("border-right", &mut style.borders.right),
("border-bottom", &mut style.borders.bottom),
("border-left", &mut style.borders.left),
] {
if let Some(value) = attr(e, name) {
*side = border_of(&value);
}
}
if let Some(vertical) = attr(e, "vertical-align") {
style.alignment.vertical = match vertical.as_str() {
"top" => VerticalAlign::Top,
"middle" => VerticalAlign::Center,
_ => VerticalAlign::Bottom,
};
}
if attr(e, "wrap-option").as_deref() == Some("wrap") {
style.alignment.wrap_text = true;
}
}
fn text_properties(style: &mut Style, e: &quick_xml::events::BytesStart<'_>) {
if let Some(name) = attr(e, "font-name").or_else(|| attr(e, "font-family")) {
style.font.name = name;
}
if let Some(size) = attr(e, "font-size")
.and_then(|s| s.trim_end_matches("pt").parse::<f64>().ok())
.filter(|s| *s > 0.0)
{
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "a font size is a small positive number of points"
)]
let hundredths = (size * 100.0).round() as u32;
style.font.size = hundredths;
}
if attr(e, "font-weight").as_deref() == Some("bold") {
style.font.bold = true;
}
if attr(e, "font-style").as_deref() == Some("italic") {
style.font.italic = true;
}
if attr(e, "text-underline-style").is_some_and(|v| v != "none") {
style.font.underline = Underline::Single;
}
if attr(e, "text-line-through-style").is_some_and(|v| v != "none") {
style.font.strike = true;
}
if let Some(colour) = attr(e, "color").as_deref().and_then(colour_of) {
style.font.color = colour;
}
}
fn horizontal_of(e: &quick_xml::events::BytesStart<'_>) -> HorizontalAlign {
match attr(e, "text-align").as_deref() {
Some("start" | "left") => HorizontalAlign::Left,
Some("center") => HorizontalAlign::Center,
Some("end" | "right") => HorizontalAlign::Right,
Some("justify") => HorizontalAlign::Justify,
_ => HorizontalAlign::General,
}
}
fn colour_of(value: &str) -> Option<Color> {
let hex = value.strip_prefix('#')?;
(hex.len() == 6)
.then(|| u32::from_str_radix(hex, 16).ok())
.flatten()
.map(|rgb| Color::Argb(0xFF00_0000 | rgb))
}
fn border_of(value: &str) -> Border {
let mut parts = value.split_whitespace();
let width = parts.next().unwrap_or("");
let kind = parts.next().unwrap_or("solid");
let colour = parts.next().and_then(colour_of).unwrap_or(Color::Auto);
if kind == "none" || width == "none" {
return Border::default();
}
let inches: f64 = width.trim_end_matches("in").parse().unwrap_or(0.0);
let style = match kind {
"double" => BorderStyle::Named("double"),
"dashed" if inches >= 0.04 => BorderStyle::Named("mediumDashed"),
"dashed" => BorderStyle::Named("dashed"),
"dotted" => BorderStyle::Named("dotted"),
_ if inches >= 0.09 => BorderStyle::Named("thick"),
_ if inches >= 0.04 => BorderStyle::Named("medium"),
_ => BorderStyle::Named("thin"),
};
Border {
style,
color: colour,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn content(rows: &str) -> String {
format!(
"<?xml version=\"1.0\"?>\
<office:document-content \
xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\" \
xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\" \
xmlns:xlink=\"http://www.w3.org/1999/xlink\">\
<office:body><office:spreadsheet>\
<table:table table:name=\"Лист\">{rows}</table:table>\
</office:spreadsheet></office:body></office:document-content>"
)
}
fn at(a: &str) -> CellRef {
CellRef::parse(a).expect("test reference is valid")
}
fn value(book: &Spreadsheet, a: &str) -> Option<CellValue> {
book.sheets()[0].get(at(a)).map(|c| c.value.clone())
}
#[test]
fn a_table_becomes_a_sheet_of_typed_values() {
let book = read_content(&content(
"<table:table-row>\
<table:table-cell office:value-type=\"string\"><text:p>привет</text:p></table:table-cell>\
<table:table-cell office:value-type=\"float\" office:value=\"3.5\"><text:p>3,50</text:p></table:table-cell>\
<table:table-cell office:value-type=\"boolean\" office:boolean-value=\"true\"><text:p>ИСТИНА</text:p></table:table-cell>\
</table:table-row>",
))
.expect("reads");
assert_eq!(book.sheets()[0].title(), "Лист");
assert_eq!(value(&book, "A1"), Some(CellValue::text("привет")));
assert_eq!(value(&book, "B1"), Some(CellValue::Number(3.5)));
assert_eq!(value(&book, "C1"), Some(CellValue::Bool(true)));
}
#[test]
fn a_repeat_count_is_a_run_of_copies_but_not_of_nothing() {
let book = read_content(&content(
"<table:table-row>\
<table:table-cell office:value-type=\"float\" office:value=\"1\" table:number-columns-repeated=\"3\"><text:p>1</text:p></table:table-cell>\
<table:table-cell table:number-columns-repeated=\"1013\"/>\
</table:table-row>\
<table:table-row table:number-rows-repeated=\"2\">\
<table:table-cell office:value-type=\"string\"><text:p>x</text:p></table:table-cell>\
</table:table-row>",
))
.expect("reads");
let sheet = &book.sheets()[0];
assert_eq!(value(&book, "C1"), Some(CellValue::Number(1.0)));
assert_eq!(value(&book, "D1"), None);
assert_eq!(value(&book, "A2"), Some(CellValue::text("x")));
assert_eq!(value(&book, "A3"), Some(CellValue::text("x")));
assert_eq!(sheet.len(), 5);
}
#[test]
fn a_formula_arrives_in_a1_notation_with_its_result() {
let book = read_content(&content(
"<table:table-row>\
<table:table-cell table:formula=\"of:=SUM([.A2:.A3])\" office:value-type=\"float\" office:value=\"7\"><text:p>7</text:p></table:table-cell>\
</table:table-row>",
))
.expect("reads");
assert_eq!(
value(&book, "A1"),
Some(CellValue::Formula {
formula: "SUM(A2:A3)".to_owned(),
cached: Some(Box::new(CellValue::Number(7.0))),
})
);
}
#[test]
fn a_span_becomes_a_merge_and_the_cells_under_it_stay_empty() {
let book = read_content(&content(
"<table:table-row>\
<table:table-cell table:number-columns-spanned=\"2\" table:number-rows-spanned=\"2\" office:value-type=\"string\"><text:p>wide</text:p></table:table-cell>\
<table:covered-table-cell/>\
</table:table-row>",
))
.expect("reads");
let sheet = &book.sheets()[0];
assert_eq!(sheet.merges, vec![Range::parse("A1:B2").expect("range")]);
assert_eq!(value(&book, "B1"), None);
}
#[test]
fn dates_and_times_arrive_as_serials_with_a_format() {
let book = read_content(&content(
"<table:table-row>\
<table:table-cell office:value-type=\"date\" office:date-value=\"2024-01-05\"><text:p>2024-01-05</text:p></table:table-cell>\
<table:table-cell office:value-type=\"date\" office:date-value=\"2024-01-05T12:00:00\"><text:p>2024-01-05 12:00</text:p></table:table-cell>\
<table:table-cell office:value-type=\"time\" office:time-value=\"PT01H30M00S\"><text:p>01:30</text:p></table:table-cell>\
<table:table-cell office:value-type=\"percentage\" office:value=\"0.125\"><text:p>12.50%</text:p></table:table-cell>\
</table:table-row>",
))
.expect("reads");
assert_eq!(value(&book, "A1"), Some(CellValue::Number(45_296.0)));
assert_eq!(value(&book, "B1"), Some(CellValue::Number(45_296.5)));
assert_eq!(value(&book, "C1"), Some(CellValue::Number(0.0625)));
assert_eq!(value(&book, "D1"), Some(CellValue::Number(0.125)));
let sheet = &book.sheets()[0];
let code = |a: &str| {
book.styles
.get(sheet.get(at(a)).expect("cell").style)
.expect("style")
.number_format
.code()
.to_owned()
};
assert_eq!(code("A1"), "yyyy-mm-dd");
assert_eq!(code("B1"), "yyyy-mm-dd hh:mm:ss");
assert_eq!(code("C1"), "hh:mm:ss");
assert_eq!(code("D1"), "0.00%");
}
#[test]
fn several_paragraphs_in_a_cell_are_several_lines() {
let book = read_content(&content(
"<table:table-row><table:table-cell office:value-type=\"string\">\
<text:p>one</text:p><text:p>two</text:p></table:table-cell></table:table-row>",
))
.expect("reads");
assert_eq!(value(&book, "A1"), Some(CellValue::text("one\ntwo")));
}
#[test]
fn a_link_is_kept_with_the_cell_it_covers() {
let book = read_content(&content(
"<table:table-row><table:table-cell office:value-type=\"string\">\
<text:p><text:a xlink:href=\"https://example.test/\">click</text:a></text:p>\
</table:table-cell></table:table-row>",
))
.expect("reads");
let sheet = &book.sheets()[0];
assert_eq!(value(&book, "A1"), Some(CellValue::text("click")));
assert_eq!(
sheet.hyperlinks[0].target,
LinkTarget::Outside("https://example.test/".to_owned())
);
}
#[test]
fn a_package_that_is_not_a_spreadsheet_is_refused() {
assert!(read_content("<office:body/>").is_err());
assert!(duration_serial("PT25H00M00S").is_some_and(|d| d > 1.0));
assert_eq!(iso_serial("nonsense"), None);
}
}