forked from p30928647/excelize
- Add protection properties associated with the cell support, relate issue #191;
- godoc and go test has been updated
This commit is contained in:
parent
06e54bf1c6
commit
ecc3adf22a
|
@ -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) {
|
func TestSetDeleteSheet(t *testing.T) {
|
||||||
xlsx, err := OpenFile("./test/Book3.xlsx")
|
xlsx, err := OpenFile("./test/Book3.xlsx")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
28
styles.go
28
styles.go
|
@ -1901,7 +1901,8 @@ func (f *File) NewStyle(style string) (int, error) {
|
||||||
fillID = s.Fills.Count - 1
|
fillID = s.Fills.Count - 1
|
||||||
|
|
||||||
applyAlignment, alignment := fs.Alignment != nil, setAlignment(fs)
|
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
|
return cellXfsID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2155,6 +2156,17 @@ func setAlignment(formatStyle *formatStyle) *xlsxAlignment {
|
||||||
return &alignment
|
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
|
// setBorders provides function to add border elements in the styles.xml by
|
||||||
// given borders format settings.
|
// given borders format settings.
|
||||||
func setBorders(formatStyle *formatStyle) *xlsxBorder {
|
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
|
// setCellXfs provides function to set describes all of the formatting for a
|
||||||
// cell.
|
// 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
|
var xf xlsxXf
|
||||||
xf.FontID = fontID
|
xf.FontID = fontID
|
||||||
if fontID != 0 {
|
if fontID != 0 {
|
||||||
|
@ -2224,6 +2236,10 @@ func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, a
|
||||||
style.CellXfs.Count++
|
style.CellXfs.Count++
|
||||||
xf.Alignment = alignment
|
xf.Alignment = alignment
|
||||||
xf.ApplyAlignment = applyAlignment
|
xf.ApplyAlignment = applyAlignment
|
||||||
|
if applyProtection {
|
||||||
|
xf.ApplyProtection = applyProtection
|
||||||
|
xf.Protection = protection
|
||||||
|
}
|
||||||
xfID := 0
|
xfID := 0
|
||||||
xf.XfID = &xfID
|
xf.XfID = &xfID
|
||||||
style.CellXfs.Xf = append(style.CellXfs.Xf, xf)
|
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)
|
// 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) {
|
func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) {
|
||||||
hcell = strings.ToUpper(hcell)
|
hcell = strings.ToUpper(hcell)
|
||||||
vcell = strings.ToUpper(vcell)
|
vcell = strings.ToUpper(vcell)
|
||||||
|
|
|
@ -344,6 +344,10 @@ type formatStyle struct {
|
||||||
Vertical string `json:"vertical"`
|
Vertical string `json:"vertical"`
|
||||||
WrapText bool `json:"wrap_text"`
|
WrapText bool `json:"wrap_text"`
|
||||||
} `json:"alignment"`
|
} `json:"alignment"`
|
||||||
|
Protection *struct {
|
||||||
|
Hidden bool `json:"hidden"`
|
||||||
|
Locked bool `json:"locked"`
|
||||||
|
} `json:"protection"`
|
||||||
NumFmt int `json:"number_format"`
|
NumFmt int `json:"number_format"`
|
||||||
DecimalPlaces int `json:"decimal_places"`
|
DecimalPlaces int `json:"decimal_places"`
|
||||||
CustomNumFmt *string `json:"custom_number_format"`
|
CustomNumFmt *string `json:"custom_number_format"`
|
||||||
|
|
Loading…
Reference in New Issue