forked from p30928647/excelize
New function `GetSheetIndex()` added, relate issue #25;
Fix return index error in function `GetSheetMap()`; Fix return worksheet name error in function `GetSheetName()`; go test updated
This commit is contained in:
parent
8fd061b98f
commit
b4ed9cc40f
|
@ -72,6 +72,8 @@ func TestOpenFile(t *testing.T) {
|
|||
xlsx.GetCellValue("Sheet2", "E231")
|
||||
// Test get active sheet of XLSX and get sheet name of XLSX by given sheet index.
|
||||
xlsx.GetSheetName(xlsx.GetActiveSheetIndex())
|
||||
// Test get sheet index of XLSX by given worksheet name.
|
||||
xlsx.GetSheetIndex("Sheet1")
|
||||
// Test get sheet name of XLSX by given invalid sheet index.
|
||||
xlsx.GetSheetName(4)
|
||||
// Test get sheet map of XLSX.
|
||||
|
|
41
sheet.go
41
sheet.go
|
@ -256,18 +256,42 @@ func (f *File) SetSheetName(oldName, newName string) {
|
|||
}
|
||||
}
|
||||
|
||||
// GetSheetName provides function to get sheet name of XLSX by given sheet
|
||||
// GetSheetName provides function to get sheet name of XLSX by given worksheet
|
||||
// index. If given sheet index is invalid, will return an empty string.
|
||||
func (f *File) GetSheetName(index int) string {
|
||||
content := f.workbookReader()
|
||||
for _, v := range content.Sheets.Sheet {
|
||||
if v.ID == "rId"+strconv.Itoa(index) {
|
||||
return v.Name
|
||||
rels := f.workbookRelsReader()
|
||||
for _, rel := range rels.Relationships {
|
||||
rID, _ := strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(rel.Target, "worksheets/sheet"), ".xml"))
|
||||
if rID == index {
|
||||
for _, v := range content.Sheets.Sheet {
|
||||
if v.ID == rel.ID {
|
||||
return v.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetSheetIndex provides function to get worksheet index of XLSX by given sheet
|
||||
// name. If given sheet name is invalid, will return an integer type value 0.
|
||||
func (f *File) GetSheetIndex(name string) int {
|
||||
content := f.workbookReader()
|
||||
rels := f.workbookRelsReader()
|
||||
for _, v := range content.Sheets.Sheet {
|
||||
if v.Name == name {
|
||||
for _, rel := range rels.Relationships {
|
||||
if v.ID == rel.ID {
|
||||
rID, _ := strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(rel.Target, "worksheets/sheet"), ".xml"))
|
||||
return rID
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetSheetMap provides function to get sheet map of XLSX. For example:
|
||||
//
|
||||
// xlsx, err := excelize.OpenFile("/tmp/Workbook.xlsx")
|
||||
|
@ -281,10 +305,15 @@ func (f *File) GetSheetName(index int) string {
|
|||
//
|
||||
func (f *File) GetSheetMap() map[int]string {
|
||||
content := f.workbookReader()
|
||||
rels := f.workbookRelsReader()
|
||||
sheetMap := map[int]string{}
|
||||
for _, v := range content.Sheets.Sheet {
|
||||
id, _ := strconv.Atoi(strings.TrimPrefix(v.ID, "rId"))
|
||||
sheetMap[id] = v.Name
|
||||
for _, rel := range rels.Relationships {
|
||||
if rel.ID == v.ID {
|
||||
rID, _ := strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(rel.Target, "worksheets/sheet"), ".xml"))
|
||||
sheetMap[rID] = v.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
return sheetMap
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue