|
|
@ -555,46 +555,58 @@ func (f *File) DeleteSheet(name string) {
|
|
|
|
wbRels := f.relsReader(f.getWorkbookRelsPath())
|
|
|
|
wbRels := f.relsReader(f.getWorkbookRelsPath())
|
|
|
|
activeSheetName := f.GetSheetName(f.GetActiveSheetIndex())
|
|
|
|
activeSheetName := f.GetSheetName(f.GetActiveSheetIndex())
|
|
|
|
deleteLocalSheetID := f.GetSheetIndex(name)
|
|
|
|
deleteLocalSheetID := f.GetSheetIndex(name)
|
|
|
|
// Delete and adjust defined names
|
|
|
|
deleteAndAdjustDefinedNames(wb, deleteLocalSheetID)
|
|
|
|
if wb.DefinedNames != nil {
|
|
|
|
|
|
|
|
for idx := 0; idx < len(wb.DefinedNames.DefinedName); idx++ {
|
|
|
|
for idx, sheet := range wb.Sheets.Sheet {
|
|
|
|
dn := wb.DefinedNames.DefinedName[idx]
|
|
|
|
if !strings.EqualFold(sheet.Name, sheetName) {
|
|
|
|
if dn.LocalSheetID != nil {
|
|
|
|
continue
|
|
|
|
localSheetID := *dn.LocalSheetID
|
|
|
|
}
|
|
|
|
if localSheetID == deleteLocalSheetID {
|
|
|
|
|
|
|
|
wb.DefinedNames.DefinedName = append(wb.DefinedNames.DefinedName[:idx], wb.DefinedNames.DefinedName[idx+1:]...)
|
|
|
|
wb.Sheets.Sheet = append(wb.Sheets.Sheet[:idx], wb.Sheets.Sheet[idx+1:]...)
|
|
|
|
idx--
|
|
|
|
var sheetXML, rels string
|
|
|
|
} else if localSheetID > deleteLocalSheetID {
|
|
|
|
if wbRels != nil {
|
|
|
|
wb.DefinedNames.DefinedName[idx].LocalSheetID = intPtr(*dn.LocalSheetID - 1)
|
|
|
|
for _, rel := range wbRels.Relationships {
|
|
|
|
|
|
|
|
if rel.ID == sheet.ID {
|
|
|
|
|
|
|
|
sheetXML = f.getWorksheetPath(rel.Target)
|
|
|
|
|
|
|
|
rels = "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[sheetName], "xl/worksheets/") + ".rels"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
target := f.deleteSheetFromWorkbookRels(sheet.ID)
|
|
|
|
|
|
|
|
f.deleteSheetFromContentTypes(target)
|
|
|
|
|
|
|
|
f.deleteCalcChain(sheet.SheetID, "")
|
|
|
|
|
|
|
|
delete(f.sheetMap, sheet.Name)
|
|
|
|
|
|
|
|
f.Pkg.Delete(sheetXML)
|
|
|
|
|
|
|
|
f.Pkg.Delete(rels)
|
|
|
|
|
|
|
|
f.Relationships.Delete(rels)
|
|
|
|
|
|
|
|
f.Sheet.Delete(sheetXML)
|
|
|
|
|
|
|
|
delete(f.xmlAttr, sheetXML)
|
|
|
|
|
|
|
|
f.SheetCount--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for idx, sheet := range wb.Sheets.Sheet {
|
|
|
|
f.SetActiveSheet(f.GetSheetIndex(activeSheetName))
|
|
|
|
if strings.EqualFold(sheet.Name, sheetName) {
|
|
|
|
}
|
|
|
|
wb.Sheets.Sheet = append(wb.Sheets.Sheet[:idx], wb.Sheets.Sheet[idx+1:]...)
|
|
|
|
|
|
|
|
var sheetXML, rels string
|
|
|
|
func deleteAndAdjustDefinedNames(wb *xlsxWorkbook, deleteLocalSheetID int) {
|
|
|
|
if wbRels != nil {
|
|
|
|
if wb == nil {
|
|
|
|
for _, rel := range wbRels.Relationships {
|
|
|
|
return
|
|
|
|
if rel.ID == sheet.ID {
|
|
|
|
}
|
|
|
|
sheetXML = f.getWorksheetPath(rel.Target)
|
|
|
|
|
|
|
|
rels = "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[sheetName], "xl/worksheets/") + ".rels"
|
|
|
|
if wb.DefinedNames == nil {
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for idx := 0; idx < len(wb.DefinedNames.DefinedName); idx++ {
|
|
|
|
|
|
|
|
dn := wb.DefinedNames.DefinedName[idx]
|
|
|
|
|
|
|
|
if dn.LocalSheetID != nil {
|
|
|
|
|
|
|
|
localSheetID := *dn.LocalSheetID
|
|
|
|
|
|
|
|
if localSheetID == deleteLocalSheetID {
|
|
|
|
|
|
|
|
wb.DefinedNames.DefinedName = append(wb.DefinedNames.DefinedName[:idx], wb.DefinedNames.DefinedName[idx+1:]...)
|
|
|
|
|
|
|
|
idx--
|
|
|
|
|
|
|
|
} else if localSheetID > deleteLocalSheetID {
|
|
|
|
|
|
|
|
wb.DefinedNames.DefinedName[idx].LocalSheetID = intPtr(*dn.LocalSheetID - 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
target := f.deleteSheetFromWorkbookRels(sheet.ID)
|
|
|
|
|
|
|
|
f.deleteSheetFromContentTypes(target)
|
|
|
|
|
|
|
|
f.deleteCalcChain(sheet.SheetID, "")
|
|
|
|
|
|
|
|
delete(f.sheetMap, sheet.Name)
|
|
|
|
|
|
|
|
f.Pkg.Delete(sheetXML)
|
|
|
|
|
|
|
|
f.Pkg.Delete(rels)
|
|
|
|
|
|
|
|
f.Relationships.Delete(rels)
|
|
|
|
|
|
|
|
f.Sheet.Delete(sheetXML)
|
|
|
|
|
|
|
|
delete(f.xmlAttr, sheetXML)
|
|
|
|
|
|
|
|
f.SheetCount--
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.SetActiveSheet(f.GetSheetIndex(activeSheetName))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// deleteSheetFromWorkbookRels provides a function to remove worksheet
|
|
|
|
// deleteSheetFromWorkbookRels provides a function to remove worksheet
|
|
|
|