forked from Luna-CY/go-office
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.go
85 lines (65 loc) · 1.37 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
package cell
import (
"bytes"
"fmt"
)
const (
STCellTypeNumber = "n"
STCellTypeInlineStr = "inlineStr"
STCellTypeSharedString = "s"
)
func newBaseCell(row uint64, col uint64) *Cell {
c := new(Cell)
c.x = row
c.y = col
c.Reference = fmt.Sprintf("%v%v", ten2col(col), row)
return c
}
func NewCellWithTextForInline(row uint64, col uint64, text string) *Cell {
c := newBaseCell(row, col)
c.Type = STCellTypeInlineStr
c.IS = &inlineStr{T: text}
return c
}
func NewCellWithText(row uint64, col uint64, text string) *Cell {
c := newBaseCell(row, col)
c.Type = STCellTypeSharedString
c.Value = &text
return c
}
func NewCellWithNumber(row uint64, col uint64, number interface{}) *Cell {
c := newBaseCell(row, col)
c.Type = STCellTypeNumber
v := fmt.Sprintf("%v", number)
c.Value = &v
return c
}
// Cell 单元格结构定义
type Cell struct {
Reference string `xml:"r,attr"`
Style *uint `xml:"s,attr"`
Type string `xml:"t,attr"`
x uint64
y uint64
Formula *Formula
Value *string `xml:"v"`
IS *inlineStr `xml:"is"`
}
func ten2col(col uint64) string {
builder := new(bytes.Buffer)
for col > 0 {
m := col % 26
if 0 == m {
m = 26
}
builder.WriteByte(byte(m + 64))
col = (col - m) / 26
}
bs := builder.Bytes()
for i, j := 0, builder.Len()-1; i < j; {
bs[j], bs[i] = bs[i], bs[j]
i++
j--
}
return string(bs)
}