Skip to content

Commit de550cc

Browse files
authored
Add new function: unprotect_sheet (#24)
1 parent 8c6a440 commit de550cc

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

excelize.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3952,6 +3952,30 @@ def unmerge_cell(
39523952
if err != "":
39533953
raise RuntimeError(err)
39543954

3955+
def unprotect_sheet(self, sheet: str, *password: str) -> None:
3956+
"""
3957+
Remove protection for a sheet, specified the second optional password
3958+
parameter to remove sheet protection with password verification.
3959+
3960+
Args:
3961+
sheet (str): The worksheet name
3962+
*password (str): Optional password for sheet protection verification
3963+
3964+
Returns:
3965+
None: Return None if no error occurred, otherwise raise a
3966+
RuntimeError with the message.
3967+
"""
3968+
lib.UnprotectSheet.restype = c_char_p
3969+
passwd = password[0] if len(password) > 0 else ""
3970+
err = lib.UnprotectSheet(
3971+
self.file_index,
3972+
sheet.encode(ENCODE),
3973+
passwd.encode(ENCODE),
3974+
len(password) > 0,
3975+
).decode(ENCODE)
3976+
if err != "":
3977+
raise RuntimeError(err)
3978+
39553979
def update_linked_value(self) -> None:
39563980
"""
39573981
Fix linked values within a spreadsheet are not updating in Office Excel

main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,6 +2747,28 @@ func UnmergeCell(idx int, sheet, topLeftCell, bottomRightCell *C.char) *C.char {
27472747
return C.CString(emptyString)
27482748
}
27492749

2750+
// UnprotectSheet provides a function to remove protection for a sheet,
2751+
// specified the second optional password parameter to remove sheet
2752+
// protection with password verification.
2753+
//
2754+
//export UnprotectSheet
2755+
func UnprotectSheet(idx int, sheet, password *C.char, verify bool) *C.char {
2756+
f, ok := files.Load(idx)
2757+
if !ok {
2758+
return C.CString(errFilePtr)
2759+
}
2760+
if verify {
2761+
if err := f.(*excelize.File).UnprotectSheet(C.GoString(sheet), C.GoString(password)); err != nil {
2762+
return C.CString(err.Error())
2763+
}
2764+
return C.CString(emptyString)
2765+
}
2766+
if err := f.(*excelize.File).UnprotectSheet(C.GoString(sheet)); err != nil {
2767+
return C.CString(err.Error())
2768+
}
2769+
return C.CString(emptyString)
2770+
}
2771+
27502772
// UpdateLinkedValue fix linked values within a spreadsheet are not updating in
27512773
// Office Excel application. This function will be remove value tag when met a
27522774
// cell have a linked value.

test_excelize.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,16 @@ def test_style(self):
450450
with self.assertRaises(RuntimeError) as context:
451451
f.protect_sheet("SheetN", excelize.SheetProtectionOptions())
452452
self.assertEqual(str(context.exception), "sheet SheetN does not exist")
453+
454+
with self.assertRaises(RuntimeError) as context:
455+
f.unprotect_sheet("SheetN")
456+
self.assertEqual(str(context.exception), "sheet SheetN does not exist")
457+
with self.assertRaises(RuntimeError) as context:
458+
f.unprotect_sheet("Sheet1", "")
459+
self.assertEqual(str(context.exception), "password length invalid")
460+
self.assertIsNone(f.unprotect_sheet("Sheet1", "password"))
461+
self.assertIsNone(f.unprotect_sheet("Sheet1"))
462+
453463
self.assertIsNone(
454464
f.protect_workbook(
455465
excelize.WorkbookProtectionOptions(

0 commit comments

Comments
 (0)