Spread.Sheets Documentation
Using Protect and Cell Locking
Spread.Sheets Documentation > Developer's Guide > Managing the User Interface > Using Protect and Cell Locking

Spread JS allows users to protect the sheet and lock cells in the widget.

Protecting the sheet prevents cells from being edited. If you wish to allow the user to edit specific cells, protect the sheet and unlock only the cells you want the user to edit. Data can be copied from locked cells.

In order to protect a worksheet, you need to set the isProtected option to true. In order to lock a cell, you can use the locked method.

The options.protectionOptions property can be used to specify what areas you want the user to be allowed to change. Areas can include resizing, dragging, inserting or deleting rows or columns, and so on.

When the isProtected option is set to true, the following properties will take effect in the below mentioned ways:

  1. allowDragInsertRows - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to perform the drag operation while inserting rows.
  2. allowDragInsertColumns - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to perform the drag operation while inserting columns.
  3. allowInsertRows - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to insert rows.
  4. allowInsertColumns - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to insert columns.
  5. allowDeleteRows - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to delete rows.
  6. allowDeleteColumns - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to delete columns.
  7. allowSelectLockedCells - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to select locked cells.
  8. allowSelectUnlockedCells - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to select unlocked cells.
  9. allowSort - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to sort ranges.
  10. allowFilter - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to filter ranges.
  11. allowEditObjects - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to edit floating objects.
  12. allowResizeRows - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to resize rows.
  13. allowResizeColumns - The default value of this option is not defined. When this option is set to false, it doesn't allow the user to resize columns.

When the isProtected option is set to false, the above protection options will not take any effect.

Using Code

This example unlocks a cell.

JavaScript
Copy Code
sheet.getCell(1,1, GC.Spread.Sheets.SheetArea.viewport).locked(false);
sheet.setValue(1,1,"unLocked");
sheet.getRange(-1,3, -1, 1).locked(false);
sheet.getRange(5, -1, 1, -1).locked(false);
sheet.options.isProtected = true;

This example protects the worksheet using various protection options that are available in Spread JS.

JavaScript
Copy Code
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
var sheet = spread.getActiveSheet();
sheet.options.isProtected = true;
sheet.options.protectionOptions.allowDeleteRows  = true;
sheet.options.protectionOptions.allowDeleteColumns = true;
sheet.options.protectionOptions.allowInsertRows = true;
sheet.options.protectionOptions.allowInsertColumns = true;
sheet.options.protectionOptions.allowDargInsertRows = true;
sheet.options.protectionOptions.allowDragInsertColumns = true;
Note: The protection options : allowInsertRows, allowInsertColumns, allowDeleteRows and allowDeleteColumns are available only when you are performing an operation on the user interface. In other words, the value will only restrict insertion and deletion of rows and columns command in the context menu.