Protect Cells
Contents
[
Hide
]
This topic describes a few techniques for protecting cells. Using these techniques allows developers to restrict users from editing all or a selected range of cells in a worksheet.
Protecting Cells
Aspose.Cells.GridWeb provides a few different techniques for controlling the protection level on cells when the control is in Edit mode (the default mode). This protects cells from being modified by end users.
Making All Cells Read Only
To set all cells in a worksheet to read only, call the worksheet’s SetAllCellsReadonly method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the reference of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Setting all cells of the worksheet to Readonly | |
sheet.SetAllCellsReadonly(); |
Making All Cells Editable
To remove protection from all cells, call the worksheet’s SetAllCellsEditable method. This method has the opposite effect to the SetAllCellsReadonly method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the reference of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Setting all cells of the worksheet to Editable | |
sheet.SetAllCellsEditable(); |
Making Selected Cells Read Only
To protect only a range of cells:
- First make all cells editable by calling the SetAllCellsEditable method.
- Specify the range of cells that to protect by calling the worksheet’s SetReadonlyRange method. This method takes the number of rows and columns to specify the range of cells.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the reference of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Setting all cells of the worksheet to Editable first | |
sheet.SetAllCellsEditable(); | |
// Finally, Setting selected cells of the worksheet to Readonly | |
sheet.SetReadonlyRange(3, 2, 4, 1); |
Making Selected Cells Editable
To un-protect a range of cells:
- Make all cells read only by calling the SetAllCellsReadonly method.
- Specify the range of cells that to be editable by calling the worksheet’s SetEditableRange method. This method takes the number of rows and columns to specify the range of cells.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the reference of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Setting all cells of the worksheet to Readonly first | |
sheet.SetAllCellsReadonly(); | |
// Finally, Setting selected cells of the worksheet to Editable | |
sheet.SetEditableRange(3, 2, 4, 1); |