From 4dbc78ce0a0d40be189b4c77207cdbb598846c73 Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Fri, 2 Nov 2018 23:08:31 +0800 Subject: resolve #273 new feature: protect sheet support new feature: protect sheet support, relate issue #273 --- sheet.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'sheet.go') 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 { -- cgit v1.2.1