diff --git a/crates/engine/src/cmd/table.rs b/crates/engine/src/cmd/table.rs
index f8a13723b..960dbae76 100644
--- a/crates/engine/src/cmd/table.rs
+++ b/crates/engine/src/cmd/table.rs
@@ -360,16 +360,22 @@ fn insert_col_like(t: &mut Table, at: usize, neighbour: usize, keep_width: bool)
let total = t.width();
let neighbour = neighbour.min(t.cols.len().saturating_sub(1));
let w = t.cols.get(neighbour).copied();
- // A neighbour covered by a horizontal merge is formatted as its anchor, which is what shows.
- let templates: Vec> = t
- .rows
- .iter()
- .map(|r| {
- let mut n = neighbour;
- while n > 0 && r.cells.get(n).is_some_and(|c| c.h_merge) {
- n -= 1;
+ // A neighbour covered by a merge (horizontally or vertically) is formatted as its anchor, which is what shows.
+ let covered = |r: usize, c: usize| t.rows.get(r).and_then(|row| row.cells.get(c)).is_some_and(|x| x.h_merge || x.v_merge);
+ let templates: Vec > = (0..t.rows.len())
+ .map(|r0| {
+ let (mut r, mut n) = (r0, neighbour);
+ while covered(r, n) {
+ let cell = &t.rows[r].cells[n];
+ if cell.h_merge && n > 0 {
+ n -= 1;
+ } else if cell.v_merge && r > 0 {
+ r -= 1;
+ } else {
+ break;
+ }
}
- r.cells.get(n).cloned()
+ t.rows[r].cells.get(n).cloned()
})
.collect();
t.insert_col(at);
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index c3e419e0a..6759b6f92 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -6571,6 +6571,19 @@ fn table_insert_column_beside_a_merge_copies_the_anchor_format() {
assert!(t.cell(0, 2).unwrap().fill.is_some());
}
+#[test]
+fn table_insert_column_beside_a_two_row_merge_copies_the_anchor_format() {
+ let mut s = session();
+ let id = table_at(&mut s, &[280.0, 280.0, 280.0], json!([["a", "b", "c"], ["d", "e", "f"]]));
+ s.execute("table.cellFill", &json!({"id": id.0, "color": "#FF0000", "cells": [[0, 0]]})).unwrap();
+ s.execute("table.merge", &json!({"id": id.0, "from": [0, 0], "to": [1, 1]})).unwrap();
+ s.execute("table.insertColumnRight", &json!({"id": id.0, "col": 1})).unwrap();
+ let t = table_of(&s, id);
+ assert!(t.cell(1, 1).unwrap().h_merge && t.cell(1, 1).unwrap().v_merge, "the merge stays put");
+ assert!(t.cell(0, 2).unwrap().fill.is_some());
+ assert_eq!(t.cell(1, 2).unwrap().fill, t.cell(0, 0).unwrap().fill);
+}
+
/// A table near the slide's right edge, with no room for another column: by default an inserted
/// column widens it past the edge, as PowerPoint's Insert Right does (the frame keeps its left and
/// grows), and `keepWidth: true` keeps it on the slide.