Skip to content

Commit 0b3a243

Browse files
author
Thomas Charbonnel
committed
qax-os#1284 WIP add formula
1 parent 218e350 commit 0b3a243

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
@@ -698,6 +698,9 @@ func (f *File) addSheetNameSpace(sheet string, ns xml.Attr) {
698698
// the precision for the numeric.
699699
func isNumeric(s string) (bool, int) {
700700
dot, e, n, p := false, false, false, 0
701+
if s == "" {
702+
return false, 0
703+
}
701704
for i, v := range s {
702705
if v == '.' {
703706
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
@@ -235,9 +236,13 @@ func (rows *Rows) rowXMLHandler(rowIterator *rowXMLIterator, xmlElement *xml.Sta
235236
// rowIterator.columns = append(appendSpace(blank, rowIterator.columns), val)
236237
//}
237238
blank := rowIterator.cellCol - len(rowIterator.columns)
238-
if val, _ := colCell.getTypedValueFrom(rows.f, rows.sst); val != "" || colCell.F != nil {
239-
rowIterator.columns = append(appendSpace(blank, rowIterator.columns), Cell{Value: val, StyleID: colCell.S})
239+
var formula string
240+
if colCell.F != nil {
241+
formula, _ = rows.f.GetCellFormula(rows.sheetName, colCell.R)
240242
}
243+
//if val, _ := colCell.getTypedValueFrom(rows.f, rows.sst); val != "" || colCell.F != nil {
244+
val, _ := colCell.getTypedValueFrom(rows.f, rows.sst)
245+
rowIterator.columns = append(appendSpace(blank, rowIterator.columns), Cell{Value: val, StyleID: colCell.S, Formula: formula})
241246
}
242247
}
243248

@@ -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
}
@@ -549,6 +554,8 @@ func (c *xlsxC) getTypedValueFrom(f *File, d *xlsxSST) (interface{}, error) {
549554
} else {
550555
return precisionV, nil
551556
}
557+
} else {
558+
return nil, nil
552559
}
553560
// TODO: add support for other possible values of T (https://stackoverflow.com/questions/18334314/what-do-excel-xml-cell-attribute-values-mean)
554561
}

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)