Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5083,6 +5083,23 @@ func TestCalcWithDefinedName(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "YES", result, "IF(\"B1_as_string\"=defined_name1,\"YES\",\"NO\")")

// the external workbook index 0 refers to the workbook scope defined name
// in the current workbook
for formula, expected := range map[string]string{
"[0]!defined_name1": "A1_as_string",
"[0]!defined_name2": "123",
"SUM([0]!defined_name2,1)": "124",
"CONCATENATE(\"<\",[0]!defined_name1,\">\")": "<A1_as_string>",
} {
assert.NoError(t, f.SetCellFormula("Sheet1", "D1", formula))
result, err = f.CalcCellValue("Sheet1", "D1")
assert.NoError(t, err, formula)
assert.Equal(t, expected, result, formula)
}
assert.NoError(t, f.SetCellFormula("Sheet1", "D1", "[0]!defined_name3"))
_, err = f.CalcCellValue("Sheet1", "D1")
assert.EqualError(t, err, formulaErrorNAME)

t.Run("for_sheet_name_with_space", func(t *testing.T) {
f := NewFile()
defer func() {
Expand Down
5 changes: 5 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ func coordinatesToRangeRef(coordinates []int, abs ...bool) (string, error) {
// getDefinedNameRefTo convert defined name to reference range.
func (f *File) getDefinedNameRefTo(definedNameName, currentSheet string) (refTo string) {
var workbookRefTo, worksheetRefTo string
// the external workbook index 0 refers to the current workbook, and the
// name qualified by a workbook is in the workbook scope
if name, ok := strings.CutPrefix(definedNameName, "[0]!"); ok {
definedNameName, currentSheet = name, ""
}
for _, definedName := range f.GetDefinedName() {
if definedName.Name == definedNameName {
// worksheet scope takes precedence over scope workbook when both definedNames exist
Expand Down
Loading