Skip to content
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

Cannot use struct value returned by GetDefinedName as argument to DeleteDefinedName #879

Closed
Orionsg opened this issue Jul 13, 2021 · 2 comments

Comments

@Orionsg
Copy link

Orionsg commented Jul 13, 2021

When I use the struct values returned by the "GetDefinedName" function to find and delete a specific defined name using the "DeleteDefinedName" function, I get this error:

"no defined name on the scope"

In the sample code below, the action takes place in the function "delDefName". An argument with the name is supplied and the code iterates over the defined names until it finds a match and it then uses the struct value as an argument to the "DeleteDefinedName" function.

Here is code that has been tested on the latest master branch (v2.4.1-0.20210711160239-f62c45fe0c11):

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/360EntSecGroup-Skylar/excelize/v2"
)

const s_file = "test.xlsx"
const s_sh = "test"
const s_def = "data"

func main() {
	fileDelete(s_file)
	wb := excelize.NewFile()

	// add new sheet and data to it
	wb.NewSheet(s_sh)
	sl_cells := []string{"A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"}
	for _, s_cell := range sl_cells {
		wb.SetCellStr(s_sh, s_cell, s_cell)
	}

	// set defined name for the data area
	if err := wb.SetDefinedName(&excelize.DefinedName{
		Name:     s_def,
		RefersTo: "test!$A1:$C3",
		Comment:  "Data area",
	}); err != nil {
		log.Fatal("Can't set defined name: ", s_def, "Error: ", err)
	}

	delDefName(wb, s_def)

	if err := wb.SaveAs(s_file); err != nil {
		log.Fatal("Could not save new Excel file `", s_file, "`: ", err)
	}
	fmt.Println("Done")
}

func delDefName(wb *excelize.File, s_def string) {
	for _, def := range wb.GetDefinedName() {
		if def.Name == s_def {
			fmt.Printf("%+v \n", def)
			if err := wb.DeleteDefinedName(&def); err != nil {
				log.Fatal("Cannot delete defined name: `", s_def, "`, Error: ", err)
			}
			break
		}
	}
}

func fileExists(s_file string) bool {
	info, err := os.Stat(s_file)
	if os.IsNotExist(err) {
		return false
	}
	if info.IsDir() {
		log.Fatal("Error: ", s_file, " is a directory")
	}
	return true
}

func fileDelete(s_file string) {
	if fileExists(s_file) {
		err := os.Remove(s_file)
		if err != nil {
			log.Fatal("Problem removing file `", s_file, "`: ", err)
		}
	}
}
@xuri xuri closed this as completed in fbcfdea Jul 15, 2021
@xuri
Copy link
Member

xuri commented Jul 15, 2021

Thanks for your feedback, I have fixed it, please try to use the master branch code, and this patch will be released in the next version.

@Orionsg
Copy link
Author

Orionsg commented Jul 15, 2021

Yes, this works now, thanks!

jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants