Skip to content

Commit cae714f

Browse files
author
Thomas Charbonnel
committed
qax-os#1284 WIP add formula
1 parent 483966b commit cae714f

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

lib.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,9 @@ func (f *File) addSheetNameSpace(sheet string, ns xml.Attr) {
693693
// the precision for the numeric.
694694
func isNumeric(s string) (bool, int) {
695695
dot, e, n, p := false, false, false, 0
696+
if s == "" {
697+
return false, 0
698+
}
696699
for i, v := range s {
697700
if v == '.' {
698701
if dot {

rows.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ type Rows struct {
7474
err error
7575
curRow, seekRow int
7676
needClose, rawCellValue bool
77-
sheet string
77+
sheetPath string
78+
sheetName string
7879
f *File
7980
tempFile *os.File
8081
sst *xlsxSST
@@ -236,9 +237,13 @@ func (rows *Rows) rowXMLHandler(rowIterator *rowXMLIterator, xmlElement *xml.Sta
236237
// rowIterator.columns = append(appendSpace(blank, rowIterator.columns), val)
237238
//}
238239
blank := rowIterator.cellCol - len(rowIterator.columns)
239-
if val, _ := colCell.getTypedValueFrom(rows.f, rows.sst); val != "" || colCell.F != nil {
240-
rowIterator.columns = append(appendSpace(blank, rowIterator.columns), Cell{Value: val, StyleID: colCell.S})
240+
var formula string
241+
if colCell.F != nil {
242+
formula, _ = rows.f.GetCellFormula(rows.sheetName, colCell.R)
241243
}
244+
//if val, _ := colCell.getTypedValueFrom(rows.f, rows.sst); val != "" || colCell.F != nil {
245+
val, _ := colCell.getTypedValueFrom(rows.f, rows.sst)
246+
rowIterator.columns = append(appendSpace(blank, rowIterator.columns), Cell{Value: val, StyleID: colCell.S, Formula: formula})
242247
}
243248
}
244249

@@ -277,7 +282,7 @@ func (f *File) Rows(sheet string) (*Rows, error) {
277282
f.saveFileList(name, f.replaceNameSpaceBytes(name, output))
278283
}
279284
var err error
280-
rows := Rows{f: f, sheet: name}
285+
rows := Rows{f: f, sheetPath: name, sheetName: sheet}
281286
rows.needClose, rows.decoder, rows.tempFile, err = f.xmlDecoder(name)
282287
return &rows, err
283288
}
@@ -547,6 +552,8 @@ func (c *xlsxC) getTypedValueFrom(f *File, d *xlsxSST) (interface{}, error) {
547552
} else {
548553
return precisionV, nil
549554
}
555+
} else {
556+
return nil, nil
550557
}
551558
// TODO: add support for other possible values of T (https://stackoverflow.com/questions/18334314/what-do-excel-xml-cell-attribute-values-mean)
552559
}

rows_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,30 @@ func TestRowsIterator(t *testing.T) {
6969

7070
rows, err := f.Rows(sheetName)
7171
require.NoError(t, err)
72+
expectedCells := [][]Cell{
73+
{Cell{Value: "Monitor", StyleID: 1}, Cell{StyleID: 1}, Cell{Value: "Brand", StyleID: 2}, Cell{StyleID: 2}, Cell{Value: "inlineStr"}},
74+
{Cell{Value: "> 23 Inch", StyleID: 1}, Cell{Value: int64(19), StyleID: 1}, Cell{Value: "HP", StyleID: 3}, Cell{Value: int64(200), StyleID: 4}},
75+
{Cell{Value: "20-23 Inch", StyleID: 1}, Cell{Value: int64(24), StyleID: 1}, Cell{Value: "DELL", StyleID: 3}, Cell{Value: int64(450), StyleID: 4}},
76+
{Cell{Value: "17-20 Inch", StyleID: 1}, Cell{Value: int64(56), StyleID: 1}, Cell{Value: "Lenove", StyleID: 3}, Cell{Value: int64(200), StyleID: 4}},
77+
{Cell{Value: "< 17 Inch", StyleID: 5}, Cell{Value: int64(21), StyleID: 1}, Cell{Value: "SONY", StyleID: 3}, Cell{Value: int64(510), StyleID: 4}},
78+
{Cell{}, Cell{}, Cell{Value: "Acer", StyleID: 3}, Cell{Value: int64(315), StyleID: 4}},
79+
{Cell{}, Cell{}, Cell{Value: "IBM", StyleID: 3}, Cell{Value: int64(127), StyleID: 4}},
80+
{Cell{}, Cell{}, Cell{Value: "ASUS", StyleID: 4}, Cell{Value: int64(89), StyleID: 4}},
81+
{Cell{}, Cell{}, Cell{Value: "Apple", StyleID: 4}, Cell{Value: int64(348), StyleID: 4}},
82+
{Cell{}, Cell{}, Cell{Value: "SAMSUNG", StyleID: 4}, Cell{Value: int64(53), StyleID: 4}},
83+
{Cell{}, Cell{}, Cell{Value: "Other", StyleID: 4}, Cell{Value: int64(37), StyleID: 4}, Cell{Formula: "B2+B3", StyleID: 4}, Cell{Formula: "IF(B2>0, (D2/B2)*100, 0)", StyleID: 4}, Cell{Formula: "IF(B2>0, (D2/B2)*100, 0)", StyleID: 4}, Cell{Formula: "IF(D2>0, (F2/D2)*100, 0)", StyleID: 4}, Cell{Formula: "IF(D2>0, (F2/D2)*100, 0)", StyleID: 4}},
84+
}
85+
gotCells := [][]Cell{}
7286

7387
for rows.Next() {
7488
rowCount++
7589
require.True(t, rowCount <= expectedNumRow, "rowCount is greater than expected")
90+
cols, err := rows.Columns()
91+
require.NoError(t, err)
92+
gotCells = append(gotCells, cols)
7693
}
7794
assert.Equal(t, expectedNumRow, rowCount)
95+
assert.Equal(t, expectedCells, gotCells)
7896
assert.NoError(t, rows.Close())
7997
assert.NoError(t, f.Close())
8098

0 commit comments

Comments
 (0)