Skip to content

SetSheetVisible for datagrip export excel, will panic: runtime error: invalid memory address or nil pointer dereference #1969

@quxionglie

Description

@quxionglie

Description

I use JetBrains datagrip database tool to export excel file, but using f.SetSheetVisible(sheet, false) will cause exception. But if I save it in excel and call f.SetSheetVisible(sheet, false) again, it will be normal.

step:
1.use datagrip,Right click on the SQL statement "execute to file " ,save as excel
2.call HideSheet(xxx)

input data : in.xlsx
in.xlsx

code:


func HideSheet(filename string) {
	slog.Info("处理文件", "filename", filename)
	f, err := excelize.OpenFile(filename)
	procError(err)

	defer func() {
		if err := f.Close(); err != nil {
			procError(err)
		}
	}()

	sheetList := f.GetSheetList()
	slog.Info("存在sheet列表", "sheetList", sheetList)
	if len(sheetList) == 2 {
		//隐藏第二个sheet
		sheet := sheetList[1]
		slog.Info("隐藏sheet", "sheet", sheet)
		err = f.SetSheetVisible(sheet, false)
		procError(err)

		err = f.SaveAs(filename)
		procError(err)
	}
}

func procError(err error) {
	if err != nil {
		slog.Info("错误", "err", err)
		os.Exit(-1)
	}
}

Output of go version:
go version go1.22.5 darwin/amd64

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x6fd04c0]

goroutine 1 [running]:
github.com/xuri/excelize/v2.(*File).SetSheetVisible(0xc000084540, {0xc00027c548, 0x5}, 0x0, {0x0, 0x0, 0xc000027ea8?})
        /Users/lie/go/pkg/mod/github.com/xuri/excelize/[email protected]/sheet.go:776 +0x3c0
main.HideSheet({0x702b1c0, 0x7})
        /Users/lie/goProjects/sql2excel/cmd/sheet/sheet_ctl.go:50 +0x287
main.main()
        /Users/lie/goProjects/sql2excel/cmd/sheet/sheet_ctl.go:15 +0x1f


image

Excelize version or commit ID:

github.com/xuri/excelize/v2 v2.8.1

Environment details (OS, Microsoft Excel™ version, physical, etc.):
Microsoft® Excel for Mac 版本 16.87 (24071426)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions