Skip to content

Commit

Permalink
This closes #1807, calculation engine support date and formula type c…
Browse files Browse the repository at this point in the history
…ell (#1810)

Co-authored-by: zhualong <[email protected]>
  • Loading branch information
taitaking and kewenof authored Feb 4, 2024
1 parent bba155e commit 9cbe3b6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:

- name: Codecov
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
file: coverage.txt
flags: unittests
Expand Down
14 changes: 13 additions & 1 deletion calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1632,8 +1632,20 @@ func (f *File) cellResolver(ctx *calcContext, sheet, cell string) (formulaArg, e
return arg.ToNumber(), err
case CellTypeInlineString, CellTypeSharedString:
return arg, err
default:
case CellTypeFormula:
if value != "" {
return arg, err
}
return newEmptyFormulaArg(), err
case CellTypeDate:
if value, err = f.GetCellValue(sheet, cell); err == nil {
if num := newStringFormulaArg(value).ToNumber(); num.Type == ArgNumber {
return num, err
}
}
return arg, err
default:
return newErrorFormulaArg(value, value), err
}
}

Expand Down
29 changes: 29 additions & 0 deletions calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6379,6 +6379,35 @@ func TestCalcCellResolver(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expected, result)
}
// Test calculates formula that reference date and error type cells
assert.NoError(t, f.SetCellValue("Sheet1", "C1", "20200208T080910.123"))
assert.NoError(t, f.SetCellValue("Sheet1", "C2", "2020-07-10 15:00:00.000"))
assert.NoError(t, f.SetCellValue("Sheet1", "C3", formulaErrorDIV))
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
assert.True(t, ok)
ws.(*xlsxWorksheet).SheetData.Row[0].C[2].T = "d"
ws.(*xlsxWorksheet).SheetData.Row[0].C[2].V = "20200208T080910.123"
ws.(*xlsxWorksheet).SheetData.Row[1].C[2].T = "d"
ws.(*xlsxWorksheet).SheetData.Row[1].C[2].V = "2020-07-10 15:00:00.000"
ws.(*xlsxWorksheet).SheetData.Row[2].C[2].T = "e"
ws.(*xlsxWorksheet).SheetData.Row[2].C[2].V = formulaErrorDIV
for _, tbl := range [][]string{
{"D1", "=SUM(C1,1)", "43870.3397004977"},
{"D2", "=LEN(C2)", "23"},
{"D3", "=IFERROR(C3,TRUE)", "TRUE"},
} {
assert.NoError(t, f.SetCellFormula("Sheet1", tbl[0], tbl[1]))
result, err := f.CalcCellValue("Sheet1", tbl[0])
assert.NoError(t, err)
assert.Equal(t, tbl[2], result)
}
// Test calculates formula that reference invalid cell
assert.NoError(t, f.SetCellValue("Sheet1", "E1", "E1"))
assert.NoError(t, f.SetCellFormula("Sheet1", "F1", "=LEN(E1)"))
f.SharedStrings = nil
f.Pkg.Store(defaultXMLPathSharedStrings, MacintoshCyrillicCharset)
_, err := f.CalcCellValue("Sheet1", "F1")
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
}

func TestEvalInfixExp(t *testing.T) {
Expand Down

0 comments on commit 9cbe3b6

Please sign in to comment.