Merge pull request #287 from q523591/unprotect-sheet

New function `UnprotectSheet()` has been added
This commit is contained in:
xuri 2018-11-04 23:28:39 +08:00 committed by GitHub
commit 2aca340f5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

14
excelize_test.go Executable file → Normal file
View File

@ -128,7 +128,7 @@ func TestOpenFile(t *testing.T) {
} }
err = xlsx.Save() err = xlsx.Save()
if err != nil { if err != nil {
t.Log(err) t.Error(err)
} }
// Test write file to not exist directory. // Test write file to not exist directory.
err = xlsx.SaveAs("") err = xlsx.SaveAs("")
@ -1221,6 +1221,18 @@ func TestProtectSheet(t *testing.T) {
} }
} }
func TestUnprotectSheet(t *testing.T) {
xlsx, err := OpenFile("./test/Book1.xlsx")
if err != nil {
t.Error(err)
}
xlsx.UnprotectSheet("Sheet1")
err = xlsx.Save()
if err != nil {
t.Error(err)
}
}
func trimSliceSpace(s []string) []string { func trimSliceSpace(s []string) []string {
for { for {
if len(s) > 0 && s[len(s)-1] == "" { if len(s) > 0 && s[len(s)-1] == "" {

View File

@ -713,7 +713,7 @@ func (f *File) SearchSheet(sheet, value string) []string {
// ProtectSheet provides a function to prevent other users from accidentally // ProtectSheet provides a function to prevent other users from accidentally
// or deliberately changing, moving, or deleting data in a worksheet. For // or deliberately changing, moving, or deleting data in a worksheet. For
// example protect Sheet1 with protection settings: // example, protect Sheet1 with protection settings:
// //
// xlsx.ProtectSheet("Sheet1", &excelize.FormatSheetProtection{ // xlsx.ProtectSheet("Sheet1", &excelize.FormatSheetProtection{
// Password: "password", // Password: "password",
@ -752,6 +752,12 @@ func (f *File) ProtectSheet(sheet string, settings *FormatSheetProtection) {
} }
} }
// UnprotectSheet provides a function to unprotect an Excel worksheet.
func (f *File) UnprotectSheet(sheet string) {
xlsx := f.workSheetReader(sheet)
xlsx.SheetProtection = nil
}
// trimSheetName provides a function to trim invaild characters by given worksheet // trimSheetName provides a function to trim invaild characters by given worksheet
// name. // name.
func trimSheetName(name string) string { func trimSheetName(name string) string {