Check Password to modify using Aspose.Cells for JavaScript via C++
Contents
[
Hide
]
Sometimes, you need to check if the given password matches with the Password to modify programmatically. Aspose.Cells provides the
WorkbookSettings.writeProtection.validatePassword()
method which you can use to check if the given Password to modify is correct or not.
Check Password to modify in Microsoft Excel
You can assign Password to open and Password to modify while creating your workbooks in Microsoft Excel. Please see this screenshot which shows the interface Microsoft Excel provides to specify these passwords.
![]() |
---|
Check Password to modify using Aspose.Cells for JavaScript via C++
The following sample codes load the source Excel file. It has a Password to open as 1234 and Password to modify as 5678. The code first checks if 567 is the correct Password to modify and it returns false, and then it checks if 5678 is the Password to modify and it returns true.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Check Write Protection Passwords</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, LoadOptions, 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();
// Specify password to open inside the load options
const opts = new LoadOptions();
opts.password = "1234";
// Open the source Excel file with load options
const workbook = new Workbook(new Uint8Array(arrayBuffer), opts);
// Check if 567 is Password to modify
let ret = workbook.settings.writeProtection.validatePassword("567");
let outputHtml = `<p>Is 567 correct Password to modify: ${ret}</p>`;
// Check if 5678 is Password to modify
ret = workbook.settings.writeProtection.validatePassword("5678");
outputHtml += `<p>Is 5678 correct Password to modify: ${ret}</p>`;
document.getElementById('result').innerHTML = outputHtml;
});
</script>
</html>
Console Output
Is 567 correct Password to modify: False
Is 5678 correct Password to modify: True