SpreadJS supports the auto merge feature allowing users to automatically merge the neighboring cells containing duplicate text. During the auto merge operation, no data is lost in the process.
The auto merge feature helps users to:
While executing data analysis on a worksheet, users may want to combine several cells containing identical text for enhanced data presentation and better structure.
For example - While generating sales reports, it's a good idea to automatically merge matching states or city names together to make it look neater and cleaner while ensuring that no information loss occurs and the data remains intact in the merged cells.
The following screenshot depicts how the auto merge operation combines the names of countries, states and cities in a worksheet.

The auto merge feature automatically merges the contiguous cells with identical text and adjusts the text position of the merged cells.
This feature works upon the condition that the cells you want to merge shouldn't belong to any span range. While working with SpreadJS, users can use the autoMerge() method to apply the auto merge feature to specified ranges, including range, row, column and the entire worksheet. The directions supported by the AutoMergeDirection enumeration include: row direction, column direction and both row and column direction.
SpreadJS provides support for the following two modes available in the AutoMergeMode enumeration:
The basic differences between executing the "Auto Merge Cells" operation and "Spanning and Merging Cells" in a worksheet are:
Note: The following limitations must be kept in mind while using the auto merge feature:
The following code snippet merges the cells containing identical country names, states and cities using the autoMerge() method.
| JavaScript |
Copy Code
|
|---|---|
<script> var data = [ { "Country": "Canada", "State": "Ontario", "City": "Ottawa", "Product": "Kraft Grated Parmesan Cheese" }, { "Country": "Canada", "State": "Ontario", "City": "Belleville", "Product": "KIND Bars Almond & Coconut Gluten Free" }, { "Country": "Canada", "State": "Ontario", "City": "Alliston", "Product": "Kraft Grated Parmesan Cheese" }, { "Country": "Canada", "State": "Saskatchewan", "City": "Prince Albert", "Product": "Smartfood Popcorn" }, { "Country": "Canada", "State": "Alberta", "City": "Red Deer", "Product": "Smartfood Popcorn" }, { "Country": "Canada", "State": "Alberta", "City": "Calgary", "Product": "Planters Deluxe Whole Cashew" }, { "Country": "Canada", "State": "Alberta", "City": "Calgary", "Product": "Kraft Grated Parmesan Cheese" }, { "Country": "Canada", "State": "Alberta", "City": "Okotoks", "Product": "Smartfood Popcorn" }, { "Country": "India", "State": "Andhra Pradesh", "City": "Hyderabad", "Product": "Teddy Grahams Crackers" }, { "Country": "South Africa", "State": "Gauteng", "City": "Roodepoort", "Product": "Jaybee's Gourmet Nuts Gift Pack (3 Lb)" }, { "Country": "Finland", "State": "Ita-Suomen Laani", "City": "Kuopio", "Product": "Planters Deluxe Whole Cashew" }, { "Country": "Switzerland", "State": "Geneve", "City": "Vesenaz", "Product": "KIND Bars Almond & Coconut Gluten Free" }, { "Country": "Switzerland", "State": "Vaud", "City": "Lausanne", "Product": "Smartfood Popcorn" }, { "Country": "Switzerland", "State": "Vaud", "City": "Morges", "Product": "Kraft Real Mayo" }, { "Country": "Denmark", "State": "Frederiksborg", "City": "Helsingor", "Product": "Planters Deluxe Whole Cashew" }, { "Country": "Denmark", "State": "Kobenhavn", "City": "Kobenhavn", "Product": "Kraft Grated Parmesan Cheese" }, { "Country": "Denmark", "State": "Storstrom", "City": "Nakskov", "Product": "Kraft Grated Parmesan Cheese" } ] </script> <script> $(document).ready(function () { GC.Spread.Sheets.LicenseKey = "xxx"; // Initializing Spread var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss'), { sheetCount: 1 }); // Get the activesheet var activeSheet = spread.getActiveSheet(); // Bind data source activeSheet.setRowHeight(0, 30, 1); activeSheet.autoGenerateColumns = true; activeSheet.setDataSource(data); /* Merging complete sheet cells when AutoMergeDirection is set to Column & AutoMergeMode is set to restricted mode */ var range = new GC.Spread.Sheets.Range(-1, -1, -1, -1); activeSheet.autoMerge(range, GC.Spread.Sheets.AutoMerge.AutoMergeDirection.column, GC.Spread.Sheets.AutoMerge.AutoMergeMode.restricted); // Set the column width for (var c = 0; c < activeSheet.getColumnCount(); c++) activeSheet.setColumnWidth(c, 130.0, GC.Spread.Sheets.SheetArea.viewport); }); </script> |
|