yao/excel/excel.go
Max 6e4b7cd59e feat: Add sheet management operations to Excel module
- Implement new process handlers for creating, reading, updating, copying, and deleting sheets in Excel files.
- Enhance the Open function to automatically create directories for file paths if they do not exist.
- Update README.md to include detailed documentation and examples for new sheet operations, improving user guidance.
2025-03-29 17:02:18 +08:00

115 lines
2.1 KiB
Go

package excel
import (
"fmt"
"os"
"path/filepath"
"sync"
"time"
"github.com/google/uuid"
"github.com/xuri/excelize/v2"
"github.com/yaoapp/yao/config"
)
// Excel the excel file
type Excel struct {
id string
path string
create int64
abs string
*excelize.File
}
// openFiles the open files
var openFiles = sync.Map{}
// Open open the excel file
func Open(path string, writable bool) (string, error) {
excel := &Excel{path: path}
// GET DATA ROOT
root := config.Conf.DataRoot
absPath, err := filepath.Abs(filepath.Join(root, path))
if err != nil {
return "", err
}
if writable {
// if the file not exists, create it
if _, err := os.Stat(absPath); os.IsNotExist(err) {
// Auto create dir
dir := filepath.Dir(absPath)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.MkdirAll(dir, 0644)
if err != nil {
return "", err
}
}
create := excelize.NewFile()
err := create.SaveAs(absPath)
if err != nil {
return "", err
}
create.Close()
}
excelFile, err := excelize.OpenFile(absPath)
if err != nil {
return "", err
}
id := uuid.NewString()
excel.File = excelFile
excel.id = id
excel.abs = absPath
excel.create = time.Now().Unix()
openFiles.Store(id, excel)
return id, nil
}
file, err := os.Open(absPath)
if err != nil {
return "", fmt.Errorf("open file %s failed: %w", absPath, err)
}
excelFile, err := excelize.OpenReader(file)
if err != nil {
return "", err
}
id := uuid.NewString()
excel.File = excelFile
excel.id = id
excel.abs = absPath
excel.create = time.Now().Unix()
openFiles.Store(id, excel)
return id, nil
}
// Close close the excel file
func Close(handler string) error {
excel, ok := openFiles.Load(handler)
if !ok {
return fmt.Errorf("file not found")
}
err := excel.(*Excel).Close()
if err != nil {
return err
}
openFiles.Delete(handler)
return nil
}
// Get get the excel file
func Get(handler string) (*Excel, error) {
excel, ok := openFiles.Load(handler)
if !ok {
return nil, fmt.Errorf("%s not found", handler)
}
return excel.(*Excel), nil
}