This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.go
108 lines (89 loc) · 2.5 KB
/
cell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package xcel
import (
"encoding/json"
"fmt"
"time"
"github.com/360EntSecGroup-Skylar/excelize"
)
// Cell represents a cell in a row
type Cell struct {
ID int
Row *Row
}
// GetFile gets the file that the cell belongs to
func (c *Cell) GetFile() *File {
return c.Row.Sheet.File
}
// GetSheet gets the sheet that the cell belongs to
func (c *Cell) GetSheet() *Sheet {
return c.Row.Sheet
}
// GetRow gets the row that the cell belongs to
func (c *Cell) GetRow() *Row {
return c.Row
}
// GetAxis gets the axis (ColRow)
func (c *Cell) GetAxis() string {
col := excelize.ToAlphaString(c.ID)
return fmt.Sprintf("%s%d", col, c.Row.ID)
}
// SetString sets a string value in the cell
// returns the modified cell
func (c *Cell) SetString(val string) *Cell {
c.GetFile().SetCellStr(c.GetSheet().Name, c.GetAxis(), val)
return c
}
// SetInt sets an int value in the cell
// returns the modified cell
func (c *Cell) SetInt(val int) *Cell {
c.GetFile().SetCellInt(c.GetSheet().Name, c.GetAxis(), val)
return c
}
// SetBool sets a bool value in the cell
// returns the modified cell
func (c *Cell) SetBool(val bool) *Cell {
c.GetFile().SetCellBool(c.GetSheet().Name, c.GetAxis(), val)
return c
}
// SetFormula sets a formula in the cell
// returns the modified cell
func (c *Cell) SetFormula(val string) *Cell {
c.GetFile().SetCellFormula(c.GetSheet().Name, c.GetAxis(), val)
return c
}
// SetFloat sets a float in the cell
// returns the modified cell
func (c *Cell) SetFloat(val float64) *Cell {
c.GetFile().SetCellValue(c.GetSheet().Name, c.GetAxis(), val)
return c
}
func (c *Cell) SetFloatWithFormat(val float64, format string) *Cell {
c.GetFile().SetCellValue(c.GetSheet().Name, c.GetAxis(), val)
return c
}
func (c *Cell) SetDate(val time.Time) *Cell {
c.GetFile().SetCellValue(c.GetSheet().Name, c.GetAxis(), val)
return c
}
func (c *Cell) Merge(hcell, vcell int) *Cell {
col := excelize.ToAlphaString(c.ID + hcell)
c.GetFile().MergeCell(c.GetSheet().Name, c.GetAxis(), fmt.Sprintf("%s%d", col, c.Row.ID+vcell))
return c
}
// SetStyle applies a style to the cell
func (c *Cell) SetStyle(styleID int) *Cell {
c.GetFile().SetCellStyle(c.GetSheet().Name, c.GetAxis(), c.GetAxis(), styleID)
return c
}
// AddPicture adds a picture to the cell
func (c *Cell) AddPicture(picture string, format *ImageFormat) error {
var sf string
if format != nil {
bts, err := json.Marshal(format)
if err != nil {
return err
}
sf = string(bts)
}
return c.GetFile().AddPicture(c.GetSheet().Name, c.GetAxis(), picture, sf)
}