Skip to content

Commit 7fd5a67

Browse files
author
Thomas Charbonnel
committed
1 parent 45afab5 commit 7fd5a67

File tree

4 files changed

+108
-54
lines changed

4 files changed

+108
-54
lines changed

cell_test.go

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ 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)
@@ -246,21 +246,21 @@ func TestGetCellValue(t *testing.T) {
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")
@@ -270,13 +270,13 @@ func TestGetCellValue(t *testing.T) {
270270
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: 65 additions & 8 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...)
@@ -140,7 +140,7 @@ func (rows *Rows) Close() error {
140140
// Columns return the current row's column values. This fetches the worksheet
141141
// data as a stream, returns each cell in a row as is, and will not skip empty
142142
// rows in the tail of the worksheet.
143-
func (rows *Rows) Columns(opts ...Options) ([]string, error) {
143+
func (rows *Rows) Columns(opts ...Options) ([]Cell, error) {
144144
if rows.curRow > rows.seekRow {
145145
return nil, nil
146146
}
@@ -183,9 +183,9 @@ func (rows *Rows) Columns(opts ...Options) ([]string, error) {
183183
}
184184

185185
// appendSpace append blank characters to slice by given length and source slice.
186-
func appendSpace(l int, s []string) []string {
186+
func appendSpace(l int, s []Cell) []Cell {
187187
for i := 1; i < l; i++ {
188-
s = append(s, "")
188+
s = append(s, Cell{})
189189
}
190190
return s
191191
}
@@ -204,7 +204,7 @@ type rowXMLIterator struct {
204204
err error
205205
inElement string
206206
cellCol int
207-
columns []string
207+
columns []Cell
208208
}
209209

210210
// rowXMLHandler parse the row XML element of the worksheet.
@@ -218,9 +218,13 @@ func (rows *Rows) rowXMLHandler(rowIterator *rowXMLIterator, xmlElement *xml.Sta
218218
return
219219
}
220220
}
221+
//blank := rowIterator.cellCol - len(rowIterator.columns)
222+
//if val, _ := colCell.getValueFrom(rows.f, rows.sst, raw); val != "" || colCell.F != nil {
223+
// rowIterator.columns = append(appendSpace(blank, rowIterator.columns), val)
224+
//}
221225
blank := rowIterator.cellCol - len(rowIterator.columns)
222-
if val, _ := colCell.getValueFrom(rows.f, rows.sst, raw); val != "" || colCell.F != nil {
223-
rowIterator.columns = append(appendSpace(blank, rowIterator.columns), val)
226+
if val, _ := colCell.getTypedValueFrom(rows.f, rows.sst); val != "" || colCell.F != nil {
227+
rowIterator.columns = append(appendSpace(blank, rowIterator.columns), Cell{Value: val, StyleID: colCell.S})
224228
}
225229
}
226230
}
@@ -487,6 +491,59 @@ func (c *xlsxC) getValueFrom(f *File, d *xlsxSST, raw bool) (string, error) {
487491
}
488492
}
489493

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