Protect Worksheet or Workbook Elements"
(→ZUML) |
m (correct highlight (via JWB)) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{ZKSpreadsheetEssentialsPageHeader}} | {{ZKSpreadsheetEssentialsPageHeader}} | ||
+ | |||
+ | |||
+ | {{Deprecated|url=http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials}} | ||
+ | |||
+ | |||
__TOC__ | __TOC__ | ||
Line 17: | Line 22: | ||
===ZUML=== | ===ZUML=== | ||
− | <source lang="xml" | + | <source lang="xml" highlight="5,6"> |
<zk> | <zk> | ||
<div height="100%" width="100%" apply="org.zkoss.zssessentials.config.ProtectionComposer"> | <div height="100%" width="100%" apply="org.zkoss.zssessentials.config.ProtectionComposer"> | ||
Line 37: | Line 42: | ||
====Protect Worksheet==== | ====Protect Worksheet==== | ||
Use <javadoc directory="zss" method="protectSheet(java.lang.String)">org.zkoss.zss.model.Range</javadoc> to set worksheet protection. | Use <javadoc directory="zss" method="protectSheet(java.lang.String)">org.zkoss.zss.model.Range</javadoc> to set worksheet protection. | ||
− | <source lang="java" | + | <source lang="java" highlight="7"> |
public void onClick$toggleSheetProtection(Event event) { | public void onClick$toggleSheetProtection(Event event) { | ||
final Worksheet sheet = spreadsheet.getSelectedSheet(); | final Worksheet sheet = spreadsheet.getSelectedSheet(); | ||
Line 48: | Line 53: | ||
Use <javadoc directory="zss" method="setLocked(java.lang.Boolean)">org.zkoss.poi.ss.usermodel.CellStyle</javadoc> to lock cells. Use the <javadoc directory="zss" method="setStyle(org.zkoss.poi.ss.usermodel.CellStyle)">org.zkoss.zss.model.Range</javadoc> to update cell style. | Use <javadoc directory="zss" method="setLocked(java.lang.Boolean)">org.zkoss.poi.ss.usermodel.CellStyle</javadoc> to lock cells. Use the <javadoc directory="zss" method="setStyle(org.zkoss.poi.ss.usermodel.CellStyle)">org.zkoss.zss.model.Range</javadoc> to update cell style. | ||
− | <source lang="java" | + | <source lang="java" highlight="3,10,11"> |
public void onClick$toggleLockCells(Event event) { | public void onClick$toggleLockCells(Event event) { | ||
final Worksheet sheet = spreadsheet.getSelectedSheet(); | final Worksheet sheet = spreadsheet.getSelectedSheet(); |
Latest revision as of 12:56, 19 January 2022
This article is out of date, please refer to http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials for more up to date information.
Purpose
Preventing end users from modifying important data, ZK Spreadsheet allow developers to apply protection to worksheet or cells.
Protection
On default, the sheet is not protected and the cell is locked. In this status, the cell can be modified.
Protected sheet
Protected sheet with locked cell. In this status, the cell can not be modified.
Protected sheet with unlocked cell. In this status, the cell can be modified
ZUML
<zk>
<div height="100%" width="100%" apply="org.zkoss.zssessentials.config.ProtectionComposer">
<div>
<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>
</div>
<spreadsheet id="spreadsheet" src="/WEB-INF/excel/config/autoFilter.xlsx"
maxrows="200"
maxcolumns="40"
width="100%"
height="450px"></spreadsheet>
</div>
</zk>
Composer
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());
newCellStyle.setLocked(lock);
Ranges.range(sheet, r, c).setStyle(newCellStyle);
}
}
}
}
View complete source of ZUML protection.zul
View complete source of composer ProtectionComposer.java
Version History
Version | Date | Content |
---|---|---|
2.1.0 | May, 2011 | Protect sheet and lock cell |
All source code listed in this book is at Github.