Skip to content

Commit a258e3d

Browse files
authored
Fix CalcCellValue does not return raw value when enable RawCellValue (#1803)
1 parent 99e91e1 commit a258e3d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

calc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ func (f *File) CalcCellValue(sheet, cell string, opts ...Options) (result string
823823
styleIdx, _ = f.GetCellStyle(sheet, cell)
824824
}
825825
result = token.Value()
826-
if isNum, precision, decimal := isNumeric(result); isNum {
826+
if isNum, precision, decimal := isNumeric(result); isNum && !rawCellValue {
827827
if precision > 15 {
828828
result, err = f.formattedValue(&xlsxC{S: styleIdx, V: strings.ToUpper(strconv.FormatFloat(decimal, 'G', 15, 64))}, rawCellValue, CellTypeNumber)
829829
return

calc_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6301,6 +6301,28 @@ func TestNestedFunctionsWithOperators(t *testing.T) {
63016301
}
63026302
}
63036303

6304+
func TestFormulaRawCellValueOption(t *testing.T) {
6305+
f := NewFile()
6306+
rawTest := []struct {
6307+
value string
6308+
raw bool
6309+
expected string
6310+
}{
6311+
{"=\"10e3\"", false, "10000"},
6312+
{"=\"10e3\"", true, "10e3"},
6313+
{"=\"10\" & \"e3\"", false, "10000"},
6314+
{"=\"10\" & \"e3\"", true, "10e3"},
6315+
{"=\"1111111111111111\"", false, "1.11111111111111E+15"},
6316+
{"=\"1111111111111111\"", true, "1111111111111111"},
6317+
}
6318+
for _, test := range rawTest {
6319+
assert.NoError(t, f.SetCellFormula("Sheet1", "A1", test.value))
6320+
val, err := f.CalcCellValue("Sheet1", "A1", Options{RawCellValue: test.raw})
6321+
assert.NoError(t, err)
6322+
assert.Equal(t, test.expected, val)
6323+
}
6324+
}
6325+
63046326
func TestFormulaArgToToken(t *testing.T) {
63056327
assert.Equal(t,
63066328
efp.Token{

0 commit comments

Comments
 (0)