Merged Cells

Jspreadsheet supports creating and managing merged cells in your spreadsheets.

What's new with Version 13

  • Clipboard: pasted content replaces the destination merges and only whole merges travel, so every pasted value lands. Cutting a merged area over its own source keeps the content, with one undo for both ends
  • Navigation: Ctrl + Arrow treats a merge as one cell, and entering a merge that starts off-screen scrolls to its start
  • Clean documents: merging never writes empty style entries or materializes inherited styles
  • Merge function: setMerge(fn) defines the merges by a rule asked only for the rendered rows, for millions of rows. Not available on collaborative spreadsheets and not kept in the history

Documentation

Methods

The following methods provide programmatic control over merged cells:

Method Description
setMerge Merge cells from the given anchor by the number of columns and rows. With a cell name, colspan and rowspan are both required; with an object, each key is an anchor and the value is [colspan, rowspan], for example { A1: [3, 2], D4: [2, 4] }. Merges inside the new area are removed and replaced. Returns false when the worksheet is not editable. With a function, the merges are defined by a rule; see Merge function.
setMerge(cells: string|object, colspan?: number, rowspan?: number) => void | false
setMerge(fn: function|null) => boolean
getMerge Get one merged cell or all merged cells. With the name of an anchor cell, returns [colspan, rowspan]; for a cell that is not an anchor, returns undefined. Without arguments, returns an object keyed by anchor cell name.
getMerge(cells?: string) => [number, number] | object | undefined
removeMerge Remove merged cells by cell name, or by an object whose keys are cell names. Any cell inside a merge resolves to its anchor. Returns false when the worksheet is not editable.
removeMerge(cells: string|object) => void | false
destroyMerge Remove all merged cells.
destroyMerge() => void

Events

The following events are triggered during merged cell operations:

Event Description
onmerge Triggered when merges are created or removed. On setMerge, newValue maps each new anchor to [colspan, rowspan] and oldValue maps the anchors of the merges replaced by the new area, empty when none. On removeMerge and destroyMerge, newValue maps each removed anchor to true and oldValue to its previous [colspan, rowspan].
onmerge(worksheet: object, newValue: object, oldValue: object) => void

Initial Settings

Configure default merged cells during spreadsheet initialization:

Property Description
mergeCells?: object|function Initial merged cells, keyed by anchor cell name with [colspan, rowspan] values. Example: { A1: [3, 2] }. A function defines the merges by a rule; see Merge function

Merges and the clipboard

Pasted content replaces everything in the destination area, merges included: a merge touching the target range is removed and only the merges the pasted content carries are re-created, so every pasted value lands. A merge travels only when the copied area holds it whole. Cutting a merged area and pasting it over a position that overlaps its own source keeps the merged content, and one undo restores values, styles and merges at both ends. See the clipboard section.

Merges and navigation

For Ctrl + Arrow jumps, a merged cell acts as one cell: an empty merge is a blank the jump passes through, and a merge with a value stops the jump on its anchor. Entering a merge whose span starts off-screen scrolls the viewport to the start of the merge. See the navigation section.

Merge function

For very large worksheets, a merge configuration with one entry per merge can hold millions of entries. Instead, setMerge accepts a function (x, y) => [colspan, rowspan] | null, called with the worksheet as this. The function only says where a merge starts and its size; everything else is handled by the spreadsheet, exactly as with setMerge('A1', colspan, rowspan): a merge already in the way is replaced, and selection, navigation and the clipboard treat the result as any other merge.

  • The function is asked once per row, when the row is about to be rendered. Rows never rendered cost nothing, and getMerge() only returns the merges given so far
  • A merge starting above the visible area still covers it: its anchor row is rendered with the rows it spans
  • The covered cells keep their values: rendering never changes the data
  • The function is positional. A row is asked once, so after rows are inserted or deleted, the merges already given follow their rows, while new rows are asked by their new position
  • A new function replaces the merges given by the previous one, and setMerge(null) removes them
  • The same rule can be given at creation with mergeCells: function(x, y) { ... }

Collaboration and history

The merge function is a local configuration, like a column renderer. It is not available on collaborative spreadsheets: when the spreadsheet is opened with a guid, setMerge(fn) is refused and returns false, since a function cannot be sent to the other users. It also does not create a history entry: undo does not restore a previous function nor the merges it gave. Use setMerge with cell names or an object when the merges must be shared or undone.

// Every third row: column A spans two rows, column C spans two columns and two rows
worksheet.setMerge(function(x, y) {
    if (y % 3 === 0 && y + 1 < this.rows.length) {
        if (x === 0) {
            return [1, 2];
        }
        if (x === 2) {
            return [2, 2];
        }
    }
    return null;
});

// Remove the merges given by the function
worksheet.setMerge(null);

Known Limitations

Merged cells spanning hidden or frozen rows and columns may produce unexpected results. These limitations will be addressed in future releases.

Examples

This example demonstrates initializing merged cells and programmatically managing merge operations.

Open this merged cells example on JSFiddle.

<html>
<script src="https://jspreadsheet.com/v13/jspreadsheet.js"></script>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jspreadsheet.com/v13/jspreadsheet.css" type="text/css" />
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons" />

<div id="spreadsheet"></div>

<p><div id="log"></div></p>

<input type="button" value="setMerge('A3', 2, 3)" id="btn1" />
<input type="button" value="removeMerge('A3')" id="btn2" />
<input type="button" value="Get all merged cells" id="btn3" />
<input type="button" value="Destroy all merged" id="btn4" />

<script>
// You can use the following license for quick testing on localhost, StackBlitz, or CodeSandbox.
// The license is valid for one day, after which the spreadsheet will become read-only.
// For a longer trial period, you can create a free account and generate a demo license with an extended expiration date.
jspreadsheet.setLicense('MjJjZDJmZmUxY2FhYzMxZTQ5NTFlNDJlNDAxMTE4YWM4NTJjOTZmZTljYzZkMThiOWZiOGQyMThiNDUwNDhlZGEzM2E5NzNhOTA4NDBjYjA5YjE2ZjAyZmE3YmMyMzM4NjQ3MTQxYjI3MDBhOTNkNmQyNzdjZjlmMGNlYzNiYjUsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnprMU5USXhPRFF4TENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2liRzlqWVd4b2IzTjBJbDBzSW5Cc1lXNGlPaUl6TkNJc0luTmpiM0JsSWpwYkluWTNJaXdpZGpnaUxDSjJPU0lzSW5ZeE1DSXNJbll4TVNJc0luWXhNaUlzSW5ZeE15SXNJbU5vWVhKMGN5SXNJbVp2Y20xeklpd2labTl5YlhWc1lTSXNJbkJoY25ObGNpSXNJbkpsYm1SbGNpSXNJbU52YlcxbGJuUnpJaXdpYVcxd2IzSjBaWElpTENKaVlYSWlMQ0oyWVd4cFpHRjBhVzl1Y3lJc0luTmxZWEpqYUNJc0luQnlhVzUwSWl3aWMyaGxaWFJ6SWl3aVkyeHBaVzUwSWl3aWMyVnlkbVZ5SWl3aWMyaGhjR1Z6SWl3aVptOXliV0YwSWl3aWNHbDJiM1FpWFN3aVpHVnRieUk2ZEhKMVpYMD0=');

// Create the spreadsheet
let table = jspreadsheet(document.getElementById('spreadsheet'), {
    toolbar: true,
    worksheets: [{
        data: [
            ['Mazda', 2001, 2000, '2006-01-01 12:00:00'],
            ['Peugeot', 2010, 5000, '2005-01-01 13:00:00'],
            ['Honda Fit', 2009, 3000, '2004-01-01 14:01:00'],
            ['Honda CRV', 2010, 6000, '2003-01-01 23:30:00'],
        ],
        columnDrag: true,
        worksheetName: 'Merged Cells',
        minDimensions: [50, 5000],
        tableOverflow: true,
        tableWidth: '800px',
        tableHeight: '300px',
        columns: [
            {
                type: 'text',
                width: '300px',
                title: 'Model',
            },
            {
                type: 'text',
                width: '80px',
                title: 'Year',
            },
            {
                type: 'text',
                width: '100px',
                title: 'Price',
            },
            {
                type: 'calendar',
                width: '150px',
                title: 'Date',
                options: {
                    format: 'DD/MM/YYYY HH24:MI',
                    time: 1,
                }
            },
        ],
        mergeCells: {
            A1: [2, 2]
        }
    }]
});

document.getElementById("btn1").onclick = () => table[0].setMerge('A3', 2, 3);
document.getElementById("btn2").onclick = () => table[0].removeMerge('A3');
document.getElementById("btn3").onclick = () => {
    document.getElementById("log").innerHTML = JSON.stringify(table[0].getMerge());
}
document.getElementById("btn4").onclick = () => table[0].destroyMerge();
</script>
</html>
import React, { useRef } from 'react';
import { Spreadsheet, Worksheet, jspreadsheet } from '@jspreadsheet/react';
import 'jsuites/dist/jsuites.css';
import 'jspreadsheet/dist/jspreadsheet.css';

// You can use the following license for quick testing on localhost, StackBlitz, or CodeSandbox.
// The license is valid for one day, after which the spreadsheet will become read-only.
// For a longer trial period, you can create a free account and generate a demo license with an extended expiration date.
jspreadsheet.setLicense('MjJjZDJmZmUxY2FhYzMxZTQ5NTFlNDJlNDAxMTE4YWM4NTJjOTZmZTljYzZkMThiOWZiOGQyMThiNDUwNDhlZGEzM2E5NzNhOTA4NDBjYjA5YjE2ZjAyZmE3YmMyMzM4NjQ3MTQxYjI3MDBhOTNkNmQyNzdjZjlmMGNlYzNiYjUsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnprMU5USXhPRFF4TENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2liRzlqWVd4b2IzTjBJbDBzSW5Cc1lXNGlPaUl6TkNJc0luTmpiM0JsSWpwYkluWTNJaXdpZGpnaUxDSjJPU0lzSW5ZeE1DSXNJbll4TVNJc0luWXhNaUlzSW5ZeE15SXNJbU5vWVhKMGN5SXNJbVp2Y20xeklpd2labTl5YlhWc1lTSXNJbkJoY25ObGNpSXNJbkpsYm1SbGNpSXNJbU52YlcxbGJuUnpJaXdpYVcxd2IzSjBaWElpTENKaVlYSWlMQ0oyWVd4cFpHRjBhVzl1Y3lJc0luTmxZWEpqYUNJc0luQnlhVzUwSWl3aWMyaGxaWFJ6SWl3aVkyeHBaVzUwSWl3aWMyVnlkbVZ5SWl3aWMyaGhjR1Z6SWl3aVptOXliV0YwSWl3aWNHbDJiM1FpWFN3aVpHVnRieUk2ZEhKMVpYMD0=');

export default function App() {
    const spreadsheet = useRef();
    const log = useRef();

    const worksheets = [
        {
            data: [
                ['Mazda', 2001, 2000, '2006-01-01 12:00:00'],
                ['Peugeot', 2010, 5000, '2005-01-01 13:00:00'],
                ['Honda Fit', 2009, 3000, '2004-01-01 14:01:00'],
                ['Honda CRV', 2010, 6000, '2003-01-01 23:30:00'],
            ],
        columnDrag: true,
        worksheetName: 'Merged Cells',
        minDimensions: [50, 5000],
        tableOverflow: true,
        tableWidth: '800px',
        tableHeight: '300px',
        columns: [
          {
            type: 'text',
            width: '300px',
            title: 'Model',
          },
          {
            type: 'text',
            width: '80px',
            title: 'Year',
          },
          {
            type: 'text',
            width: '100px',
            title: 'Price',
          },
          {
            type: 'calendar',
            width: '150px',
            title: 'Date',
            options: {
              format: 'DD/MM/YYYY HH24:MI',
              time: 1,
            },
          },
        ],
        mergeCells: {
          A1: [2, 2],
        },
      },
    ];
  
    // Extracted functions
    const handleSetMerge = () => {
      spreadsheet.current[0].setMerge('A3', 2, 3);
    };
  
    const handleRemoveMerge = () => {
      spreadsheet.current[0].removeMerge('A3');
    };
  
    const handleGetAllMergedCells = () => {
      log.current.value = JSON.stringify(spreadsheet.current[0].getMerge());
    };
  
    const handleDestroyAllMerged = () => {
      spreadsheet.current[0].destroyMerge();
    };
  
    return (
      <>
        <Spreadsheet
          ref={spreadsheet}
          worksheets={worksheets}
        />
        <div ref={log}></div>
        <input
          type="button"
          value="setMerge('A3', 2, 3)"
          onClick={handleSetMerge}
        />
        <input
          type="button"
          value="removeMerge('A3')"
          onClick={handleRemoveMerge}
        />
        <input
          type="button"
          value="Get all merged cells"
          onClick={handleGetAllMergedCells}
        />
        <input
          type="button"
          value="Destroy all merged"
          onClick={handleDestroyAllMerged}
        />
      </>
    );
}
<template>
  <Spreadsheet ref="spreadsheet" :worksheets="worksheets" />
  <div ref="log"></div>
  <input type="button" value="setMerge('A3', 2, 3)" @click="handleSetMerge" />
  <input type="button" value="removeMerge('A3')" @click="handleRemoveMerge" />
  <input
    type="button"
    value="Get all merged cells"
    @click="handleGetAllMergedCells"
  />
  <input
    type="button"
    value="Destroy all merged"
    @click="handleDestroyAllMerged"
  />
</template>

<script>
import { Spreadsheet, Worksheet, jspreadsheet } from '@jspreadsheet/vue';
import 'jsuites/dist/jsuites.css';
import 'jspreadsheet/dist/jspreadsheet.css';

// You can use the following license for quick testing on localhost, StackBlitz, or CodeSandbox.
// The license is valid for one day, after which the spreadsheet will become read-only.
// For a longer trial period, you can create a free account and generate a demo license with an extended expiration date.
jspreadsheet.setLicense('MjJjZDJmZmUxY2FhYzMxZTQ5NTFlNDJlNDAxMTE4YWM4NTJjOTZmZTljYzZkMThiOWZiOGQyMThiNDUwNDhlZGEzM2E5NzNhOTA4NDBjYjA5YjE2ZjAyZmE3YmMyMzM4NjQ3MTQxYjI3MDBhOTNkNmQyNzdjZjlmMGNlYzNiYjUsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnprMU5USXhPRFF4TENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2liRzlqWVd4b2IzTjBJbDBzSW5Cc1lXNGlPaUl6TkNJc0luTmpiM0JsSWpwYkluWTNJaXdpZGpnaUxDSjJPU0lzSW5ZeE1DSXNJbll4TVNJc0luWXhNaUlzSW5ZeE15SXNJbU5vWVhKMGN5SXNJbVp2Y20xeklpd2labTl5YlhWc1lTSXNJbkJoY25ObGNpSXNJbkpsYm1SbGNpSXNJbU52YlcxbGJuUnpJaXdpYVcxd2IzSjBaWElpTENKaVlYSWlMQ0oyWVd4cFpHRjBhVzl1Y3lJc0luTmxZWEpqYUNJc0luQnlhVzUwSWl3aWMyaGxaWFJ6SWl3aVkyeHBaVzUwSWl3aWMyVnlkbVZ5SWl3aWMyaGhjR1Z6SWl3aVptOXliV0YwSWl3aWNHbDJiM1FpWFN3aVpHVnRieUk2ZEhKMVpYMD0=');

export default {
  components: {
    Spreadsheet,
    Worksheet,
  },
  data() {
    const worksheets = [
      {
        data: [
          ['Mazda', 2001, 2000, '2006-01-01 12:00:00'],
          ['Peugeot', 2010, 5000, '2005-01-01 13:00:00'],
          ['Honda Fit', 2009, 3000, '2004-01-01 14:01:00'],
          ['Honda CRV', 2010, 6000, '2003-01-01 23:30:00'],
        ],
        columnDrag: true,
        worksheetName: 'Merged Cells',
        minDimensions: [50, 5000],
        tableOverflow: true,
        tableWidth: '800px',
        tableHeight: '300px',
        columns: [
          {
            type: 'text',
            width: '300px',
            title: 'Model',
          },
          {
            type: 'text',
            width: '80px',
            title: 'Year',
          },
          {
            type: 'text',
            width: '100px',
            title: 'Price',
          },
          {
            type: 'calendar',
            width: '150px',
            title: 'Date',
            options: {
              format: 'DD/MM/YYYY HH24:MI',
              time: 1,
            },
          },
        ],
        mergeCells: {
          A1: [2, 2],
        },
      },
    ];

    return {
      worksheets,
    };
  },
  methods: {
    handleSetMerge() {
      this.$refs.spreadsheet.current[0].setMerge('A3', 2, 3);
    },

    handleRemoveMerge() {
      this.$refs.spreadsheet.current[0].removeMerge('A3');
    },

    handleGetAllMergedCells() {
      this.$refs.log.value = JSON.stringify(
        this.$refs.spreadsheet.current[0].getMerge()
      );
    },

    handleDestroyAllMerged() {
      this.$refs.spreadsheet.current[0].destroyMerge();
    },
  },
};
</script>
import { Component, ViewChild, ElementRef } from "@angular/core";
import jspreadsheet from "jspreadsheet";
import 'jsuites/dist/jsuites.css';
import 'jspreadsheet/dist/jspreadsheet.css';

// You can use the following license for quick testing on localhost, StackBlitz, or CodeSandbox.
// The license is valid for one day, after which the spreadsheet will become read-only.
// For a longer trial period, you can create a free account and generate a demo license with an extended expiration date.
jspreadsheet.setLicense('MjJjZDJmZmUxY2FhYzMxZTQ5NTFlNDJlNDAxMTE4YWM4NTJjOTZmZTljYzZkMThiOWZiOGQyMThiNDUwNDhlZGEzM2E5NzNhOTA4NDBjYjA5YjE2ZjAyZmE3YmMyMzM4NjQ3MTQxYjI3MDBhOTNkNmQyNzdjZjlmMGNlYzNiYjUsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnprMU5USXhPRFF4TENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2liRzlqWVd4b2IzTjBJbDBzSW5Cc1lXNGlPaUl6TkNJc0luTmpiM0JsSWpwYkluWTNJaXdpZGpnaUxDSjJPU0lzSW5ZeE1DSXNJbll4TVNJc0luWXhNaUlzSW5ZeE15SXNJbU5vWVhKMGN5SXNJbVp2Y20xeklpd2labTl5YlhWc1lTSXNJbkJoY25ObGNpSXNJbkpsYm1SbGNpSXNJbU52YlcxbGJuUnpJaXdpYVcxd2IzSjBaWElpTENKaVlYSWlMQ0oyWVd4cFpHRjBhVzl1Y3lJc0luTmxZWEpqYUNJc0luQnlhVzUwSWl3aWMyaGxaWFJ6SWl3aVkyeHBaVzUwSWl3aWMyVnlkbVZ5SWl3aWMyaGhjR1Z6SWl3aVptOXliV0YwSWl3aWNHbDJiM1FpWFN3aVpHVnRieUk2ZEhKMVpYMD0=');

// Create component
@Component({
    standalone: true,
    selector: "app-root",
    template: `<div #spreadsheet></div>
        <div #log></div>
        <input type="button" value="setMerge('A3', 2, 3)" (click)="this.worksheets[0].setMerge('A3', 2, 3);" />
        <input type="button" value="removeMerge('A3')" (click)="this.worksheets[0].removeMerge('A3');" />
        <input type="button" value="Get all merged cells" (click)="getMerge()" />
        <input type="button" value="Destroy all merged" (click)="this.worksheets[0].destroyMerge();" />`,
})
export class AppComponent {
    @ViewChild("spreadsheet") spreadsheet: ElementRef;
    @ViewChild("log") log: ElementRef;
    // Worksheets
    worksheets: jspreadsheet.worksheetInstance[];
    // Create a new data grid
    ngAfterViewInit() {
        // Create spreadsheet
        this.worksheets = jspreadsheet(this.spreadsheet.nativeElement, {
            toolbar: true,
            worksheets: [{
                data: [
                    ['Mazda', 2001, 2000, '2006-01-01 12:00:00'],
                    ['Peugeot', 2010, 5000, '2005-01-01 13:00:00'],
                    ['Honda Fit', 2009, 3000, '2004-01-01 14:01:00'],
                    ['Honda CRV', 2010, 6000, '2003-01-01 23:30:00'],
                ],
                columnDrag: true,
                worksheetName: 'Merged Cells',
                minDimensions: [50, 5000],
                tableOverflow: true,
                tableWidth: '800px',
                tableHeight: '300px',
                columns: [
                    {
                        type: 'text',
                        width: '300px',
                        title: 'Model',
                    },
                    {
                        type: 'text',
                        width: '80px',
                        title: 'Year',
                    },
                    {
                        type: 'text',
                        width: '100px',
                        title: 'Price',
                    },
                    {
                        type: 'calendar',
                        width: '150px',
                        title: 'Date',
                        options: {
                            format: 'DD/MM/YYYY HH24:MI',
                            time: 1,
                        }
                    },
                ],
                mergeCells: {
                    A1: [2, 2]
                }
            }]
        });
    }
    getMerge() {
        this.log.nativeElement.innerHTML = JSON.stringify(this.worksheets[0].getMerge());
    }
}

Batch operations

Batch operations allow you to apply multiple merge operations simultaneously with a single command.

Example

This example shows how to perform multiple merge operations simultaneously using batch commands.

<html>
<script src="https://jspreadsheet.com/v13/jspreadsheet.js"></script>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jspreadsheet.com/v13/jspreadsheet.css" type="text/css" />
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons" />

<div id="spreadsheet"></div>

<p>
<input type="button" value="Batch Set Merge" id="btn1" />
<input type="button" value="Batch Remove Merge" id="btn2" />
<input type="button" value="Get All Merged" id="btn3" />
</p>

<script>
// You can use the following license for quick testing on localhost, StackBlitz, or CodeSandbox.
// The license is valid for one day, after which the spreadsheet will become read-only.
// For a longer trial period, you can create a free account and generate a demo license with an extended expiration date.
jspreadsheet.setLicense('MjJjZDJmZmUxY2FhYzMxZTQ5NTFlNDJlNDAxMTE4YWM4NTJjOTZmZTljYzZkMThiOWZiOGQyMThiNDUwNDhlZGEzM2E5NzNhOTA4NDBjYjA5YjE2ZjAyZmE3YmMyMzM4NjQ3MTQxYjI3MDBhOTNkNmQyNzdjZjlmMGNlYzNiYjUsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnprMU5USXhPRFF4TENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2liRzlqWVd4b2IzTjBJbDBzSW5Cc1lXNGlPaUl6TkNJc0luTmpiM0JsSWpwYkluWTNJaXdpZGpnaUxDSjJPU0lzSW5ZeE1DSXNJbll4TVNJc0luWXhNaUlzSW5ZeE15SXNJbU5vWVhKMGN5SXNJbVp2Y20xeklpd2labTl5YlhWc1lTSXNJbkJoY25ObGNpSXNJbkpsYm1SbGNpSXNJbU52YlcxbGJuUnpJaXdpYVcxd2IzSjBaWElpTENKaVlYSWlMQ0oyWVd4cFpHRjBhVzl1Y3lJc0luTmxZWEpqYUNJc0luQnlhVzUwSWl3aWMyaGxaWFJ6SWl3aVkyeHBaVzUwSWl3aWMyVnlkbVZ5SWl3aWMyaGhjR1Z6SWl3aVptOXliV0YwSWl3aWNHbDJiM1FpWFN3aVpHVnRieUk2ZEhKMVpYMD0=');

// Create the spreadsheet
let table = jspreadsheet(document.getElementById('spreadsheet'), {
    toolbar: true,
    worksheets: [{
        data: [
            ['A1', 'B1', 'C1', 'D1', 'E1', 'F1'],
            ['A2', 'B2', 'C2', 'D2', 'E2', 'F2'],
            ['A3', 'B3', 'C3', 'D3', 'E3', 'F3'],
            ['A4', 'B4', 'C4', 'D4', 'E4', 'F4'],
            ['A5', 'B5', 'C5', 'D5', 'E5', 'F5'],
        ],
        minDimensions: [6, 6],
        tableOverflow: true,
        tableWidth: '600px',
        tableHeight: '300px',
    }]
});

// Batch set multiple merged cells at the same time
document.getElementById("btn1").onclick = () => {
    table[0].setMerge({ A1: [2,2], D1: [2,2] });
};

// Batch remove multiple merged cells at the same time
document.getElementById("btn2").onclick = () => {
    table[0].removeMerge({ A1: true, D1: true });
};

// Get all merged cells to see the current state
document.getElementById("btn3").onclick = () => {
    alert(JSON.stringify(table[0].getMerge()));
};
</script>
</html>
import React, { useRef } from 'react';
import { Spreadsheet, Worksheet, jspreadsheet } from '@jspreadsheet/react';
import 'jsuites/dist/jsuites.css';
import 'jspreadsheet/dist/jspreadsheet.css';

// You can use the following license for quick testing on localhost, StackBlitz, or CodeSandbox.
// The license is valid for one day, after which the spreadsheet will become read-only.
// For a longer trial period, you can create a free account and generate a demo license with an extended expiration date.
jspreadsheet.setLicense('MjJjZDJmZmUxY2FhYzMxZTQ5NTFlNDJlNDAxMTE4YWM4NTJjOTZmZTljYzZkMThiOWZiOGQyMThiNDUwNDhlZGEzM2E5NzNhOTA4NDBjYjA5YjE2ZjAyZmE3YmMyMzM4NjQ3MTQxYjI3MDBhOTNkNmQyNzdjZjlmMGNlYzNiYjUsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnprMU5USXhPRFF4TENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2liRzlqWVd4b2IzTjBJbDBzSW5Cc1lXNGlPaUl6TkNJc0luTmpiM0JsSWpwYkluWTNJaXdpZGpnaUxDSjJPU0lzSW5ZeE1DSXNJbll4TVNJc0luWXhNaUlzSW5ZeE15SXNJbU5vWVhKMGN5SXNJbVp2Y20xeklpd2labTl5YlhWc1lTSXNJbkJoY25ObGNpSXNJbkpsYm1SbGNpSXNJbU52YlcxbGJuUnpJaXdpYVcxd2IzSjBaWElpTENKaVlYSWlMQ0oyWVd4cFpHRjBhVzl1Y3lJc0luTmxZWEpqYUNJc0luQnlhVzUwSWl3aWMyaGxaWFJ6SWl3aVkyeHBaVzUwSWl3aWMyVnlkbVZ5SWl3aWMyaGhjR1Z6SWl3aVptOXliV0YwSWl3aWNHbDJiM1FpWFN3aVpHVnRieUk2ZEhKMVpYMD0=');

export default function App() {
    const spreadsheet = useRef();

    const worksheets = [
        {
            data: [
                ['A1', 'B1', 'C1', 'D1', 'E1', 'F1'],
                ['A2', 'B2', 'C2', 'D2', 'E2', 'F2'],
                ['A3', 'B3', 'C3', 'D3', 'E3', 'F3'],
                ['A4', 'B4', 'C4', 'D4', 'E4', 'F4'],
                ['A5', 'B5', 'C5', 'D5', 'E5', 'F5'],
            ],
            minDimensions: [6, 6],
            tableOverflow: true,
            tableWidth: '600px',
            tableHeight: '300px',
        },
    ];

    // Batch set multiple merged cells at the same time
    const handleBatchSetMerge = () => {
        spreadsheet.current[0].setMerge({ A1: [2,2], D1: [2,2] });
    };

    // Batch remove multiple merged cells at the same time
    const handleBatchRemoveMerge = () => {
        spreadsheet.current[0].removeMerge({ A1: true, D1: true });
    };

    // Get all merged cells to see the current state
    const handleGetAllMerged = () => {
        alert(JSON.stringify(spreadsheet.current[0].getMerge()));
    };

    return (
        <>
            <Spreadsheet
                ref={spreadsheet}
                toolbar
                worksheets={worksheets}
            />
            <p>
                <input
                    type="button"
                    value="Batch Set Merge"
                    onClick={handleBatchSetMerge}
                />
                <input
                    type="button"
                    value="Batch Remove Merge"
                    onClick={handleBatchRemoveMerge}
                />
                <input
                    type="button"
                    value="Get All Merged"
                    onClick={handleGetAllMerged}
                />
            </p>
        </>
    );
}
<template>
    <Spreadsheet ref="spreadsheet" toolbar :worksheets="worksheets" />
    <p>
        <input type="button" value="Batch Set Merge" @click="handleBatchSetMerge" />
        <input type="button" value="Batch Remove Merge" @click="handleBatchRemoveMerge" />
        <input type="button" value="Get All Merged" @click="handleGetAllMerged" />
    </p>
</template>

<script>
import { Spreadsheet, Worksheet, jspreadsheet } from '@jspreadsheet/vue';
import 'jsuites/dist/jsuites.css';
import 'jspreadsheet/dist/jspreadsheet.css';

// You can use the following license for quick testing on localhost, StackBlitz, or CodeSandbox.
// The license is valid for one day, after which the spreadsheet will become read-only.
// For a longer trial period, you can create a free account and generate a demo license with an extended expiration date.
jspreadsheet.setLicense('MjJjZDJmZmUxY2FhYzMxZTQ5NTFlNDJlNDAxMTE4YWM4NTJjOTZmZTljYzZkMThiOWZiOGQyMThiNDUwNDhlZGEzM2E5NzNhOTA4NDBjYjA5YjE2ZjAyZmE3YmMyMzM4NjQ3MTQxYjI3MDBhOTNkNmQyNzdjZjlmMGNlYzNiYjUsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnprMU5USXhPRFF4TENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2liRzlqWVd4b2IzTjBJbDBzSW5Cc1lXNGlPaUl6TkNJc0luTmpiM0JsSWpwYkluWTNJaXdpZGpnaUxDSjJPU0lzSW5ZeE1DSXNJbll4TVNJc0luWXhNaUlzSW5ZeE15SXNJbU5vWVhKMGN5SXNJbVp2Y20xeklpd2labTl5YlhWc1lTSXNJbkJoY25ObGNpSXNJbkpsYm1SbGNpSXNJbU52YlcxbGJuUnpJaXdpYVcxd2IzSjBaWElpTENKaVlYSWlMQ0oyWVd4cFpHRjBhVzl1Y3lJc0luTmxZWEpqYUNJc0luQnlhVzUwSWl3aWMyaGxaWFJ6SWl3aVkyeHBaVzUwSWl3aWMyVnlkbVZ5SWl3aWMyaGhjR1Z6SWl3aVptOXliV0YwSWl3aWNHbDJiM1FpWFN3aVpHVnRieUk2ZEhKMVpYMD0=');

export default {
    components: {
        Spreadsheet,
        Worksheet,
    },
    data() {
        const worksheets = [
            {
                data: [
                    ['A1', 'B1', 'C1', 'D1', 'E1', 'F1'],
                    ['A2', 'B2', 'C2', 'D2', 'E2', 'F2'],
                    ['A3', 'B3', 'C3', 'D3', 'E3', 'F3'],
                    ['A4', 'B4', 'C4', 'D4', 'E4', 'F4'],
                    ['A5', 'B5', 'C5', 'D5', 'E5', 'F5'],
                ],
                minDimensions: [6, 6],
                tableOverflow: true,
                tableWidth: '600px',
                tableHeight: '300px',
            },
        ];

        return {
            worksheets,
        };
    },
    methods: {
        // Batch set multiple merged cells at the same time
        handleBatchSetMerge() {
            this.$refs.spreadsheet.current[0].setMerge({ A1: [2,2], D1: [2,2] });
        },

        // Batch remove multiple merged cells at the same time
        handleBatchRemoveMerge() {
            this.$refs.spreadsheet.current[0].removeMerge({ A1: true, D1: true });
        },

        // Get all merged cells to see the current state
        handleGetAllMerged() {
            alert(JSON.stringify(this.$refs.spreadsheet.current[0].getMerge()));
        },
    },
};
</script>
import { Component, ViewChild, ElementRef } from "@angular/core";
import jspreadsheet from "jspreadsheet";
import 'jsuites/dist/jsuites.css';
import 'jspreadsheet/dist/jspreadsheet.css';

// You can use the following license for quick testing on localhost, StackBlitz, or CodeSandbox.
// The license is valid for one day, after which the spreadsheet will become read-only.
// For a longer trial period, you can create a free account and generate a demo license with an extended expiration date.
jspreadsheet.setLicense('MjJjZDJmZmUxY2FhYzMxZTQ5NTFlNDJlNDAxMTE4YWM4NTJjOTZmZTljYzZkMThiOWZiOGQyMThiNDUwNDhlZGEzM2E5NzNhOTA4NDBjYjA5YjE2ZjAyZmE3YmMyMzM4NjQ3MTQxYjI3MDBhOTNkNmQyNzdjZjlmMGNlYzNiYjUsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnprMU5USXhPRFF4TENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2liRzlqWVd4b2IzTjBJbDBzSW5Cc1lXNGlPaUl6TkNJc0luTmpiM0JsSWpwYkluWTNJaXdpZGpnaUxDSjJPU0lzSW5ZeE1DSXNJbll4TVNJc0luWXhNaUlzSW5ZeE15SXNJbU5vWVhKMGN5SXNJbVp2Y20xeklpd2labTl5YlhWc1lTSXNJbkJoY25ObGNpSXNJbkpsYm1SbGNpSXNJbU52YlcxbGJuUnpJaXdpYVcxd2IzSjBaWElpTENKaVlYSWlMQ0oyWVd4cFpHRjBhVzl1Y3lJc0luTmxZWEpqYUNJc0luQnlhVzUwSWl3aWMyaGxaWFJ6SWl3aVkyeHBaVzUwSWl3aWMyVnlkbVZ5SWl3aWMyaGhjR1Z6SWl3aVptOXliV0YwSWl3aWNHbDJiM1FpWFN3aVpHVnRieUk2ZEhKMVpYMD0=');

// Create component
@Component({
    standalone: true,
    selector: "app-root",
    template: `
        <div #spreadsheet></div>
        <p>
            <input type="button" value="Batch Set Merge" (click)="handleBatchSetMerge()" />
            <input type="button" value="Batch Remove Merge" (click)="handleBatchRemoveMerge()" />
            <input type="button" value="Get All Merged" (click)="handleGetAllMerged()" />
        </p>
    `,
})
export class AppComponent {
    @ViewChild("spreadsheet") spreadsheet: ElementRef;
    // Worksheets
    worksheets: jspreadsheet.worksheetInstance[];

    // Create a new data grid
    ngAfterViewInit() {
        // Create spreadsheet
        this.worksheets = jspreadsheet(this.spreadsheet.nativeElement, {
            toolbar: true,
            worksheets: [{
                data: [
                    ['A1', 'B1', 'C1', 'D1', 'E1', 'F1'],
                    ['A2', 'B2', 'C2', 'D2', 'E2', 'F2'],
                    ['A3', 'B3', 'C3', 'D3', 'E3', 'F3'],
                    ['A4', 'B4', 'C4', 'D4', 'E4', 'F4'],
                    ['A5', 'B5', 'C5', 'D5', 'E5', 'F5'],
                ],
                minDimensions: [6, 6],
                tableOverflow: true,
                tableWidth: '600px',
                tableHeight: '300px',
            }]
        });
    }

    // Batch set multiple merged cells at the same time
    handleBatchSetMerge() {
        this.worksheets[0].setMerge({ A1: [2,2], D1: [2,2] });
    }

    // Batch remove multiple merged cells at the same time
    handleBatchRemoveMerge() {
        this.worksheets[0].removeMerge({ A1: true, D1: true });
    }

    // Get all merged cells to see the current state
    handleGetAllMerged() {
        alert(JSON.stringify(this.worksheets[0].getMerge()));
    }
}

See Also