forked from qax-os/excelize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrows.go
150 lines (144 loc) · 3.86 KB
/
rows.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package excelize
import (
"encoding/xml"
"strconv"
"strings"
)
// GetRows return all the rows in a sheet by given "sheet" + index. For now you
// should use sheet_name like "sheet3" where "sheet" is a constant part and "3"
// is a sheet number. For example, if sheet named as "SomeUniqueData" and it is
// second if spreadsheet program interface - you should use "sheet2" here. For
// example:
//
// index := xlsx.GetSheetIndex("Sheet2")
// rows := xlsx.GetRows("sheet" + strconv.Itoa(index))
// for _, row := range rows {
// for _, colCell := range row {
// fmt.Print(colCell, "\t")
// }
// fmt.Println()
// }
//
func (f *File) GetRows(sheet string) [][]string {
xlsx := f.workSheetReader(sheet)
rows := [][]string{}
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
if xlsx != nil {
output, _ := xml.Marshal(f.Sheet[name])
f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
}
decoder := xml.NewDecoder(strings.NewReader(f.readXML(name)))
d, _ := readXMLSST(f)
var inElement string
var r xlsxRow
var row []string
tr, tc := f.getTotalRowsCols(sheet)
for i := 0; i < tr; i++ {
row = []string{}
for j := 0; j <= tc; j++ {
row = append(row, "")
}
rows = append(rows, row)
}
decoder = xml.NewDecoder(strings.NewReader(f.readXML(name)))
for {
token, _ := decoder.Token()
if token == nil {
break
}
switch startElement := token.(type) {
case xml.StartElement:
inElement = startElement.Name.Local
if inElement == "row" {
r = xlsxRow{}
decoder.DecodeElement(&r, &startElement)
cr := r.R - 1
for _, colCell := range r.C {
c := titleToNumber(strings.Map(letterOnlyMapF, colCell.R))
val, _ := colCell.getValueFrom(f, d)
rows[cr][c] = val
}
}
default:
}
}
return rows
}
// getTotalRowsCols provides a function to get total columns and rows in a
// sheet.
func (f *File) getTotalRowsCols(sheet string) (int, int) {
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
decoder := xml.NewDecoder(strings.NewReader(f.readXML(name)))
var inElement string
var r xlsxRow
var tr, tc int
for {
token, _ := decoder.Token()
if token == nil {
break
}
switch startElement := token.(type) {
case xml.StartElement:
inElement = startElement.Name.Local
if inElement == "row" {
r = xlsxRow{}
decoder.DecodeElement(&r, &startElement)
tr = r.R
for _, colCell := range r.C {
col := titleToNumber(strings.Map(letterOnlyMapF, colCell.R))
if col > tc {
tc = col
}
}
}
default:
}
}
return tr, tc
}
// SetRowHeight provides a function to set the height of a single row.
// For example:
//
// xlsx := excelize.CreateFile()
// xlsx.SetRowHeight("Sheet1", 0, 50)
// err := xlsx.Save()
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
//
func (f *File) SetRowHeight(sheet string, rowIndex int, height float64) {
xlsx := f.workSheetReader(sheet)
rows := rowIndex + 1
cells := 0
completeRow(xlsx, rows, cells)
xlsx.SheetData.Row[rowIndex].Ht = strconv.FormatFloat(height, 'f', -1, 64)
xlsx.SheetData.Row[rowIndex].CustomHeight = true
}
// readXMLSST read xmlSST simple function.
func readXMLSST(f *File) (*xlsxSST, error) {
shardStrings := xlsxSST{}
err := xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
return &shardStrings, err
}
// getValueFrom return a value from a column/row cell, this function is inteded
// to be used with for range on rows an argument with the xlsx opened file.
func (xlsx *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error) {
switch xlsx.T {
case "s":
xlsxSI := 0
xlsxSI, _ = strconv.Atoi(xlsx.V)
if len(d.SI[xlsxSI].R) > 0 {
value := ""
for _, v := range d.SI[xlsxSI].R {
value += v.T
}
return value, nil
}
return d.SI[xlsxSI].T, nil
case "str":
return xlsx.V, nil
default:
return xlsx.V, nil
}
}