You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
}
}
}
The text was updated successfully, but these errors were encountered:
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):
The text was updated successfully, but these errors were encountered: