From b14b74bf560f192f658d66fdbf719190f691bc5f Mon Sep 17 00:00:00 2001 From: xuri Date: Sat, 10 Jul 2021 23:47:35 +0800 Subject: [PATCH] This closes #873, make the sheet names are not case sensitive for `NewSheet`, `GetSheetIndex`, `DeleteSheet` --- sheet.go | 26 +++++++++++++++----------- sheet_test.go | 3 ++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/sheet.go b/sheet.go index 8d3d457..ac22b88 100644 --- a/sheet.go +++ b/sheet.go @@ -33,8 +33,9 @@ import ( // NewSheet provides the function to create a new sheet by given a worksheet // name and returns the index of the sheets in the workbook -// (spreadsheet) after it appended. Note that when creating a new spreadsheet -// file, the default worksheet named `Sheet1` will be created. +// (spreadsheet) after it appended. Note that the worksheet names are not +// case sensitive, when creating a new spreadsheet file, the default +// worksheet named `Sheet1` will be created. func (f *File) NewSheet(name string) int { // Check if the worksheet already exists index := f.GetSheetIndex(name) @@ -391,12 +392,13 @@ func (f *File) getSheetID(name string) int { } // GetSheetIndex provides a function to get a sheet index of the workbook by -// the given sheet name. If the given sheet name is invalid or sheet doesn't -// exist, it will return an integer type value -1. +// the given sheet name, the sheet names are not case sensitive. If the given +// sheet name is invalid or sheet doesn't exist, it will return an integer +// type value -1. func (f *File) GetSheetIndex(name string) int { var idx = -1 for index, sheet := range f.GetSheetList() { - if sheet == trimSheetName(name) { + if strings.EqualFold(sheet, trimSheetName(name)) { idx = index } } @@ -485,10 +487,12 @@ func (f *File) SetSheetBackground(sheet, picture string) error { } // DeleteSheet provides a function to delete worksheet in a workbook by given -// worksheet name. Use this method with caution, which will affect changes in -// references such as formulas, charts, and so on. If there is any referenced -// value of the deleted worksheet, it will cause a file error when you open it. -// This function will be invalid when only the one worksheet is left. +// worksheet name, the sheet names are not case sensitive.the sheet names are +// not case sensitive. Use this method with caution, which will affect +// changes in references such as formulas, charts, and so on. If there is any +// referenced value of the deleted worksheet, it will cause a file error when +// you open it. This function will be invalid when only the one worksheet is +// left. func (f *File) DeleteSheet(name string) { if f.SheetCount == 1 || f.GetSheetIndex(name) == -1 { return @@ -514,7 +518,7 @@ func (f *File) DeleteSheet(name string) { } } for idx, sheet := range wb.Sheets.Sheet { - if sheet.Name == sheetName { + if strings.EqualFold(sheet.Name, sheetName) { wb.Sheets.Sheet = append(wb.Sheets.Sheet[:idx], wb.Sheets.Sheet[idx+1:]...) var sheetXML, rels string if wbRels != nil { @@ -528,7 +532,7 @@ func (f *File) DeleteSheet(name string) { target := f.deleteSheetFromWorkbookRels(sheet.ID) f.deleteSheetFromContentTypes(target) f.deleteCalcChain(sheet.SheetID, "") - delete(f.sheetMap, sheetName) + delete(f.sheetMap, sheet.Name) f.Pkg.Delete(sheetXML) f.Pkg.Delete(rels) f.Relationships.Delete(rels) diff --git a/sheet_test.go b/sheet_test.go index 268abdc..0a604de 100644 --- a/sheet_test.go +++ b/sheet_test.go @@ -81,7 +81,8 @@ func ExampleFile_GetPageLayout() { func TestNewSheet(t *testing.T) { f := NewFile() - sheetID := f.NewSheet("Sheet2") + f.NewSheet("Sheet2") + sheetID := f.NewSheet("sheet2") f.SetActiveSheet(sheetID) // delete original sheet f.DeleteSheet(f.GetSheetName(f.GetSheetIndex("Sheet1")))