From a258e3d8580c5a4d16454bc4642d2a1704dd0ed9 Mon Sep 17 00:00:00 2001 From: funa12 <21205286+funa12@users.noreply.github.com> Date: Fri, 2 Feb 2024 11:11:16 +0900 Subject: [PATCH] Fix CalcCellValue does not return raw value when enable RawCellValue (#1803) --- calc.go | 2 +- calc_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/calc.go b/calc.go index a6086a0..ef1b0c0 100644 --- a/calc.go +++ b/calc.go @@ -823,7 +823,7 @@ func (f *File) CalcCellValue(sheet, cell string, opts ...Options) (result string styleIdx, _ = f.GetCellStyle(sheet, cell) } result = token.Value() - if isNum, precision, decimal := isNumeric(result); isNum { + if isNum, precision, decimal := isNumeric(result); isNum && !rawCellValue { if precision > 15 { result, err = f.formattedValue(&xlsxC{S: styleIdx, V: strings.ToUpper(strconv.FormatFloat(decimal, 'G', 15, 64))}, rawCellValue, CellTypeNumber) return diff --git a/calc_test.go b/calc_test.go index 83b578f..f6b3790 100644 --- a/calc_test.go +++ b/calc_test.go @@ -6301,6 +6301,28 @@ func TestNestedFunctionsWithOperators(t *testing.T) { } } +func TestFormulaRawCellValueOption(t *testing.T) { + f := NewFile() + rawTest := []struct { + value string + raw bool + expected string + }{ + {"=\"10e3\"", false, "10000"}, + {"=\"10e3\"", true, "10e3"}, + {"=\"10\" & \"e3\"", false, "10000"}, + {"=\"10\" & \"e3\"", true, "10e3"}, + {"=\"1111111111111111\"", false, "1.11111111111111E+15"}, + {"=\"1111111111111111\"", true, "1111111111111111"}, + } + for _, test := range rawTest { + assert.NoError(t, f.SetCellFormula("Sheet1", "A1", test.value)) + val, err := f.CalcCellValue("Sheet1", "A1", Options{RawCellValue: test.raw}) + assert.NoError(t, err) + assert.Equal(t, test.expected, val) + } +} + func TestFormulaArgToToken(t *testing.T) { assert.Equal(t, efp.Token{ -- GitLab