use super::ExcelConfig;
use super::drawing::{Drawn, PictureRef, Shape};
use super::grid::{CellKind, Grid, Merge, MergeIndex};
use super::regions::{self, Visible, is_blank};
use crate::chart;
use crate::common::geom::Rect;
use crate::model::{Block, BlockKind, FloatKind, ImageSpec, Span, Table, TableCell};
use crate::word::pictures::FALLBACK_PICTURE_PT;
use std::collections::{BTreeMap, HashMap, HashSet};
const CAPTION_SEPARATOR: &str = " — ";
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Parts {
pub(crate) lead: Vec<u32>,
pub(crate) caption: Vec<u32>,
pub(crate) header: Vec<u32>,
pub(crate) body: Vec<u32>,
pub(crate) notes: Vec<u32>,
pub(crate) paragraphs: bool,
}
pub(crate) fn sheet_blocks(name: &str, grid: &Grid, shapes: &[Shape], config: &ExcelConfig) -> Vec<Block> {
let mut blocks = Vec::new();
let merges = MergeIndex::new(&grid.merges);
let mut in_cells: Vec<(&(u32, u32), &PictureRef)> = shapes
.iter()
.filter_map(|shape| match &shape.drawn {
Drawn::CellPicture(picture) => Some((&shape.at, picture)),
_ => None,
})
.collect();
for region in regions::split(grid, config) {
let cells = RegionCells::new(grid, &merges, ®ion);
if cells.cells.is_empty() {
continue;
}
let parts = parts_of(grid, ®ion, &cells, config);
let inside = |(row, col): &(u32, u32)| {
(region.top..=region.bottom).contains(row) && (region.left..=region.right).contains(col)
};
let (mine, others): (Vec<_>, Vec<_>) = in_cells.into_iter().partition(|(at, _)| inside(at));
in_cells = others;
if parts.paragraphs {
blocks.extend(parts.body.iter().map(|&row| paragraph(cells.row_text(row))));
blocks.extend(mine.into_iter().filter_map(|(_, picture)| figure(picture)));
continue;
}
blocks.extend(parts.lead.iter().map(|&row| paragraph(cells.row_text(row))));
let mut float = Block::new(
0,
BlockKind::Figure {
kind: FloatKind::Table,
image: None,
label: None,
},
);
float.caption_above = true;
for &row in &parts.caption {
if !float.spans.is_empty() {
float.spans.push(Span::plain(CAPTION_SEPARATOR));
}
float
.spans
.extend(cells.row(row).flat_map(|(_, cell)| cell.spans.iter().cloned()));
}
float.table = Some(table(&cells, &parts, config));
blocks.push(float);
blocks.extend(parts.notes.iter().map(|&row| paragraph(cells.row_text(row))));
blocks.extend(mine.into_iter().filter_map(|(_, picture)| figure(picture)));
}
blocks.extend(in_cells.into_iter().filter_map(|(_, picture)| figure(picture)));
blocks.extend(truncation_notes(grid));
blocks.extend(drawn_blocks(shapes));
if blocks.is_empty() {
return blocks;
}
let mut heading = Block::new(0, BlockKind::Heading { level: 1 });
heading.spans = vec![Span::plain(name)];
blocks.insert(0, heading);
for (id, block) in blocks.iter_mut().enumerate() {
block.id = id;
}
blocks
}
fn truncation_notes(grid: &Grid) -> Vec<Block> {
let mut notes = Vec::new();
for (count, one, many) in [
(grid.truncated_rows, "row is", "rows are"),
(grid.truncated_cols, "column is", "columns are"),
] {
if count > 0 {
let text = if count == 1 {
format!("1 more {one} not shown.")
} else {
format!("{count} more {many} not shown.")
};
notes.push(paragraph(vec![Span::plain(text)]));
}
}
notes
}
fn drawn_blocks(shapes: &[Shape]) -> Vec<Block> {
let mut blocks = Vec::new();
let mut charts = 0;
for shape in shapes {
match &shape.drawn {
Drawn::Text(text) => blocks.extend(
text.text
.lines()
.filter(|line| !line.trim().is_empty())
.map(|line| paragraph(vec![Span::plain(line)])),
),
Drawn::Picture(picture) => blocks.extend(figure(picture)),
Drawn::CellPicture(_) => {}
Drawn::Chart(chart) => {
if let Some(placed) = &chart.placed {
charts += 1;
let (placed, index) = placed.as_ref();
blocks.extend(chart::blocks(placed.clone(), *index, &format!("Chart {charts}")));
}
}
}
}
blocks
}
fn figure(picture: &PictureRef) -> Option<Block> {
let index = picture.index?;
let (width, height) = picture.size_pt.unwrap_or(FALLBACK_PICTURE_PT);
let image = ImageSpec {
page: 0,
rect: Rect {
x0: 0.0,
y0: 0.0,
x1: width,
y1: height,
},
transparent: false,
embedded: Some(index),
};
let mut block = Block::new(
0,
BlockKind::Figure {
kind: FloatKind::Graphic,
image: Some(image),
label: None,
},
);
if !picture.alt.is_empty() {
block.rows = vec![vec![Span::plain(picture.alt.clone())]];
}
Some(block)
}
#[cfg(test)]
pub(crate) fn parts(grid: &Grid, region: &Merge, config: &ExcelConfig) -> Parts {
parts_of(
grid,
region,
&RegionCells::new(grid, &MergeIndex::new(&grid.merges), region),
config,
)
}
fn parts_of(grid: &Grid, region: &Merge, cells: &RegionCells<'_>, config: &ExcelConfig) -> Parts {
let rows = cells.rows.clone();
let text = |rows: Vec<u32>| Parts {
body: rows,
paragraphs: true,
..Parts::default()
};
if cells.is_paragraphs() {
return text(rows);
}
let single = |row: &u32| {
let mut row = cells.row(*row);
matches!((row.next(), row.next()), (Some((_, cell)), None) if cell.kind == CellKind::Text)
};
let leading = rows.iter().take_while(|row| single(row)).count();
let lead = leading - leading.min(config.max_caption_rows);
let notes = rows[leading..].iter().rev().take_while(|row| single(row)).count();
let table = &rows[leading..rows.len() - notes];
let header = header_rows(grid, region, cells, table, config);
let body = &table[header..];
if fill(grid, region, cells, body) < config.table_min_fill_ratio {
return text(rows);
}
Parts {
lead: rows[..lead].to_vec(),
caption: rows[lead..leading].to_vec(),
header: table[..header].to_vec(),
body: body.to_vec(),
notes: rows[rows.len() - notes..].to_vec(),
paragraphs: false,
}
}
fn fill(grid: &Grid, region: &Merge, cells: &RegionCells<'_>, body: &[u32]) -> f32 {
let (Some(&first), Some(&last)) = (body.first(), body.last()) else {
return 1.0;
};
let rows = Visible::new(&grid.hidden_rows);
let cols = Visible::new(&grid.hidden_cols);
let positions = rows.count(first, last) * cols.count(region.left, region.right);
if positions == 0 {
return 1.0;
}
let covered: u64 = cells
.cells
.iter()
.filter(|&(&(row, _), cell)| row <= last && cell.bottom >= first)
.map(|(&(row, col), cell)| rows.count(row.max(first), cell.bottom.min(last)) * cols.count(col, cell.right))
.sum();
covered as f32 / positions as f32
}
fn header_rows(grid: &Grid, region: &Merge, cells: &RegionCells<'_>, table: &[u32], config: &ExcelConfig) -> usize {
if table.len() <= 1 {
return 0;
}
let most = table.len() - 1;
if let Some(rows) = config.header_rows {
return rows.min(most);
}
let found = declared_header(grid, region, table)
.map(|rows| rows.min(most))
.or_else(|| header_by_types(cells, table, config))
.unwrap_or(1);
if found == 0 {
return 0;
}
let mut header = found;
loop {
let reach = table[..header]
.iter()
.flat_map(|&row| cells.row(row))
.map(|(_, cell)| table.partition_point(|&row| row <= cell.bottom))
.max()
.unwrap_or(header);
if reach <= header || reach >= table.len() {
return header;
}
header = reach;
}
}
fn declared_header(grid: &Grid, region: &Merge, table: &[u32]) -> Option<usize> {
let declared = &grid.declared;
let covers_rows = |first: u32, last: u32| first <= region.bottom && region.top <= last;
let mut last_rows: Vec<u32> = Vec::new();
for (area, header_rows) in &declared.tables {
if area.overlaps(region) {
if *header_rows == 0 {
return Some(0);
}
last_rows.push(area.top.saturating_add(header_rows - 1));
}
}
if let Some(area) = declared.auto_filter.filter(|area| area.overlaps(region)) {
last_rows.push(area.top);
}
if declared.frozen_rows > 0 && covers_rows(0, declared.frozen_rows - 1) {
last_rows.push(declared.frozen_rows - 1);
}
if let Some((_, last)) = declared
.print_title_rows
.filter(|&(first, last)| covers_rows(first, last))
{
last_rows.push(last);
}
let first = *table.first()?;
last_rows
.into_iter()
.find(|&last| last >= first)
.map(|last| table.partition_point(|&row| row <= last))
}
fn header_by_types(cells: &RegionCells<'_>, table: &[u32], config: &ExcelConfig) -> Option<usize> {
let text_only = table
.iter()
.flat_map(|&row| cells.row(row))
.all(|(_, cell)| cell.kind == CellKind::Text);
if text_only {
return None;
}
let lookahead = config.header_lookahead_rows.max(1);
let rows: Vec<Vec<(u32, CellKind)>> = table
.iter()
.take(lookahead.saturating_mul(2))
.map(|&row| cells.row(row).map(|(col, cell)| (col, cell.kind)).collect())
.collect();
(0..rows.len().saturating_sub(1).min(lookahead)).find(|&index| {
let below = &rows[index + 1..rows.len().min(index + 1 + lookahead)];
let mut judged = 0usize;
let mut matching = 0usize;
for &(col, kind) in &rows[index] {
if let Some(common) = most_common(
below
.iter()
.flatten()
.filter(|(other, _)| *other == col)
.map(|&(_, kind)| kind),
) {
judged += 1;
matching += usize::from(kind == common);
}
}
judged > 0 && matching as f32 >= config.header_type_match_ratio * judged as f32
})
}
fn most_common(kinds: impl Iterator<Item = CellKind>) -> Option<CellKind> {
let mut counts = [0usize; KINDS.len()];
for kind in kinds {
if let Some(index) = KINDS.iter().position(|&other| other == kind) {
counts[index] += 1;
}
}
let (index, &count) = counts.iter().enumerate().rev().max_by_key(|&(_, count)| count)?;
(count > 0).then_some(KINDS[index])
}
const KINDS: [CellKind; 5] = [
CellKind::Number,
CellKind::Date,
CellKind::Text,
CellKind::Bool,
CellKind::Error,
];
fn table(cells: &RegionCells<'_>, parts: &Parts, config: &ExcelConfig) -> Table {
let rows: Vec<u32> = parts.header.iter().chain(&parts.body).copied().collect();
let mut cols: Vec<u32> = rows
.iter()
.flat_map(|&row| cells.row(row).map(|(col, _)| col))
.collect::<HashSet<_>>()
.into_iter()
.collect();
cols.sort_unstable();
let n_cols = cols.len().max(1);
let count = |kept: &[u32], first: u32, last: u32| {
(kept.partition_point(|&index| index <= last) - kept.partition_point(|&index| index < first)).max(1)
};
let mut numeric = vec![(0usize, 0usize); n_cols];
let built: Vec<Vec<TableCell>> = rows
.iter()
.enumerate()
.map(|(index, &row)| {
cells
.row(row)
.map(|(col, cell)| {
let at = cols.partition_point(|&other| other < col);
if index >= parts.header.len() {
let (numbers, all) = &mut numeric[at];
*all += 1;
*numbers += usize::from(matches!(cell.kind, CellKind::Number | CellKind::Date));
}
TableCell {
col: at,
colspan: count(&cols, col, cell.right),
rowspan: count(&rows, row, cell.bottom),
spans: cell.spans.to_vec(),
}
})
.collect()
})
.collect();
let col_align = numeric
.iter()
.map(|&(numbers, all)| {
if all > 0 && numbers as f32 > config.numeric_column_ratio * all as f32 {
"right"
} else {
"left"
}
})
.collect();
Table::ruled(built, n_cols, parts.header.len(), col_align)
}
fn paragraph(spans: Vec<Span>) -> Block {
let mut block = Block::new(0, BlockKind::Paragraph { in_abstract: false });
block.spans = spans;
block
}
struct Cell<'g> {
bottom: u32,
right: u32,
spans: &'g [Span],
kind: CellKind,
}
struct RegionCells<'g> {
cells: BTreeMap<(u32, u32), Cell<'g>>,
rows: Vec<u32>,
}
impl<'g> RegionCells<'g> {
fn new(grid: &'g Grid, merges: &MergeIndex, region: &Merge) -> Self {
let visible_col = |col: u32| !grid.hidden_cols.contains(&col);
let mut merged: HashMap<(u32, u32), Merge> = HashMap::new();
let mut covered: HashSet<(u32, u32)> = HashSet::new();
for merge in merges.overlapping(region) {
let clipped = Merge {
top: merge.top.max(region.top),
left: merge.left.max(region.left),
bottom: merge.bottom.min(region.bottom),
right: merge.right.min(region.right),
};
merged.insert((merge.top, merge.left), clipped);
for row in clipped.top..=clipped.bottom {
covered.extend(
grid.cells
.range((row, clipped.left)..=(row, clipped.right))
.map(|(&place, _)| place)
.filter(|&place| place != (merge.top, merge.left)),
);
}
}
let next_col = |col: u32| (col + 1..=region.right).find(|&col| visible_col(col));
let mut cells: BTreeMap<(u32, u32), Cell<'g>> = BTreeMap::new();
for row in (region.top..=region.bottom).filter(|row| !grid.hidden_rows.contains(row)) {
let mut centred: Option<(u32, u32)> = None;
for (&(_, col), cell) in grid.cells.range((row, region.left)..=(row, region.right)) {
if !visible_col(col) || covered.contains(&(row, col)) {
continue;
}
if is_blank(&cell.spans) {
match centred
.filter(|_| cell.center_continuous)
.and_then(|place| cells.get_mut(&place))
{
Some(open) if next_col(open.right) == Some(col) => open.right = col,
_ => centred = None,
}
continue;
}
let merge = merged.get(&(row, col));
cells.insert(
(row, col),
Cell {
bottom: merge.map_or(row, |merge| merge.bottom),
right: merge.map_or(col, |merge| merge.right),
spans: &cell.spans,
kind: cell.kind,
},
);
centred = (cell.center_continuous && merge.is_none()).then_some((row, col));
}
}
let mut rows: Vec<u32> = cells.keys().map(|&(row, _)| row).collect();
rows.dedup();
Self { cells, rows }
}
fn row(&self, row: u32) -> impl Iterator<Item = (u32, &Cell<'g>)> {
self.cells
.range((row, 0)..=(row, u32::MAX))
.map(|(&(_, col), cell)| (col, cell))
}
fn row_text(&self, row: u32) -> Vec<Span> {
let mut spans = Vec::new();
for (_, cell) in self.row(row) {
if !spans.is_empty() {
spans.push(Span::plain(" "));
}
spans.extend(cell.spans.iter().cloned());
}
spans
}
fn is_paragraphs(&self) -> bool {
self.rows.iter().all(|&row| self.row(row).nth(1).is_none())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn text_shape(at: (u32, u32), text: &str) -> Shape {
Shape {
at,
frame: crate::excel::drawing::Frame::default(),
drawn: Drawn::Text(crate::excel::drawing::TextShape {
text: text.to_string(),
..Default::default()
}),
}
}
use crate::excel::grid::GridCell;
use crate::model::{HEAVY_RULE, THIN_RULE};
fn grid(rows: &[&[&str]]) -> Grid {
let mut grid = Grid::default();
for (row, cells) in rows.iter().enumerate() {
for (col, text) in cells.iter().enumerate() {
if text.is_empty() {
continue;
}
let kind = if text.parse::<f64>().is_ok() {
CellKind::Number
} else {
CellKind::Text
};
let place = (u32::try_from(row).unwrap(), u32::try_from(col).unwrap());
grid.cells.insert(
place,
GridCell {
spans: vec![Span::plain(*text)],
kind,
center_continuous: false,
own_font: Vec::new(),
overflow_pt: 0.0,
},
);
}
}
grid
}
fn area(top: u32, left: u32, bottom: u32, right: u32) -> Merge {
Merge {
top,
left,
bottom,
right,
}
}
fn only_parts(grid: &Grid, config: &ExcelConfig) -> Parts {
let regions = regions::split(grid, config);
assert_eq!(regions.len(), 1, "{regions:?}");
parts(grid, ®ions[0], config)
}
fn only_table(blocks: &[Block]) -> &Table {
let tables: Vec<&Table> = blocks.iter().filter_map(|block| block.table.as_ref()).collect();
assert_eq!(tables.len(), 1);
tables[0]
}
fn texts(table: &Table) -> Vec<Vec<String>> {
table
.rows
.iter()
.map(|row| {
row.iter()
.map(|cell| cell.spans.iter().map(|span| span.text.as_str()).collect())
.collect()
})
.collect()
}
const TWO_LEVEL: &[&[&str]] = &[
&["Area", "Population", "Men", "Women"],
&["Name", "Total", "of which", "of which"],
&["", "persons", "persons", "persons"],
&["North", "10", "4", "6"],
&["South", "12", "5", "7"],
&["East", "8", "3", "5"],
&["West", "9", "4", "5"],
];
#[test]
fn title_and_unit_rows_become_the_caption() {
let grid = grid(&[
&["Table 1: Households"],
&["(in thousands)"],
&["Region", "Count"],
&["North", "5"],
&["South", "7"],
]);
let config = ExcelConfig::default();
let parts = only_parts(&grid, &config);
assert_eq!(
parts,
Parts {
lead: vec![],
caption: vec![0, 1],
header: vec![2],
body: vec![3, 4],
notes: vec![],
paragraphs: false,
}
);
let blocks = sheet_blocks("Data", &grid, &[], &config);
assert_eq!(blocks.len(), 2);
assert_eq!(blocks[0].text(), "Data");
assert!(matches!(blocks[0].kind, BlockKind::Heading { level: 1 }));
assert!(blocks[1].caption_above);
assert_eq!(blocks[1].text(), "Table 1: Households — (in thousands)");
assert_eq!(only_table(&blocks).rows.len(), 3);
let ids: Vec<usize> = blocks.iter().map(|block| block.id).collect();
assert_eq!(ids, [0, 1]);
}
#[test]
fn source_line_becomes_a_note() {
let grid = grid(&[
&["Region", "Count"],
&["North", "5"],
&["South", "7"],
&[],
&["Source: census"],
]);
let config = ExcelConfig::default();
let parts = only_parts(&grid, &config);
assert_eq!(parts.notes, [4]);
assert_eq!(parts.body, [1, 2]);
let blocks = sheet_blocks("Data", &grid, &[], &config);
assert_eq!(blocks.len(), 3);
assert!(blocks[1].spans.is_empty());
assert!(matches!(blocks[2].kind, BlockKind::Paragraph { .. }));
assert_eq!(blocks[2].text(), "Source: census");
}
#[test]
fn two_level_header_found_by_types() {
let parts = only_parts(&grid(TWO_LEVEL), &ExcelConfig::default());
assert_eq!(parts.header, [0, 1, 2]);
assert_eq!(parts.body, [3, 4, 5, 6]);
}
#[test]
fn declaration_wins_over_types() {
let mut grid = grid(&[&["Region", "2021", "2022"], &["North", "1", "2"], &["South", "3", "4"]]);
let config = ExcelConfig::default();
assert!(only_parts(&grid, &config).header.is_empty());
grid.declared.frozen_rows = 1;
assert_eq!(only_parts(&grid, &config).header, [0]);
grid.declared.frozen_rows = 0;
grid.declared.print_title_rows = Some((0, 1));
assert_eq!(only_parts(&grid, &config).header, [0, 1]);
grid.declared.print_title_rows = None;
grid.declared.auto_filter = Some(area(0, 0, 2, 2));
assert_eq!(only_parts(&grid, &config).header, [0]);
grid.declared.auto_filter = None;
grid.declared.tables.push((area(0, 0, 2, 2), 2));
assert_eq!(only_parts(&grid, &config).header, [0, 1]);
}
#[test]
fn declaration_elsewhere_says_nothing() {
let mut rows: Vec<&[&str]> = vec![&["Title"]];
rows.extend_from_slice(TWO_LEVEL);
let mut grid = grid(&rows);
grid.declared.frozen_rows = 1;
let parts = only_parts(&grid, &ExcelConfig::default());
assert_eq!(parts.caption, [0]);
assert_eq!(parts.header, [1, 2, 3]);
grid.declared.frozen_rows = 0;
grid.declared.auto_filter = Some(area(20, 0, 30, 3));
assert_eq!(only_parts(&grid, &ExcelConfig::default()).header, [1, 2, 3]);
}
#[test]
fn all_text_table_without_declaration_takes_the_first_row() {
let grid = grid(&[&["Name", "City"], &["Ann", "Oslo"], &["Bob", "Rome"]]);
assert_eq!(only_parts(&grid, &ExcelConfig::default()).header, [0]);
}
#[test]
fn user_header_rows_wins() {
let config = ExcelConfig {
header_rows: Some(2),
..ExcelConfig::default()
};
let mut grid = grid(TWO_LEVEL);
grid.declared.frozen_rows = 1;
assert_eq!(only_parts(&grid, &config).header, [0, 1]);
}
#[test]
fn one_row_region_has_no_header() {
let grid = grid(&[&["Name", "City"]]);
let parts = only_parts(&grid, &ExcelConfig::default());
assert!(parts.header.is_empty());
assert_eq!(parts.body, [0]);
}
#[test]
fn one_column_region_is_paragraphs() {
let grid = grid(&[&["Application form"], &["Name"], &["", ""], &["2024"]]);
let blocks = sheet_blocks("Form", &grid, &[], &ExcelConfig::default());
let texts: Vec<String> = blocks.iter().map(Block::text).collect();
assert_eq!(texts, ["Form", "Application form", "Name", "2024"]);
assert!(
blocks[1..]
.iter()
.all(|block| matches!(block.kind, BlockKind::Paragraph { .. }))
);
}
#[test]
fn merged_cells_get_spans() {
let mut grid = grid(&[
&["Region", "Sales", ""],
&["", "2021", "2022"],
&["North", "1", "2"],
&["South", "3", "4"],
]);
grid.merges = vec![area(0, 0, 1, 0), area(0, 1, 0, 2)];
let blocks = sheet_blocks("Data", &grid, &[], &ExcelConfig::default());
let table = only_table(&blocks);
assert_eq!(table.header_rows, 2);
assert_eq!(table.n_cols, 3);
let first: Vec<_> = table.rows[0]
.iter()
.map(|cell| (cell.col, cell.colspan, cell.rowspan))
.collect();
assert_eq!(first, [(0, 1, 2), (1, 2, 1)]);
let second: Vec<_> = table.rows[1].iter().map(|cell| cell.col).collect();
assert_eq!(second, [1, 2]);
assert_eq!(table.rule_above[0], [HEAVY_RULE; 3]);
assert_eq!(table.rule_above[2], [THIN_RULE; 3]);
assert_eq!(table.rule_bottom, [HEAVY_RULE; 3]);
}
#[test]
fn a_merge_down_the_whole_table_keeps_a_body() {
let mut grid = grid(&[&["Group", "Name", "Count"], &["", "North", "5"], &["", "South", "7"]]);
grid.merges = vec![area(0, 0, 2, 0)];
let blocks = sheet_blocks("Data", &grid, &[], &ExcelConfig::default());
assert_eq!(only_table(&blocks).header_rows, 1);
}
#[test]
fn declared_header_keeps_a_body() {
let mut grid = grid(&[&["Name", "City"], &["Ann", "Oslo"]]);
grid.declared.frozen_rows = 5;
let parts = only_parts(&grid, &ExcelConfig::default());
assert_eq!((parts.header, parts.body), (vec![0], vec![1]));
}
#[test]
fn user_header_rows_keep_a_body() {
let config = ExcelConfig {
header_rows: Some(5),
..ExcelConfig::default()
};
let grid = grid(&[&["Name", "City"], &["Ann", "Oslo"]]);
let parts = only_parts(&grid, &config);
assert_eq!((parts.header, parts.body), (vec![0], vec![1]));
}
#[test]
fn damaged_header_row_count_does_not_overflow() {
let mut grid = grid(&[&[], &[], &["Name", "City"], &["Ann", "Oslo"], &["Bob", "Rome"]]);
grid.declared.tables.push((area(2, 0, 4, 1), u32::MAX));
let parts = only_parts(&grid, &ExcelConfig::default());
assert_eq!(parts.body, [4]);
}
#[test]
fn only_the_last_single_rows_are_the_caption() {
let grid = grid(&[
&["Read this first."],
&["Then this."],
&["Table 1"],
&["(units)"],
&["Region", "Count"],
&["North", "5"],
&["South", "7"],
]);
let config = ExcelConfig {
max_caption_rows: 2,
..ExcelConfig::default()
};
let parts = only_parts(&grid, &config);
assert_eq!(parts.lead, [0, 1]);
assert_eq!(parts.caption, [2, 3]);
let blocks = sheet_blocks("Data", &grid, &[], &config);
let texts: Vec<String> = blocks.iter().map(Block::text).collect();
assert_eq!(
texts[..4],
["Data", "Read this first.", "Then this.", "Table 1 — (units)"]
);
assert!(matches!(blocks[1].kind, BlockKind::Paragraph { .. }));
assert!(blocks[3].table.is_some());
}
#[test]
fn sparse_region_is_paragraphs() {
let grid = grid(&[
&["Name", "Ann", "", "", ""],
&["", "", "Address", "Oslo", ""],
&["Date", "", "", "", "2024"],
]);
let config = ExcelConfig {
table_min_fill_ratio: 0.5,
..ExcelConfig::default()
};
let blocks = sheet_blocks("Form", &grid, &[], &config);
let texts: Vec<String> = blocks.iter().map(Block::text).collect();
assert_eq!(texts, ["Form", "Name Ann", "Address Oslo", "Date 2024"]);
assert!(
blocks[1..]
.iter()
.all(|block| matches!(block.kind, BlockKind::Paragraph { .. }))
);
let full = super::tests::grid(&[&["Name", "City"], &["Ann", "Oslo"], &["Bob", "Rome"]]);
assert_eq!(only_table(&sheet_blocks("Data", &full, &[], &config)).rows.len(), 3);
}
#[test]
fn a_merge_fills_its_area() {
let mut grid = grid(&[
&["Group", "Name", "Count"],
&["A", "North", "5"],
&["", "South", "7"],
&["", "East", "8"],
]);
grid.merges = vec![area(1, 0, 3, 0)];
let config = ExcelConfig {
table_min_fill_ratio: 0.9,
..ExcelConfig::default()
};
assert!(sheet_blocks("Data", &grid, &[], &config)[1].table.is_some());
}
#[test]
fn centred_text_spans_the_empty_cells_after_it() {
let mut grid = grid(&[
&["Population", "", "", "Area"],
&["Total", "Men", "Women", "km2"],
&["10", "4", "6", "3"],
&["12", "5", "7", "4"],
]);
let centred = GridCell {
spans: Vec::new(),
kind: CellKind::Text,
center_continuous: true,
own_font: Vec::new(),
overflow_pt: 0.0,
};
grid.cells.get_mut(&(0, 0)).unwrap().center_continuous = true;
grid.cells.insert((0, 1), centred.clone());
grid.cells.insert((0, 2), centred);
let blocks = sheet_blocks("Data", &grid, &[], &ExcelConfig::default());
let table = only_table(&blocks);
let first: Vec<_> = table.rows[0].iter().map(|cell| (cell.col, cell.colspan)).collect();
assert_eq!(first, [(0, 3), (3, 1)]);
assert_eq!(table.header_rows, 2);
}
#[test]
fn numeric_columns_are_right_aligned() {
let grid = grid(&[
&["Region", "Count", "Note"],
&["North", "5", "a"],
&["South", "7", "8"],
&["East", "x", "b"],
]);
let blocks = sheet_blocks("Data", &grid, &[], &ExcelConfig::default());
let table = only_table(&blocks);
assert_eq!(table.col_align, ["left", "right", "left"]);
assert_eq!(texts(table)[1], ["North", "5", "a"]);
}
#[test]
fn hidden_rows_and_columns_are_left_out() {
let mut grid = grid(&[
&["Region", "Secret", "Count"],
&["North", "x", "5"],
&["Old", "y", "1"],
&["South", "z", "7"],
]);
grid.hidden_cols.insert(1);
grid.hidden_rows.insert(2);
let blocks = sheet_blocks("Data", &grid, &[], &ExcelConfig::default());
assert_eq!(
texts(only_table(&blocks)),
[["Region", "Count"], ["North", "5"], ["South", "7"]]
);
}
#[test]
fn empty_sheet_has_no_blocks_and_a_truncated_one_says_so() {
assert!(sheet_blocks("Empty", &Grid::default(), &[], &ExcelConfig::default()).is_empty());
let mut grid = grid(&[&["Region", "Count"], &["North", "5"]]);
grid.truncated_rows = 40;
let blocks = sheet_blocks("Data", &grid, &[], &ExcelConfig::default());
assert_eq!(blocks.last().unwrap().text(), "40 more rows are not shown.");
}
#[test]
fn columns_past_the_limit_say_so() {
let mut grid = grid(&[&["Region", "Count"], &["North", "5"]]);
grid.truncated_cols = 1;
let blocks = sheet_blocks("Data", &grid, &[], &ExcelConfig::default());
assert_eq!(blocks.last().unwrap().text(), "1 more column is not shown.");
grid.truncated_rows = 2;
grid.truncated_cols = 30;
let blocks = sheet_blocks("Data", &grid, &[], &ExcelConfig::default());
let texts: Vec<_> = blocks.iter().rev().take(2).map(Block::text).collect();
assert_eq!(texts, ["30 more columns are not shown.", "2 more rows are not shown."]);
}
#[test]
fn many_regions_with_merges_are_fast() {
let mut grid = Grid::default();
for band in 0..500u32 {
for slot in 0..400u32 {
let (row, col) = (band * 3, slot * 3);
grid.cells.insert(
(row, col),
GridCell {
spans: vec![Span::plain("x")],
kind: CellKind::Text,
center_continuous: false,
own_font: Vec::new(),
overflow_pt: 0.0,
},
);
grid.merges.push(area(row, col, row, col + 1));
}
}
let blocks = sheet_blocks("Data", &grid, &[], &ExcelConfig::default());
assert_eq!(blocks.len(), 200_001);
assert_eq!(regions::split(&grid, &ExcelConfig::default())[1], area(0, 3, 0, 4));
}
#[test]
fn side_by_side_tables_are_two_tables() {
let grid = grid(&[
&["A", "B", "", "C", "D"],
&["1", "2", "", "3", "4"],
&["5", "6", "", "7", "8"],
]);
let blocks = sheet_blocks("Data", &grid, &[], &ExcelConfig::default());
let tables: Vec<_> = blocks
.iter()
.filter_map(|block| block.table.as_ref())
.map(texts)
.collect();
assert_eq!(tables.len(), 2);
assert_eq!(tables[1][0], ["C", "D"]);
}
#[test]
fn shape_text_follows_the_regions_one_paragraph_per_line() {
let grid = grid(&[&["A", "B"], &["1", "2"]]);
let shapes = [text_shape((0, 0), "Emergency\n03-1234"), text_shape((9, 9), "Last")];
let blocks = sheet_blocks("Data", &grid, &shapes, &ExcelConfig::default());
let texts: Vec<_> = blocks.iter().map(Block::text).collect();
assert_eq!(texts[2..], ["Emergency", "03-1234", "Last"]);
assert!(matches!(blocks[2].kind, BlockKind::Paragraph { .. }));
assert_eq!(blocks[4].id, 4);
}
#[test]
fn a_sheet_with_only_shapes_has_a_heading() {
let shapes = [text_shape((0, 0), "Org chart")];
let blocks = sheet_blocks("Chart", &Grid::default(), &shapes, &ExcelConfig::default());
let texts: Vec<_> = blocks.iter().map(Block::text).collect();
assert_eq!(texts, ["Chart", "Org chart"]);
}
}