summaryrefslogtreecommitdiff
path: root/styles.go
diff options
context:
space:
mode:
authorRi Xu <xuri.me@gmail.com>2018-03-07 12:56:18 +0800
committerRi Xu <xuri.me@gmail.com>2018-03-07 12:56:18 +0800
commitecc3adf22ab825b69c2aca74018247fbb4ed8cfd (patch)
treed80cc8a4019fe5c717754b3f13f2b9923562e4d8 /styles.go
parent06e54bf1c6658f7c78b43c5d1d37b0cc8e2d9e64 (diff)
- Add protection properties associated with the cell support, relate issue #191;
- godoc and go test has been updated
Diffstat (limited to 'styles.go')
-rw-r--r--styles.go28
1 files changed, 26 insertions, 2 deletions
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)