@@ -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.
21602172func 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+ //
22892313func (f * File ) SetCellStyle (sheet , hcell , vcell string , styleID int ) {
22902314 hcell = strings .ToUpper (hcell )
22912315 vcell = strings .ToUpper (vcell )
0 commit comments