Skip to content

Merge adjacent cells, using the Ctrl+M shortcut or the context menu. Control merged cells, using Handsontable’s API.

Overview

By merging, you can combine two or more adjacent cells into a single cell that spans several rows or columns.

Handsontable merges cells in the same way as Microsoft Excel: keeps only the upper-left value of the selected range and clears other values.

How to merge cells

To enable the merge cells feature, set the mergeCells option to true or to an array.

To initialize Handsontable with predefined merged cells, provide merged cells details in form of an array:

mergeCells={[{ row: 1, col: 1, rowspan: 2, colspan: 2 }]}

JavaScript
import { HotTable } from '@handsontable/react-wrapper';
import { registerAllModules } from 'handsontable/registry';
// register Handsontable's modules
registerAllModules();
// generate an array of arrays with dummy data
const data = new Array(100) // number of rows
.fill(null)
.map((_, row) =>
new Array(50) // number of columns
.fill(null)
.map((_, column) => `${row}, ${column}`)
);
const ExampleComponent = () => {
return (
<HotTable
data={data}
height={320}
autoColumnSize={{
allowSampleDuplicates: true,
samplingRatio: 100,
}}
rowHeaders={true}
colHeaders={true}
contextMenu={true}
mergeCells={[
{ row: 1, col: 1, rowspan: 3, colspan: 3 },
{ row: 3, col: 4, rowspan: 2, colspan: 2 },
{ row: 5, col: 6, rowspan: 3, colspan: 3 },
]}
autoWrapRow={true}
autoWrapCol={true}
licenseKey="non-commercial-and-evaluation"
/>
);
};
export default ExampleComponent;
TypeScript
import { HotTable } from '@handsontable/react-wrapper';
import { registerAllModules } from 'handsontable/registry';
// register Handsontable's modules
registerAllModules();
// generate an array of arrays with dummy data
const data = new Array(100) // number of rows
.fill(null)
.map((_, row) =>
new Array(50) // number of columns
.fill(null)
.map((_, column) => `${row}, ${column}`)
);
const ExampleComponent = () => {
return (
<HotTable
data={data}
height={320}
autoColumnSize={{
allowSampleDuplicates: true,
samplingRatio: 100,
}}
rowHeaders={true}
colHeaders={true}
contextMenu={true}
mergeCells={[
{ row: 1, col: 1, rowspan: 3, colspan: 3 },
{ row: 3, col: 4, rowspan: 2, colspan: 2 },
{ row: 5, col: 6, rowspan: 3, colspan: 3 },
]}
autoWrapRow={true}
autoWrapCol={true}
licenseKey="non-commercial-and-evaluation"
/>
);
};
export default ExampleComponent;

Optimizing rendering of the wide/tall merged cells

When cells span thousands of rows or columns, scrolling may feel slower compared to unmerged cells. To improve performance, consider enabling the dedicated virtualization feature for merged cells, which is disabled by default.

To enable the merged cells virtualization mode, enable the virtualized option:

mergeCells={{
virtualized: true,
cells: [{ row: 1, col: 1, rowspan: 200, colspan: 2 }]
}}

The example below uses virtualized merged cells. It’s also recommended to increase the buffer of rendered rows/columns to minimize the flickering effects.

JavaScript
import { HotTable } from '@handsontable/react-wrapper';
import { registerAllModules } from 'handsontable/registry';
// register Handsontable's modules
registerAllModules();
// generate an array of arrays with dummy data
const data = new Array(50) // number of rows
.fill(null)
.map((_, row) =>
new Array(500) // number of columns
.fill(null)
.map((_, column) => `${row}, ${column}`)
);
const ExampleComponent = () => {
return (
<HotTable
data={data}
height={320}
colWidths={100}
rowHeaders={true}
colHeaders={true}
contextMenu={true}
mergeCells={{
virtualized: true,
cells: [{ row: 1, col: 1, rowspan: 3, colspan: 498 }],
}}
viewportColumnRenderingOffset={15}
viewportColumnRenderingThreshold={5}
autoWrapRow={true}
autoWrapCol={true}
licenseKey="non-commercial-and-evaluation"
/>
);
};
export default ExampleComponent;
TypeScript
import { HotTable } from '@handsontable/react-wrapper';
import { registerAllModules } from 'handsontable/registry';
// register Handsontable's modules
registerAllModules();
// generate an array of arrays with dummy data
const data = new Array(50) // number of rows
.fill(null)
.map((_, row) =>
new Array(500) // number of columns
.fill(null)
.map((_, column) => `${row}, ${column}`)
);
const ExampleComponent = () => {
return (
<HotTable
data={data}
height={320}
colWidths={100}
rowHeaders={true}
colHeaders={true}
contextMenu={true}
mergeCells={{
virtualized: true,
cells: [{ row: 1, col: 1, rowspan: 3, colspan: 498 }],
}}
viewportColumnRenderingOffset={15}
viewportColumnRenderingThreshold={5}
autoWrapRow={true}
autoWrapCol={true}
licenseKey="non-commercial-and-evaluation"
/>
);
};
export default ExampleComponent;

Behavior during row/column reorder and column freeze

When a merged cell’s underlying rows or columns are reordered (through manualColumnMove, manualRowMove, or manualColumnFreeze), Handsontable follows the merge to the new visual position. Two side effects can occur:

  • Auto-split: if the move bisects a merge so the underlying cells are no longer contiguous in the new visual order, the merge is split into separate merges, one per contiguous run. The cross-axis span (rowspan for column moves, colspan for row moves) is preserved on every fragment.
  • Silent drop of single-cell fragments: any resulting fragment that ends up as a single cell (rowspan === 1 && colspan === 1) is removed, because a single cell is no longer a merge. The afterMergeCells hook is not fired for the dropped fragment.

undo and redo restore the pre-move state, including any merges that were split or dropped by the reorder.

WindowsmacOSActionExcelSheets
Ctrl+M+MMerge or unmerge the selected cells

Configuration options

Hooks

Plugins

Result

Cells at the configured positions are now merged. Users see a single cell spanning multiple rows or columns.