Protect Worksheet or Workbook Elements

From Documentation
Revision as of 12:56, 19 January 2022 by Hawk (talk | contribs) (correct highlight (via JWB))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Protect Worksheet or Workbook Elements

Stop.png This article is out of date, please refer to for more up to date information.


Preventing end users from modifying important data, ZK Spreadsheet allow developers to apply protection to worksheet or cells.


On default, the sheet is not protected and the cell is locked. In this status, the cell can be modified.
ZKSsEss Spreadsheet Unprotected Sheet.png

Protected sheet
Protected sheet with locked cell. In this status, the cell can not be modified.
ZKSsEss Spreadsheet Protected LockedCell.png
Protected sheet with unlocked cell. In this status, the cell can be modified
ZKSsEss Spreadsheet Protected UnLockedCell.png


<div height="100%" width="100%" apply="org.zkoss.zssessentials.config.ProtectionComposer">
    	        <label id="message" style="color: red; padding-right: 5px;"></label>
                <button id="toggleSheetProtection" label="Toggle sheet protection" mold="trendy"></button>
                <button id="toggleLockCells" label="Toggle lock cells" mold="trendy"></button>
	<spreadsheet id="spreadsheet" src="/WEB-INF/excel/config/autoFilter.xlsx"	


Protect Worksheet

Use Range.protectSheet(String) to set worksheet protection.

public void onClick$toggleSheetProtection(Event event) {
	final Worksheet sheet = spreadsheet.getSelectedSheet();
	boolean sheetProtection = !sheet.getProtect();
	Ranges.range(sheet).protectSheet(sheetProtection ? "password" : null);

Lock cells

Use CellStyle.setLocked(Boolean) to lock cells. Use the Range.setStyle(CellStyle) to update cell style.

public void onClick$toggleLockCells(Event event) {
	final Worksheet sheet = spreadsheet.getSelectedSheet();
	boolean lock = !Utils.getOrCreateCell(sheet, topRow, leftCol).getCellStyle().getLocked();
	for (int r = topRow; r <= bottomRow; r++) {
		for (int c = leftCol; c <= rightCol; c++) {
			Cell cell = Utils.getOrCreateCell(sheet, r, c);
			CellStyle cellStyle = cell.getCellStyle();
			if (cellStyle.getLocked() != lock) {
				CellStyle newCellStyle = cloneStyle(cellStyle, sheet.getBook());
				Ranges.range(sheet, r, c).setStyle(newCellStyle);

View complete source of ZUML protection.zul

View complete source of composer

Version History

Last Update : 2022/01/19

Version Date Content
2.1.0 May, 2011 Protect sheet and lock cell

All source code listed in this book is at Github.

Last Update : 2022/01/19

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