Advanced Protection Settings since Excel XP with JavaScript via C++
Introduction
These protection settings restrict or allow users to:
- Delete rows or columns.
- Edit contents, objects, or scenarios.
- Format cells, rows, or columns.
- Insert rows, columns, or hyperlinks.
- Select locked or unlocked cells.
- Use pivot tables and much more.
Aspose.Cells for JavaScript via C++ supports all the advanced protection settings offered by Excel XP or later versions.
Advanced Protection Settings Using Excel XP and Later Versions
To view the protection settings available in Excel XP:
- From the Tools menu, select Protection followed by Protect Sheet. A dialog will be displayed.
To view the protection settings available in Excel 2016:
- From the File menu, select Protect Workbook followed by Protect Current Sheet.
- Select the Protect Sheet in the Review menu.
Following the steps mentioned above will show a dialog where you can allow or restrict worksheet features or apply a password to the worksheet.
Advanced Protection Settings Using Aspose.Cells for JavaScript via C++
Aspose.Cells for JavaScript via C++ supports all of the advanced protection settings.
Aspose.Cells provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a Worksheets collection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class.
The Worksheet class provides the protection property that is used to apply these advanced protection settings. The protection property is in fact an object of the Protection class that encapsulates several Boolean properties for disabling or enabling restrictions.
Below is a small example application. It opens an Excel file and uses most of the advanced protection settings supported by Excel XP and later versions.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
<meta charset="utf-8" />
</head>
<body>
<h1>Aspose.Cells Worksheet Protection Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
const workbook = new Workbook(new Uint8Array(arrayBuffer));
const worksheet = workbook.worksheets.get(0);
const protection = worksheet.protection;
// Restricting users to delete columns of the worksheet
protection.allowDeletingColumn = false;
// Restricting users to delete row of the worksheet
protection.allowDeletingRow = false;
// Restricting users to edit contents of the worksheet
protection.allowEditingContent = false;
// Restricting users to edit objects of the worksheet
protection.allowEditingObject = false;
// Restricting users to edit scenarios of the worksheet
protection.allowEditingScenario = false;
// Restricting users to filter
protection.allowFiltering = false;
// Allowing users to format cells of the worksheet
protection.allowFormattingCell = true;
// Allowing users to format rows of the worksheet
protection.allowFormattingRow = true;
// Allowing users to insert columns in the worksheet
protection.allowFormattingColumn = true;
// Allowing users to insert hyperlinks in the worksheet
protection.allowInsertingHyperlink = true;
// Allowing users to insert rows in the worksheet
protection.allowInsertingRow = true;
// Allowing users to select locked cells of the worksheet
protection.allowSelectingLockedCell = true;
// Allowing users to select unlocked cells of the worksheet
protection.allowSelectingUnlockedCell = true;
// Allowing users to sort
protection.allowSorting = true;
// Allowing users to use pivot tables in the worksheet
protection.allowUsingPivotTable = true;
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Worksheet protection settings applied successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Cell Locking Issue
If you want to restrict users from editing cells, the cells must be locked before any protection settings are applied. Otherwise, the cells can be edited even if the worksheet is protected. In Microsoft Excel XP, cells can be locked through the following dialog:
Dialog to lock cells in Excel XP |
---|
![]() |
It is possible to lock cells using the Aspose.Cells API too. Each cell can get Style formatting that contains a Boolean property, isLocked(). Set the isLocked() property to true or false to lock or unlock the cell.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Protect Worksheet Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, ProtectionType, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.worksheets.get(0);
// Lock cell A1 by setting the style property
const cell = worksheet.cells.get("A1");
cell.style.isLocked = true;
// Protect the sheet now.
worksheet.protect(ProtectionType.All);
// Saving the modified Excel file and provide download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Result';
document.getElementById('result').innerHTML = '<p style="color: green;">Worksheet protected successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>