Fixed #735, refresh active tab after delete sheet

This commit is contained in:
xuri 2020-11-23 00:01:06 +08:00
parent 599a8cb0bc
commit 13e0ed2a69
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 17 additions and 15 deletions

View File

@ -765,21 +765,20 @@ func (f *File) formattedValue(s int, v string) string {
if s >= len(styleSheet.CellXfs.Xf) {
return v
}
numFmtId := *styleSheet.CellXfs.Xf[s].NumFmtID
ok := builtInNumFmtFunc[numFmtId]
numFmtID := *styleSheet.CellXfs.Xf[s].NumFmtID
ok := builtInNumFmtFunc[numFmtID]
if ok != nil {
return ok(v, builtInNumFmt[numFmtId])
return ok(v, builtInNumFmt[numFmtID])
}
if styleSheet == nil || styleSheet.NumFmts == nil {
return v
}
for _, xlsxFmt := range styleSheet.NumFmts.NumFmt {
if xlsxFmt.NumFmtID == numFmtId {
if xlsxFmt.NumFmtID == numFmtID {
format := strings.ToLower(xlsxFmt.FormatCode)
if strings.Contains(format, "y") || strings.Contains(format, "m") || strings.Contains(strings.Replace(format, "red", "", -1), "d") || strings.Contains(format, "h") {
return parseTime(v, format)
}
return v
}
}

View File

@ -317,7 +317,7 @@ func (f *File) GetActiveSheetIndex() (index int) {
return
}
// getActiveSheetID provides a function to get active sheet index of the
// getActiveSheetID provides a function to get active sheet ID of the
// spreadsheet. If not found the active sheet will be return integer 0.
func (f *File) getActiveSheetID() int {
wb := f.workbookReader()
@ -499,6 +499,7 @@ func (f *File) DeleteSheet(name string) {
sheetName := trimSheetName(name)
wb := f.workbookReader()
wbRels := f.relsReader(f.getWorkbookRelsPath())
activeSheetName := f.GetSheetName(f.GetActiveSheetIndex())
for idx, sheet := range wb.Sheets.Sheet {
if sheet.Name == sheetName {
wb.Sheets.Sheet = append(wb.Sheets.Sheet[:idx], wb.Sheets.Sheet[idx+1:]...)
@ -526,14 +527,7 @@ func (f *File) DeleteSheet(name string) {
f.SheetCount--
}
}
if wb.BookViews != nil {
for idx, bookView := range wb.BookViews.WorkBookView {
if bookView.ActiveTab >= f.SheetCount {
wb.BookViews.WorkBookView[idx].ActiveTab--
}
}
}
f.SetActiveSheet(len(f.GetSheetMap()))
f.SetActiveSheet(f.GetSheetIndex(activeSheetName))
}
// deleteSheetFromWorkbookRels provides a function to remove worksheet

View File

@ -359,6 +359,15 @@ func TestGetWorkbookRelsPath(t *testing.T) {
assert.Equal(t, "_rels/workbook.xml.rels", f.getWorkbookRelsPath())
}
func TestDeleteSheet(t *testing.T) {
f := NewFile()
f.SetActiveSheet(f.NewSheet("Sheet2"))
f.NewSheet("Sheet3")
f.DeleteSheet("Sheet1")
assert.Equal(t, "Sheet2", f.GetSheetName(f.GetActiveSheetIndex()))
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestDeleteSheet.xlsx")))
}
func BenchmarkNewSheet(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
@ -380,9 +389,9 @@ func BenchmarkFile_SaveAs(b *testing.B) {
for pb.Next() {
newSheetWithSave()
}
})
}
func newSheetWithSave() {
file := NewFile()
file.NewSheet("sheet1")