summaryrefslogtreecommitdiff
path: root/sheet.go
diff options
context:
space:
mode:
authorHcySunYang <HcySunYang@outlook.com>2018-11-02 23:08:31 +0800
committerxuri <xuri.me@gmail.com>2018-11-02 23:08:31 +0800
commit4dbc78ce0a0d40be189b4c77207cdbb598846c73 (patch)
treeb8295d120ecafa5c7e9e7aad22be09e65fc76aed /sheet.go
parent30122d0346dc751b01c9865af3febb1f99e1f5e0 (diff)
resolve #273 new feature: protect sheet support
new feature: protect sheet support, relate issue #273
Diffstat (limited to 'sheet.go')
-rw-r--r--sheet.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/sheet.go b/sheet.go
index 522d112..8ddb8c9 100644
--- a/sheet.go
+++ b/sheet.go
@@ -711,6 +711,47 @@ func (f *File) SearchSheet(sheet, value string) []string {
return result
}
+// 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:
+//
+// xlsx.ProtectSheet("Sheet1", &excelize.FormatSheetProtection{
+// Password: "password",
+// EditScenarios: false,
+// })
+//
+func (f *File) ProtectSheet(sheet string, settings *FormatSheetProtection) {
+ xlsx := f.workSheetReader(sheet)
+ if settings == nil {
+ settings = &FormatSheetProtection{
+ EditObjects: true,
+ EditScenarios: true,
+ SelectLockedCells: true,
+ }
+ }
+ xlsx.SheetProtection = &xlsxSheetProtection{
+ AutoFilter: settings.AutoFilter,
+ DeleteColumns: settings.DeleteColumns,
+ DeleteRows: settings.DeleteRows,
+ FormatCells: settings.FormatCells,
+ FormatColumns: settings.FormatColumns,
+ FormatRows: settings.FormatRows,
+ InsertColumns: settings.InsertColumns,
+ InsertHyperlinks: settings.InsertHyperlinks,
+ InsertRows: settings.InsertRows,
+ Objects: settings.EditObjects,
+ PivotTables: settings.PivotTables,
+ Scenarios: settings.EditScenarios,
+ SelectLockedCells: settings.SelectLockedCells,
+ SelectUnlockedCells: settings.SelectUnlockedCells,
+ Sheet: true,
+ Sort: settings.Sort,
+ }
+ if settings.Password != "" {
+ xlsx.SheetProtection.Password = genSheetPasswd(settings.Password)
+ }
+}
+
// trimSheetName provides a function to trim invaild characters by given worksheet
// name.
func trimSheetName(name string) string {