Skip to content

Commit ecc3adf

Browse files
committed
- Add protection properties associated with the cell support, relate issue qax-os#191;
- godoc and go test has been updated
1 parent 06e54bf commit ecc3adf

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

excelize_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,23 @@ func TestSetCellStyleFont(t *testing.T) {
607607
}
608608
}
609609

610+
func TestSetCellStyleProtection(t *testing.T) {
611+
xlsx, err := OpenFile("./test/Book2.xlsx")
612+
if err != nil {
613+
t.Log(err)
614+
}
615+
var style int
616+
style, err = xlsx.NewStyle(`{"protection":{"hidden":true, "locked":true}}`)
617+
if err != nil {
618+
t.Log(err)
619+
}
620+
xlsx.SetCellStyle("Sheet2", "A6", "A6", style)
621+
err = xlsx.Save()
622+
if err != nil {
623+
t.Log(err)
624+
}
625+
}
626+
610627
func TestSetDeleteSheet(t *testing.T) {
611628
xlsx, err := OpenFile("./test/Book3.xlsx")
612629
if err != nil {

styles.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,8 @@ func (f *File) NewStyle(style string) (int, error) {
19011901
fillID = s.Fills.Count - 1
19021902

19031903
applyAlignment, alignment := fs.Alignment != nil, setAlignment(fs)
1904-
cellXfsID = setCellXfs(s, fontID, numFmtID, fillID, borderID, applyAlignment, alignment)
1904+
applyProtection, protection := fs.Protection != nil, setProtection(fs)
1905+
cellXfsID = setCellXfs(s, fontID, numFmtID, fillID, borderID, applyAlignment, applyProtection, alignment, protection)
19051906
return cellXfsID, nil
19061907
}
19071908

@@ -2155,6 +2156,17 @@ func setAlignment(formatStyle *formatStyle) *xlsxAlignment {
21552156
return &alignment
21562157
}
21572158

2159+
// setProtection provides function to set protection properties associated
2160+
// with the cell.
2161+
func setProtection(formatStyle *formatStyle) *xlsxProtection {
2162+
var protection xlsxProtection
2163+
if formatStyle.Protection != nil {
2164+
protection.Hidden = formatStyle.Protection.Hidden
2165+
protection.Locked = formatStyle.Protection.Locked
2166+
}
2167+
return &protection
2168+
}
2169+
21582170
// setBorders provides function to add border elements in the styles.xml by
21592171
// given borders format settings.
21602172
func setBorders(formatStyle *formatStyle) *xlsxBorder {
@@ -2209,7 +2221,7 @@ func setBorders(formatStyle *formatStyle) *xlsxBorder {
22092221

22102222
// setCellXfs provides function to set describes all of the formatting for a
22112223
// cell.
2212-
func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, applyAlignment bool, alignment *xlsxAlignment) int {
2224+
func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, applyAlignment, applyProtection bool, alignment *xlsxAlignment, protection *xlsxProtection) int {
22132225
var xf xlsxXf
22142226
xf.FontID = fontID
22152227
if fontID != 0 {
@@ -2224,6 +2236,10 @@ func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, a
22242236
style.CellXfs.Count++
22252237
xf.Alignment = alignment
22262238
xf.ApplyAlignment = applyAlignment
2239+
if applyProtection {
2240+
xf.ApplyProtection = applyProtection
2241+
xf.Protection = protection
2242+
}
22272243
xfID := 0
22282244
xf.XfID = &xfID
22292245
style.CellXfs.Xf = append(style.CellXfs.Xf, xf)
@@ -2286,6 +2302,14 @@ func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, a
22862302
// }
22872303
// xlsx.SetCellStyle("Sheet1", "H9", "H9", style)
22882304
//
2305+
// Hide and lock for cell H9 on Sheet1:
2306+
//
2307+
// style, err := xlsx.NewStyle(`{"protection":{"hidden":true, "locked":true}`)
2308+
// if err != nil {
2309+
// fmt.Println(err)
2310+
// }
2311+
// xlsx.SetCellStyle("Sheet1", "H9", "H9", style)
2312+
//
22892313
func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) {
22902314
hcell = strings.ToUpper(hcell)
22912315
vcell = strings.ToUpper(vcell)

xmlStyles.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ type formatStyle struct {
344344
Vertical string `json:"vertical"`
345345
WrapText bool `json:"wrap_text"`
346346
} `json:"alignment"`
347+
Protection *struct {
348+
Hidden bool `json:"hidden"`
349+
Locked bool `json:"locked"`
350+
} `json:"protection"`
347351
NumFmt int `json:"number_format"`
348352
DecimalPlaces int `json:"decimal_places"`
349353
CustomNumFmt *string `json:"custom_number_format"`

0 commit comments

Comments
 (0)