Skip to content

Commit 38eb7c1

Browse files
authored
Trim single quotes from sheet names to fix calculation engine resolve references failed (#2253)
- Strip surrounding quotes during parsing to ensure formulas correctly resolve data across sheets with spaces, hyphens, or other special characters - Update unit tests
1 parent 2dcfb60 commit 38eb7c1

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
@@ -1534,7 +1534,7 @@ func parseRef(ref string) (cellRef, bool, bool, error) {
15341534
tokens = strings.Split(ref, "!")
15351535
)
15361536
if len(tokens) == 2 { // have a worksheet
1537-
cr.Sheet, cell = tokens[0], tokens[1]
1537+
cr.Sheet, cell = strings.TrimSuffix(strings.TrimPrefix(tokens[0], "'"), "'"), tokens[1]
15381538
}
15391539
if cr.Col, cr.Row, err = CellNameToCoordinates(cell); err != nil {
15401540
if cr.Col, colErr = ColumnNameToNumber(cell); colErr == nil { // cast to column

calc_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4797,6 +4797,28 @@ func TestCalcWithDefinedName(t *testing.T) {
47974797
result, err = f.CalcCellValue("Sheet1", "D1")
47984798
assert.NoError(t, err)
47994799
assert.Equal(t, "YES", result, "IF(\"B1_as_string\"=defined_name1,\"YES\",\"NO\")")
4800+
4801+
t.Run("for_sheet_name_with_space", func(t *testing.T) {
4802+
f := NewFile()
4803+
defer func() {
4804+
assert.NoError(t, f.Close())
4805+
}()
4806+
assert.NoError(t, f.SetSheetName("Sheet1", "Sheet 1"))
4807+
cells := []string{"A1", "A2", "A3", "A4"}
4808+
names := []string{"val1", "val2", "val3", "val4"}
4809+
for idx, v := range []interface{}{100, 20, 30, 5} {
4810+
assert.NoError(t, f.SetCellValue("Sheet 1", cells[idx], v))
4811+
}
4812+
for idx, cell := range cells {
4813+
assert.NoError(t, f.SetDefinedName(&DefinedName{
4814+
Name: names[idx], RefersTo: "'Sheet 1'!" + cell,
4815+
}))
4816+
}
4817+
assert.NoError(t, f.SetCellFormula("Sheet 1", "B1", "=val1-val2-val3-val4"))
4818+
result, err := f.CalcCellValue("Sheet 1", "B1")
4819+
assert.NoError(t, err)
4820+
assert.Equal(t, "45", result)
4821+
})
48004822
}
48014823

48024824
func TestCalcISBLANK(t *testing.T) {

0 commit comments

Comments
 (0)