[]
        
(Showing Draft Content)

Protect Cell Range

DsExcel allows you to protect cells in a worksheet by locking the cells or hiding the formulas in the cells with Locked and FormulaHidden properties of IRange and IStyle interfaces.

Interface

Property

Description

IRange

Locked

Locks specific cell range of a worksheet.

FormulaHidden

Hides formulas in a specific cell range of a worksheet.

IStyle

Locked

Locks all worksheet cells with style.

FormulaHidden

Hides formulas in all worksheet cells with style.

!type=note

Note: By default, Locked property is set to true, and FormulaHidden property is set to false.

!type=note

Note: Locked and FormulaHidden properties will work when the worksheet is protected. For more information on protecting a worksheet, see Protect a Worksheet.

Lock Cells

Lock Specific Cell Range

Refer to the following example code to lock a specific cell range of a worksheet when protected:

// Lock cell range C7:C11. 
worksheet.Range["C7:C11"].Locked = true;

Lock Worksheet Cells with Style

Refer to the following example code to lock worksheet cells with style when protected:

// Lock cells with custom style.
IStyle style = workbook.Styles.Add("CustomStyle");
style.Locked = true;
worksheet.Range["B2:D18"].Style = style;

By default, all the cells in a worksheet have "Normal" style, and you can lock all the worksheet cells by setting Locked property of the style of any cell. Refer to the following example code to lock all the worksheet cells:

// Lock all worksheet cells.
worksheet.Range["A1"].Style.Locked = true;

Hide Formula in Cells

Hide Formula in Specific Cell Range

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.Range["C7:C19"].FormulaHidden = true;

Hide Formula in Worksheet Cells with Style

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.Styles.Add("CustomStyle");
style.FormulaHidden = true;
worksheet.Range["B2:D18"].Style = style;

By default, all the cells in a worksheet have "Normal" style, and you can hide formulas in all the worksheet cells by setting FormulaHidden property 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.Range["A1"].Style.FormulaHidden = true;