Hello,
I am working with various levels of password protection and Aspose.Cells .NET 18.5.1 (via NuGet).
I have an XLSX file that is password protected (PSWD: PEERNET) at the WORKBOOK level (not encrypted): ProtectWBStructurePassword_PEERNET_FilteredAndCommentedCells.zip (9.8 KB)
I protected this Workbook using the Protect Workbook option on the Review tab: ProtectWorkbook.png (10.2 KB)
When I open this file in Excel, and cannot add/delete/move any sheets in the protected workbook until I remove the protection. See image of worksheet tab context menu: SheetContextMenu.png (6.6 KB)
Using Aspose, I can detect that the workbook is protected but I can still add a new worksheet to the end of my collection. I would have expected that action to fail or throw an exception.
Here a code snippet showing what I am doing:
wBook = new Aspose.Cells.Workbook(FILE_PATH, lo);
// Try to unprotect
if (wBook.Settings.IsProtected &&
(wBook.Settings.ProtectionType == Aspose.Cells.ProtectionType.All ||
wBook.Settings.ProtectionType == Aspose.Cells.ProtectionType.Structure ||
wBook.Settings.ProtectionType == Aspose.Cells.ProtectionType.Windows))
{
try
{
wBook.Unprotect(UnprotectPassword);
TestContext.WriteLine(" ====>>>> Workbook - called Unprotect with password {0} - current IsProtected: {1}",
(UnprotectPassword == null ? ("null") : String.IsNullOrEmpty(UnprotectPassword) ? "empty" : UnprotectPassword),
wBook.Settings.IsProtected );
}
catch (Exception ex)
{
TestContext.WriteLine(" ====>>>> Workbook - Unprotect failed with password {0}",
(UnprotectPassword == null ? ("null") : String.IsNullOrEmpty(UnprotectPassword) ? "empty" : UnprotectPassword) );
Assert.That(((Aspose.Cells.CellsException)ex).Code, Is.EqualTo(Aspose.Cells.ExceptionType.IncorrectPassword));
}
}
// This succeeds on a protected Workbook
Aspose.Cells.Worksheet addedSheet = wBook.Worksheets.Add("NUnut Added Sheet");
addedSheet.Cells["A1"].Value = "Nunit Added Sheet";
The above code of adding the sheet works on the protected XLSX file; my output PDF had the extra page at the end.
It successfully adds the new worksheet both when I don’t call Unprotect and when I do call Unprotect with an invalid password. When calling with an invalid password, I do get an InvalidPassword exception as expected but if I handle it and move on, it seems like other actions/methods on the workbook are not listening to the protection mode.
Thanks,
Sheri