Merge Cells or Split Merged Cells

From Documentation





Stop.png This article is out of date, please refer to http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials for more up to date information.

Purpose

ZK Spreadsheet support merge cells and split merged cells.

Merge cells

Merge cell can cross rows, also known as vertical merge.
ZKSsEss Spreadsheet MergeCell.png

ZUML

<zk>
<div height="100%" width="100%" apply="org.zkoss.zssessentials.config.MergeCellComposer">
	<div>
		<button id="mergeCells" label="Merge Cells" mold="trendy"></button>
		<button id="splitMergedCells" label="Split Merged Cells" mold="trendy"></button>
	</div>
	<spreadsheet id="spreadsheet" src="/WEB-INF/excel/config/merge.xlsx"	
				maxrows="200" 
				maxcolumns="40"
				width="100%"
				height="450px"></spreadsheet>
</div>
</zk>

Composer

Range to merge or split

Use CellSelectionEvent to get current range.

Spreadsheet spreadsheet;
int topRow;
int bottomRow;
int leftCol;
int rightCol;

public void onCellSelection$spreadsheet(CellSelectionEvent event) {
	topRow = event.getTop();
	bottomRow = event.getBottom();
	leftCol = event.getLeft();
	rightCol = event.getRight();
}

ZKSsEss Spreadsheet MergeCell CellSelection.png

Merge Cells

Use Range.merge(Boolean)to merge cells. The boolean argument indicates whether to merge cells for each individual rows. true to merge cells for each row while false indicates merge cells as a whole.

public void onClick$mergeCells() {
	Ranges.range(spreadsheet.getSelectedSheet(), 
			topRow, 
			leftCol, 
			bottomRow, 
			rightCol).merge(false);
}

ZKSsEss Spreadsheet MergeCell Merge.png

Split Merged Cells

public void onClick$splitMergedCells() {
	Ranges.range(spreadsheet.getSelectedSheet(), 
			topRow, 
			leftCol, 
			bottomRow, 
			rightCol).unMerge();
}

ZKSsEss Spreadsheet MergeCell Split CellSelection.png
ZKSsEss Spreadsheet MergeCell Split Cells.png

View the complete source of ZUML mergeCells.zul

View the complete source of composer MergeCellComposer.java

Version History

Last Update : 2022/01/19


Version Date Content
2.1.0 May, 2011 support vertical merge


All source code listed in this book is at Github.

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.