Skip to content

Commit 8fc6e6f

Browse files
author
Thomas Charbonnel
committed
1 parent ebea684 commit 8fc6e6f

File tree

4 files changed

+112
-58
lines changed

4 files changed

+112
-58
lines changed

cell_test.go

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,11 @@ func TestGetCellValue(t *testing.T) {
224224
f.checked = nil
225225
cells := []string{"A3", "A4", "B4", "A7", "B7"}
226226
rows, err := f.GetRows("Sheet1")
227-
assert.Equal(t, [][]string{nil, nil, {"A3"}, {"A4", "B4"}, nil, nil, {"A7", "B7"}, {"A8", "B8"}}, rows)
227+
assert.Equal(t, [][]Cell{nil, nil, {Cell{Value: "A3"}}, {Cell{Value: "A4"}, Cell{Value: "B4"}}, nil, nil, {Cell{Value: "A7"}, Cell{Value: "B7"}}, {Cell{Value: "A8"}, Cell{Value: "B8"}}}, rows)
228228
assert.NoError(t, err)
229229
for _, cell := range cells {
230230
value, err := f.GetCellValue("Sheet1", cell)
231-
assert.Equal(t, cell, value)
231+
assert.Equal(t, cell, value)
232232
assert.NoError(t, err)
233233
}
234234
cols, err := f.GetCols("Sheet1")
@@ -239,44 +239,44 @@ func TestGetCellValue(t *testing.T) {
239239
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(fmt.Sprintf(sheetData, `<row r="2"><c r="A2" t="str"><v>A2</v></c></row><row r="2"><c r="B2" t="str"><v>B2</v></c></row>`)))
240240
f.checked = nil
241241
cell, err := f.GetCellValue("Sheet1", "A2")
242-
assert.Equal(t, "A2", cell)
242+
assert.Equal(t, "A2", cell)
243243
assert.NoError(t, err)
244244

245245
f.Sheet.Delete("xl/worksheets/sheet1.xml")
246246
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(fmt.Sprintf(sheetData, `<row r="2"><c r="A2" t="str"><v>A2</v></c></row><row r="2"><c r="B2" t="str"><v>B2</v></c></row>`)))
247247
f.checked = nil
248248
rows, err = f.GetRows("Sheet1")
249-
assert.Equal(t, [][]string{nil, {"A2", "B2"}}, rows)
249+
assert.Equal(t, [][]Cell{nil, {Cell{Value: "A2"}, Cell{Value: "B2"}}}, rows)
250250
assert.NoError(t, err)
251251

252252
f.Sheet.Delete("xl/worksheets/sheet1.xml")
253253
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(fmt.Sprintf(sheetData, `<row r="1"><c r="A1" t="str"><v>A1</v></c></row><row r="1"><c r="B1" t="str"><v>B1</v></c></row>`)))
254254
f.checked = nil
255255
rows, err = f.GetRows("Sheet1")
256-
assert.Equal(t, [][]string{{"A1", "B1"}}, rows)
256+
assert.Equal(t, [][]Cell{{Cell{Value: "A1"}, Cell{Value: "B1"}}}, rows)
257257
assert.NoError(t, err)
258258

259259
f.Sheet.Delete("xl/worksheets/sheet1.xml")
260260
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(fmt.Sprintf(sheetData, `<row><c t="str"><v>A3</v></c></row><row><c t="str"><v>A4</v></c><c t="str"><v>B4</v></c></row><row r="7"><c t="str"><v>A7</v></c><c t="str"><v>B7</v></c></row><row><c t="str"><v>A8</v></c><c t="str"><v>B8</v></c></row>`)))
261261
f.checked = nil
262262
rows, err = f.GetRows("Sheet1")
263-
assert.Equal(t, [][]string{{"A3"}, {"A4", "B4"}, nil, nil, nil, nil, {"A7", "B7"}, {"A8", "B8"}}, rows)
263+
assert.Equal(t, [][]Cell{{Cell{Value: "A3"}}, {Cell{Value: "A4"}, Cell{Value: "B4"}}, nil, nil, nil, nil, {Cell{Value: "A7"}, Cell{Value: "B7"}}, {Cell{Value: "A8"}, Cell{Value: "B8"}}}, rows)
264264
assert.NoError(t, err)
265265

266266
f.Sheet.Delete("xl/worksheets/sheet1.xml")
267267
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(fmt.Sprintf(sheetData, `<row r="0"><c r="H6" t="str"><v>H6</v></c><c r="A1" t="str"><v>r0A6</v></c><c r="F4" t="str"><v>F4</v></c></row><row><c r="A1" t="str"><v>A6</v></c><c r="B1" t="str"><v>B6</v></c><c r="C1" t="str"><v>C6</v></c></row><row r="3"><c r="A3"><v>100</v></c><c r="B3" t="str"><v>B3</v></c></row>`)))
268268
f.checked = nil
269269
cell, err = f.GetCellValue("Sheet1", "H6")
270-
assert.Equal(t, "H6", cell)
270+
assert.Equal(t, "H6", cell)
271271
assert.NoError(t, err)
272272
rows, err = f.GetRows("Sheet1")
273-
assert.Equal(t, [][]string{
274-
{"A6", "B6", "C6"},
273+
assert.Equal(t, [][]Cell{
274+
{Cell{Value: "A6"}, Cell{Value: "B6"}, Cell{Value: "C6"}},
275275
nil,
276-
{"100", "B3"},
277-
{"", "", "", "", "", "F4"},
276+
{Cell{Value: int64(100)}, Cell{Value: "B3"}},
277+
{Cell{}, Cell{}, Cell{}, Cell{}, Cell{}, Cell{Value: "F4"}},
278278
nil,
279-
{"", "", "", "", "", "", "", "H6"},
279+
{Cell{}, Cell{}, Cell{}, Cell{}, Cell{}, Cell{}, Cell{}, Cell{Value: "H6"}},
280280
}, rows)
281281
assert.NoError(t, err)
282282

@@ -314,36 +314,36 @@ func TestGetCellValue(t *testing.T) {
314314
</row>`)))
315315
f.checked = nil
316316
rows, err = f.GetRows("Sheet1")
317-
assert.Equal(t, [][]string{{
318-
"2422.3",
319-
"2422.3",
320-
"12.4",
321-
"964",
322-
"1101.6",
323-
"275.4",
324-
"68.9",
325-
"44385.2083333333",
326-
"5.1",
327-
"5.11",
328-
"5.1",
329-
"5.111",
330-
"5.1111",
331-
"2422.012345678",
332-
"2422.0123456789",
333-
"12.012345678901",
334-
"964",
335-
"1101.6",
336-
"275.4",
337-
"68.9",
338-
"0.08888",
339-
"0.00004",
340-
"2422.3",
341-
"1101.6",
342-
"275.4",
343-
"68.9",
344-
"1.1",
345-
"1234567890123_4",
346-
"123456789_0123_4",
317+
assert.Equal(t, [][]Cell{{
318+
Cell{Value: 2422.3},
319+
Cell{Value: 2422.3},
320+
Cell{Value: 12.4},
321+
Cell{Value: int64(964)},
322+
Cell{Value: 1101.6},
323+
Cell{Value: 275.4},
324+
Cell{Value: 68.9},
325+
Cell{Value: 44385.2083333333},
326+
Cell{Value: 5.1},
327+
Cell{Value: 5.11},
328+
Cell{Value: 5.1},
329+
Cell{Value: 5.111},
330+
Cell{Value: 5.1111},
331+
Cell{Value: 2422.012345678},
332+
Cell{Value: 2422.0123456789},
333+
Cell{Value: 12.012345678901},
334+
Cell{Value: int64(964)},
335+
Cell{Value: 1101.6},
336+
Cell{Value: 275.4},
337+
Cell{Value: 68.9},
338+
Cell{Value: 0.08888},
339+
Cell{Value: 0.00004},
340+
Cell{Value: 2422.3},
341+
Cell{Value: 1101.6},
342+
Cell{Value: 275.4},
343+
Cell{Value: 68.9},
344+
Cell{Value: 1.1},
345+
Cell{Value: "1234567890123_4"},
346+
Cell{Value: "123456789_0123_4"},
347347
}}, rows)
348348
assert.NoError(t, err)
349349
}

excelize_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,12 +1114,12 @@ func TestSharedStrings(t *testing.T) {
11141114
if !assert.NoError(t, err) {
11151115
t.FailNow()
11161116
}
1117-
assert.Equal(t, "A", rows[0][0])
1117+
assert.Equal(t, Cell{Value: "A"}, rows[0][0])
11181118
rows, err = f.GetRows("Sheet2")
11191119
if !assert.NoError(t, err) {
11201120
t.FailNow()
11211121
}
1122-
assert.Equal(t, "Test Weight (Kgs)", rows[0][0])
1122+
assert.Equal(t, Cell{Value: "Test Weight (Kgs)"}, rows[0][0])
11231123
assert.NoError(t, f.Close())
11241124
}
11251125

rows.go

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ import (
4949
// fmt.Println()
5050
// }
5151
//
52-
func (f *File) GetRows(sheet string, opts ...Options) ([][]string, error) {
52+
func (f *File) GetRows(sheet string, opts ...Options) ([][]Cell, error) {
5353
rows, err := f.Rows(sheet)
5454
if err != nil {
5555
return nil, err
5656
}
57-
results, cur, max := make([][]string, 0, 64), 0, 0
57+
results, cur, max := make([][]Cell, 0, 64), 0, 0
5858
for rows.Next() {
5959
cur++
6060
row, err := rows.Columns(opts...)
@@ -128,7 +128,7 @@ func (rows *Rows) Close() error {
128128
// Columns return the current row's column values. This fetches the worksheet
129129
// data as a stream, returns each cell in a row as is, and will not skip empty
130130
// rows in the tail of the worksheet.
131-
func (rows *Rows) Columns(opts ...Options) ([]string, error) {
131+
func (rows *Rows) Columns(opts ...Options) ([]Cell, error) {
132132
if rows.curRow > rows.seekRow {
133133
return nil, nil
134134
}
@@ -171,9 +171,9 @@ func (rows *Rows) Columns(opts ...Options) ([]string, error) {
171171
}
172172

173173
// appendSpace append blank characters to slice by given length and source slice.
174-
func appendSpace(l int, s []string) []string {
174+
func appendSpace(l int, s []Cell) []Cell {
175175
for i := 1; i < l; i++ {
176-
s = append(s, "")
176+
s = append(s, Cell{})
177177
}
178178
return s
179179
}
@@ -192,7 +192,7 @@ type rowXMLIterator struct {
192192
err error
193193
inElement string
194194
cellCol int
195-
columns []string
195+
columns []Cell
196196
}
197197

198198
// rowXMLHandler parse the row XML element of the worksheet.
@@ -206,10 +206,14 @@ func (rows *Rows) rowXMLHandler(rowIterator *rowXMLIterator, xmlElement *xml.Sta
206206
return
207207
}
208208
}
209+
//blank := rowIterator.cellCol - len(rowIterator.columns)
210+
//if val, _ := colCell.getValueFrom(rows.f, rows.sst, raw); val != "" || colCell.F != nil {
211+
// rowIterator.columns = append(appendSpace(blank, rowIterator.columns), val)
212+
//}
209213
blank := rowIterator.cellCol - len(rowIterator.columns)
210-
if val, _ := colCell.getValueFrom(rows.f, rows.sst, raw); val != "" || colCell.F != nil {
211-
rowIterator.columns = append(appendSpace(blank, rowIterator.columns), val)
212-
}
214+
if val, _ := colCell.getTypedValueFrom(rows.f, rows.sst); val != "" || colCell.F != nil {
215+
rowIterator.columns = append(appendSpace(blank, rowIterator.columns), Cell{Value: val, StyleID: colCell.S})
216+
}
213217
}
214218
}
215219

@@ -475,6 +479,59 @@ func (c *xlsxC) getValueFrom(f *File, d *xlsxSST, raw bool) (string, error) {
475479
}
476480
}
477481

482+
func (c *xlsxC) getTypedValueFrom(f *File, d *xlsxSST) (interface{}, error) {
483+
f.Lock()
484+
defer f.Unlock()
485+
switch c.T {
486+
case "b":
487+
if c.V == "1" {
488+
return true, nil
489+
} else if c.V == "0" {
490+
return false, nil
491+
}
492+
case "s":
493+
if c.V != "" {
494+
xlsxSI := 0
495+
xlsxSI, _ = strconv.Atoi(c.V)
496+
if _, ok := f.tempFiles.Load(defaultXMLPathSharedStrings); ok {
497+
return f.getFromStringItem(xlsxSI), nil
498+
}
499+
if len(d.SI) > xlsxSI {
500+
return d.SI[xlsxSI].String(), nil
501+
}
502+
}
503+
case "str":
504+
return c.V, nil
505+
case "inlineStr":
506+
if c.IS != nil {
507+
return c.IS.String(), nil
508+
}
509+
return c.V, nil
510+
default:
511+
if isNum, precision := isNumeric(c.V); isNum {
512+
var precisionV string
513+
if precision == 0 {
514+
precisionV = roundPrecision(c.V, 15)
515+
} else {
516+
precisionV = roundPrecision(c.V, -1)
517+
}
518+
519+
vi, erri := strconv.ParseInt(precisionV, 10, 64)
520+
vf, errf := strconv.ParseFloat(precisionV, 64)
521+
if erri == nil {
522+
return vi, nil
523+
} else if errf == nil {
524+
return vf, nil
525+
} else {
526+
return precisionV, nil
527+
}
528+
}
529+
// TODO: add support for other possible values of T (https://stackoverflow.com/questions/18334314/what-do-excel-xml-cell-attribute-values-mean)
530+
}
531+
532+
return c.V, nil
533+
}
534+
478535
// roundPrecision provides a function to format floating-point number text
479536
// with precision, if the given text couldn't be parsed to float, this will
480537
// return the original string.

rows_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ func TestRows(t *testing.T) {
2424
t.FailNow()
2525
}
2626

27-
var collectedRows [][]string
27+
var collectedRows [][]Cell
2828
for rows.Next() {
2929
columns, err := rows.Columns()
3030
assert.NoError(t, err)
31-
collectedRows = append(collectedRows, trimSliceSpace(columns))
31+
collectedRows = append(collectedRows, columns)
3232
}
3333
if !assert.NoError(t, rows.Error()) {
3434
t.FailNow()
@@ -37,9 +37,6 @@ func TestRows(t *testing.T) {
3737

3838
returnedRows, err := f.GetRows(sheet2)
3939
assert.NoError(t, err)
40-
for i := range returnedRows {
41-
returnedRows[i] = trimSliceSpace(returnedRows[i])
42-
}
4340
if !assert.Equal(t, collectedRows, returnedRows) {
4441
t.FailNow()
4542
}

0 commit comments

Comments
 (0)