@abdelaspose
Preventing Both Editing and Deleting of Formula Cells in Aspose.Cells.GridJS
When you protect a worksheet and set a cell’s Locked flag, Excel (and GridJS) will block any direct edit of that cell.
However, the Delete key (or a program‑matic call to setValue(null)
) can still clear the cell’s content if the sheet protection does not forbid “clear‑contents” operations.
Below is a complete, step‑by‑step solution that works for Aspose.Cells.GridJS:
- Mark the formula cells as locked
- Protect the worksheet with the appropriate protection options (disable “Clear contents”, “Delete rows/columns”, etc.).
- (Optional)‑ Add a client‑side guard so that any attempt to change a locked cell via the GridJS UI is cancelled.
Lock the cells that contain formulas
// assume `workbook` is an instance of Aspose.Cells.GridJS.Workbook
var sheet = workbook.getWorksheets().get(0); // first sheet
var range = sheet.getRange("B2:D10"); // cells with formulas
// lock every cell in the range
for (var r = range.getFirstRow(); r <= range.getLastRow(); r++) {
for (var c = range.getFirstColumn(); c <= range.getLastColumn(); c++) {
var cell = sheet.getCell(r, c);
cell.getStyle().setLocked(true); // <-- this is the key flag
}
}
Why?
In GridJS a cell is editable only when both the worksheet is unprotected or the cell’s Locked
property is false
. Setting Locked = true
tells the protection engine to treat the cell as read‑only.
Protect the worksheet and disable “clear‑contents”
// protect the sheet with a password (optional)
var protection = sheet.getProtection();
protection.setPassword("MySecretPwd");
// ---------- Restrict actions on protected sheets ----------
protection.setAllowDeletingRows(false);
protection.setAllowDeletingColumns(false);
protection.setAllowEditingObject(false);
protection.setAllowEditingScenario(false);
protection.setAllowFormattingCells(false);
protection.setAllowFormattingColumns(false);
protection.setAllowFormattingRows(false);
protection.setAllowInsertingRows(false);
protection.setAllowInsertingColumns(false);
protection.setAllowSelectLockedCells(true); // allow selection (read‑only)
protection.setAllowSelectUnlockedCells(true);
// IMPORTANT: this flag stops the **Clear Contents** command
protection.setAllowDeleteRows(false); // prevents row‑level delete
protection.setAllowDeleteColumns(false); // prevents column‑level delete
// finally, enable protection
protection.setProtected(true);
The setAllowDeleteRows/Columns
flags are the GridJS equivalents of Excel’s “Delete rows/columns” protection option. When they are false, the UI will also block the Delete key (which internally tries to clear the cell).
Optional – Client‑side safeguard (recommended for custom UI)
If you expose a toolbar or allow JavaScript code to change cell values directly, you should also listen to the CellChanging event and cancel the operation when the target cell is locked.
workbook.on("cellChanging", function (e) {
var cell = e.cell; // Aspose.Cells.GridJS.Cell
var style = cell.getStyle();
// If the cell is locked, reject the change
if (style.getLocked()) {
// Cancel the operation
e.cancel = true;
// (Optional) show a friendly message
alert("This cell contains a protected formula and cannot be modified or cleared.");
}
});
The event handler runs before any value is written, so even a scripted call such as cell.setValue(null)
will be blocked.
Summary Checklist
Step |
Action |
GridJS API |
1 |
Set Locked = true on every formula cell |
cell.getStyle().setLocked(true); |
2 |
Protect the worksheet and disable delete/clear operations |
sheet.getProtection().setProtected(true); + related setAllow* flags |
3 (optional) |
Cancel any programmatic edit of locked cells |
workbook.on("cellChanging", …); |
After these steps:
- Users can select the protected cells (read‑only) – useful for review purposes.
- The Delete key, right‑click → Clear Contents, or any JavaScript call that tries to wipe the cell will be rejected.
Related Aspose Documentation
If you run into any issues while implementing the above code (e.g., version‑specific API differences), please let us know:
- Which version of Aspose.Cells.GridJS you are using.
- Whether the problem occurs in the UI, via the API, or both.
We’ll be happy to fine‑tune the solution for your exact environment.