[]
DsExcel allows you to protect cells in a worksheet by locking the cells or hiding the formulas in the cells with setLocked and setFormulaHidden methods of IRange and IStyle interfaces.
Interface | Method | Description |
---|---|---|
IRange | setLocked | Locks specific cell range of a worksheet. |
setFormulaHidden | Hides formulas in a specific cell range of a worksheet. | |
IStyle | setLocked | Locks all worksheet cells with style. |
setFormulaHidden | Hides formulas in all worksheet cells with style. |
!type=note
Note: By default, setLocked method is set to true, and setFormulaHidden method is set to false.
!type=note
Note: setLocked and setFormulaHidden methods will work when the worksheet is protected. For more information on protecting a worksheet, see Protect a Worksheet.
Refer to the following example code to lock a specific cell range of a worksheet when protected:
// Lock cell range C7:C11.
worksheet.getRange("C7:C11").setLocked(true);
Refer to the following example code to lock worksheet cells with style when protected:
// Lock cells with custom style.
IStyle style = workbook.getStyles().add("CustomStyle");
style.setLocked(true);
worksheet.getRange("B2:D18").setStyle(style);
By default, all the cells in a worksheet have "Normal" style, and you can lock all the worksheet cells by setting setLocked method of the style of any cell. Refer to the following example code to lock all the worksheet cells:
// Lock all worksheet cells.
worksheet.getRange("A1").getStyle().setLocked(true);
Refer to the following example code to hide formulas in a specific cell range of a worksheet when protected:
// Hide formula(s) in cell range C7:C19.
worksheet.getRange("C7:C19").setFormulaHidden(true);
Refer to the following example code to hide formulas in all worksheet cells with a specific style when protected:
// Hide formula in cells with custom style.
IStyle style = workbook.getStyles().add("CustomStyle");
style.setFormulaHidden(true);
worksheet.getRange("B2:D18").setStyle(style);
By default, all the cells in a worksheet have "Normal" style, and you can hide formulas in all the worksheet cells by setting setFormulaHidden method of the style of any cell. Refer to the following example code to hide the formula in all the worksheet cells:
// Hide formula in all worksheet cells.
worksheet.getRange("A1").getStyle().setFormulaHidden(true);