summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--excelize_test.go17
-rw-r--r--styles.go28
-rw-r--r--xmlStyles.go4
3 files changed, 47 insertions, 2 deletions
diff --git a/excelize_test.go b/excelize_test.go
index a89369a..41a2d7f 100644
--- a/excelize_test.go
+++ b/excelize_test.go
@@ -607,6 +607,23 @@ func TestSetCellStyleFont(t *testing.T) {
}
}
+func TestSetCellStyleProtection(t *testing.T) {
+ xlsx, err := OpenFile("./test/Book2.xlsx")
+ if err != nil {
+ t.Log(err)
+ }
+ var style int
+ style, err = xlsx.NewStyle(`{"protection":{"hidden":true, "locked":true}}`)
+ if err != nil {
+ t.Log(err)
+ }
+ xlsx.SetCellStyle("Sheet2", "A6", "A6", style)
+ err = xlsx.Save()
+ if err != nil {
+ t.Log(err)
+ }
+}
+
func TestSetDeleteSheet(t *testing.T) {
xlsx, err := OpenFile("./test/Book3.xlsx")
if err != nil {
diff --git a/styles.go b/styles.go
index 98fc280..d1ef726 100644
--- a/styles.go
+++ b/styles.go
@@ -1901,7 +1901,8 @@ func (f *File) NewStyle(style string) (int, error) {
fillID = s.Fills.Count - 1
applyAlignment, alignment := fs.Alignment != nil, setAlignment(fs)
- cellXfsID = setCellXfs(s, fontID, numFmtID, fillID, borderID, applyAlignment, alignment)
+ applyProtection, protection := fs.Protection != nil, setProtection(fs)
+ cellXfsID = setCellXfs(s, fontID, numFmtID, fillID, borderID, applyAlignment, applyProtection, alignment, protection)
return cellXfsID, nil
}
@@ -2155,6 +2156,17 @@ func setAlignment(formatStyle *formatStyle) *xlsxAlignment {
return &alignment
}
+// setProtection provides function to set protection properties associated
+// with the cell.
+func setProtection(formatStyle *formatStyle) *xlsxProtection {
+ var protection xlsxProtection
+ if formatStyle.Protection != nil {
+ protection.Hidden = formatStyle.Protection.Hidden
+ protection.Locked = formatStyle.Protection.Locked
+ }
+ return &protection
+}
+
// setBorders provides function to add border elements in the styles.xml by
// given borders format settings.
func setBorders(formatStyle *formatStyle) *xlsxBorder {
@@ -2209,7 +2221,7 @@ func setBorders(formatStyle *formatStyle) *xlsxBorder {
// setCellXfs provides function to set describes all of the formatting for a
// cell.
-func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, applyAlignment bool, alignment *xlsxAlignment) int {
+func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, applyAlignment, applyProtection bool, alignment *xlsxAlignment, protection *xlsxProtection) int {
var xf xlsxXf
xf.FontID = fontID
if fontID != 0 {
@@ -2224,6 +2236,10 @@ func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, a
style.CellXfs.Count++
xf.Alignment = alignment
xf.ApplyAlignment = applyAlignment
+ if applyProtection {
+ xf.ApplyProtection = applyProtection
+ xf.Protection = protection
+ }
xfID := 0
xf.XfID = &xfID
style.CellXfs.Xf = append(style.CellXfs.Xf, xf)
@@ -2286,6 +2302,14 @@ func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, a
// }
// xlsx.SetCellStyle("Sheet1", "H9", "H9", style)
//
+// Hide and lock for cell H9 on Sheet1:
+//
+// style, err := xlsx.NewStyle(`{"protection":{"hidden":true, "locked":true}`)
+// if err != nil {
+// fmt.Println(err)
+// }
+// xlsx.SetCellStyle("Sheet1", "H9", "H9", style)
+//
func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) {
hcell = strings.ToUpper(hcell)
vcell = strings.ToUpper(vcell)
diff --git a/xmlStyles.go b/xmlStyles.go
index 8477804..05ff22b 100644
--- a/xmlStyles.go
+++ b/xmlStyles.go
@@ -344,6 +344,10 @@ type formatStyle struct {
Vertical string `json:"vertical"`
WrapText bool `json:"wrap_text"`
} `json:"alignment"`
+ Protection *struct {
+ Hidden bool `json:"hidden"`
+ Locked bool `json:"locked"`
+ } `json:"protection"`
NumFmt int `json:"number_format"`
DecimalPlaces int `json:"decimal_places"`
CustomNumFmt *string `json:"custom_number_format"`