mirror of
https://github.com/unidoc/unioffice.git
synced 2025-04-25 13:48:53 +08:00
spreadsheet: clear temp files when closing sheets
We don't bother keeping all files in memory as there can be several, and we don't need them all. This just adds a Close() method that can be used to ensure the temporary files are removed.
This commit is contained in:
parent
56e9890d9f
commit
76cd395f6a
@ -8,6 +8,8 @@
|
||||
package spreadsheet
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"baliance.com/gooxml"
|
||||
"baliance.com/gooxml/common"
|
||||
"baliance.com/gooxml/schema/soo/sml"
|
||||
@ -18,6 +20,8 @@ func New() *Workbook {
|
||||
wb := &Workbook{}
|
||||
wb.x = sml.NewWorkbook()
|
||||
|
||||
runtime.SetFinalizer(wb, workbookFinalizer)
|
||||
|
||||
wb.AppProperties = common.NewAppProperties()
|
||||
wb.CoreProperties = common.NewCoreProperties()
|
||||
wb.StyleSheet = NewStyleSheet(wb)
|
||||
|
@ -15,6 +15,7 @@ import (
|
||||
"image/jpeg"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"baliance.com/gooxml"
|
||||
@ -29,6 +30,9 @@ import (
|
||||
"baliance.com/gooxml/schema/soo/sml"
|
||||
)
|
||||
|
||||
// ErrorNotFound is returned when something is not found
|
||||
var ErrorNotFound = errors.New("not found")
|
||||
|
||||
// Workbook is the top level container item for a set of spreadsheets.
|
||||
type Workbook struct {
|
||||
common.DocBase
|
||||
@ -552,11 +556,26 @@ func (wb *Workbook) Protection() WorkbookProtection {
|
||||
return WorkbookProtection{wb.x.WorkbookProtection}
|
||||
}
|
||||
|
||||
func (wb *Workbook) GetSheet(name string) Sheet {
|
||||
// GetSheet returns a sheet by name, or an error if a sheet by the given name
|
||||
// was not found.
|
||||
func (wb *Workbook) GetSheet(name string) (Sheet, error) {
|
||||
for _, s := range wb.Sheets() {
|
||||
if s.Name() == name {
|
||||
return s
|
||||
return s, nil
|
||||
}
|
||||
}
|
||||
return Sheet{}
|
||||
return Sheet{}, ErrorNotFound
|
||||
}
|
||||
|
||||
func workbookFinalizer(wb *Workbook) {
|
||||
wb.Close()
|
||||
}
|
||||
|
||||
// Close closes the workbook, removing any temporary files that might have been
|
||||
// created when opening a document.
|
||||
func (wb *Workbook) Close() error {
|
||||
if wb.TmpPath != "" && strings.HasPrefix(wb.TmpPath, os.TempDir()) {
|
||||
return os.RemoveAll(wb.TmpPath)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user