Skip to content

Executing CalcCellValue containing a VLOOKUP formula takes a long time. #2139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 tasks done
zhuzhengyang opened this issue May 21, 2025 · 2 comments
Open
2 tasks done
Labels
enhancement New feature or request

Comments

@zhuzhengyang
Copy link

zhuzhengyang commented May 21, 2025

Description

Executing calculations with formulas containing nested VLOOKUPs is very slow, and the more rows in the table, the slower the speed.
flame

Steps to reproduce the issue

package test

import (
	"github.com/xuri/excelize/v2"
	"testing"

	. "github.com/smartystreets/goconvey/convey"
)

func TestVlookup(t *testing.T) {
	Convey("test VLOOKUP", t, func() {
		file := excelize.NewFile()
		// sheet1
		sheet1Name := "Sheet1"
		var err error
		err = file.SetCellStr(sheet1Name, "A1", "a")
		So(err, ShouldBeNil)
		err = file.SetCellFormula(sheet1Name, "A2", `VLOOKUP($A1,'Sheet2'!$A:$D,2,FALSE)`)
		So(err, ShouldBeNil)

		// sheet2
		sheet2Name := "Sheet2"
		_, err = file.NewSheet(sheet2Name)
		So(err, ShouldBeNil)
		var cellsValue = map[string]string{
			"A1": "a",
			"A2": "b",
			"A3": "c",
			"A4": "d",
			"A5": "e",
			"A6": "f",
			"A7": "g",
			"B1": "hero_level_name_1",
			"B2": "hero_level_name_2",
			"B3": "hero_level_name_3",
			"B4": "hero_level_name_4",
			"B5": "hero_level_name_5",
			"B6": "hero_level_name_6",
			"B7": "hero_level_name_7",
			"E1": "w",
			"E2": "w",
			"E3": "w",
			"E4": "w",
			"E5": "w",
			"E6": "w",
			"E7": "w",
			"I1": "w",
			"I2": "g",
			"I3": "b",
			"I4": "p",
			"I5": "o",
			"J1": "#FFFFFF",
			"J2": "#FFFFFF",
			"J3": "#FFFFFF",
			"J4": "#FFFFFF",
			"J5": "#FFFFFF",
		}
		for k, v := range cellsValue {
			err = file.SetCellStr(sheet2Name, k, v)
			So(err, ShouldBeNil)
		}
		var cellsFormula = map[string]string{
			"D1": `VLOOKUP($E1,$I:$J,2,FALSE)`,
			"D2": `VLOOKUP($E2,$I:$J,2,FALSE)`,
			"D3": `VLOOKUP($E3,$I:$J,2,FALSE)`,
			"D4": `VLOOKUP($E4,$I:$J,2,FALSE)`,
			"D5": `VLOOKUP($E5,$I:$J,2,FALSE)`,
			"D6": `VLOOKUP($E6,$I:$J,2,FALSE)`,
			"D7": `VLOOKUP($E7,$I:$J,2,FALSE)`,
		}
		for k, v := range cellsFormula {
			err = file.SetCellFormula(sheet2Name, k, v)
			So(err, ShouldBeNil)
		}

		// CalcCellValue VLOOKUP
		result, err := file.CalcCellValue(sheet1Name, "A2")
		So(err, ShouldBeNil)
		So(result, ShouldEqual, "hero_level_name_1")
	})
}


### Describe the results you received

cost time > 1s 

### Describe the results you expected

When there is a lot of data, calculating a single cell can take up to 20 seconds.

### Go version

go1.24.2

### Excelize version or commit ID

2.9.1

### Environment

```shell
Apple M3 Pro
macos 14.3

Validations

  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • The provided reproduction is a minimal reproducible example of the bug.
@zhuzhengyang
Copy link
Author

parameter lookupArray.Matrix of func lookupLinearSearch has 1048576 elements...

@zhuzhengyang
Copy link
Author

rangeResolver still takes a significant amount of time to allocate memory. My spreadsheet only has a few dozen elements. How can I prevent the formula from accessing millions of unnecessary elements?

@xuri xuri added the enhancement New feature or request label May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants