Jelajahi Sumber

New function `UnprotectSheet()` has been added

q523591 7 tahun lalu
induk
melakukan
51857a217d
2 mengubah file dengan 20 tambahan dan 2 penghapusan
  1. 13 1
      excelize_test.go
  2. 7 1
      sheet.go

+ 13 - 1
excelize_test.go

@@ -128,7 +128,7 @@ func TestOpenFile(t *testing.T) {
 	}
 	err = xlsx.Save()
 	if err != nil {
-		t.Log(err)
+		t.Error(err)
 	}
 	// Test write file to not exist directory.
 	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 {
 	for {
 		if len(s) > 0 && s[len(s)-1] == "" {

+ 7 - 1
sheet.go

@@ -713,7 +713,7 @@ func (f *File) SearchSheet(sheet, value string) []string {
 
 // ProtectSheet provides a function to prevent other users from accidentally
 // 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{
 //        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
 // name.
 func trimSheetName(name string) string {