- Add protection properties associated with the cell support, relate issue #191;

- godoc and go test has been updated
This commit is contained in:
Ri Xu 2018-03-07 12:56:18 +08:00
parent 06e54bf1c6
commit ecc3adf22a
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 47 additions and 2 deletions

View File

@ -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 {

View File

@ -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)

View File

@ -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"`