Skip to content

Commit 234c554

Browse files
author
Thomas Charbonnel
committed
1 parent c8e5b9c commit 234c554

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...)
@@ -147,7 +147,7 @@ func (rows *Rows) Close() error {
147147
// Columns return the current row's column values. This fetches the worksheet
148148
// data as a stream, returns each cell in a row as is, and will not skip empty
149149
// rows in the tail of the worksheet.
150-
func (rows *Rows) Columns(opts ...Options) ([]string, error) {
150+
func (rows *Rows) Columns(opts ...Options) ([]Cell, error) {
151151
if rows.curRow > rows.seekRow {
152152
return nil, nil
153153
}
@@ -190,9 +190,9 @@ func (rows *Rows) Columns(opts ...Options) ([]string, error) {
190190
}
191191

192192
// appendSpace append blank characters to slice by given length and source slice.
193-
func appendSpace(l int, s []string) []string {
193+
func appendSpace(l int, s []Cell) []Cell {
194194
for i := 1; i < l; i++ {
195-
s = append(s, "")
195+
s = append(s, Cell{})
196196
}
197197
return s
198198
}
@@ -211,7 +211,7 @@ type rowXMLIterator struct {
211211
err error
212212
inElement string
213213
cellCol int
214-
columns []string
214+
columns []Cell
215215
}
216216

217217
// rowXMLHandler parse the row XML element of the worksheet.
@@ -225,9 +225,13 @@ func (rows *Rows) rowXMLHandler(rowIterator *rowXMLIterator, xmlElement *xml.Sta
225225
return
226226
}
227227
}
228+
//blank := rowIterator.cellCol - len(rowIterator.columns)
229+
//if val, _ := colCell.getValueFrom(rows.f, rows.sst, raw); val != "" || colCell.F != nil {
230+
// rowIterator.columns = append(appendSpace(blank, rowIterator.columns), val)
231+
//}
228232
blank := rowIterator.cellCol - len(rowIterator.columns)
229-
if val, _ := colCell.getValueFrom(rows.f, rows.sst, raw); val != "" || colCell.F != nil {
230-
rowIterator.columns = append(appendSpace(blank, rowIterator.columns), val)
233+
if val, _ := colCell.getTypedValueFrom(rows.f, rows.sst); val != "" || colCell.F != nil {
234+
rowIterator.columns = append(appendSpace(blank, rowIterator.columns), Cell{Value: val, StyleID: colCell.S})
231235
}
232236
}
233237
}
@@ -494,6 +498,59 @@ func (c *xlsxC) getValueFrom(f *File, d *xlsxSST, raw bool) (string, error) {
494498
}
495499
}
496500

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