Compare commits
No commits in common. "main" and "v0.0.1" have entirely different histories.
129
Excel.go
129
Excel.go
@ -5,20 +5,43 @@ import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"apigo.cc/gojs"
|
||||
"apigo.cc/gojs/goja"
|
||||
"github.com/ssgo/u"
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
// func WriteExcel(excelFile, sheetName string, table [][]string, password *string) error {
|
||||
// var f *excelize.File
|
||||
// var err error
|
||||
// if f, err = excelize.OpenFile(excelFile, excelize.Options{Password: u.String(password)}); err != nil {
|
||||
// f = excelize.NewFile()
|
||||
// }
|
||||
// if sheetName == "" {
|
||||
// sheetName = "Sheet1"
|
||||
// }
|
||||
// if sheetName >= "0" && sheetName <= "9" {
|
||||
// sheetName = f.GetSheetName(u.Int(sheetName))
|
||||
// }
|
||||
|
||||
// for y, row := range table {
|
||||
// for x, v := range row {
|
||||
// _ = f.SetCellStr(sheetName, MakeCellID(x, y), v)
|
||||
// }
|
||||
// }
|
||||
// if err == nil {
|
||||
// err = f.Save()
|
||||
// } else {
|
||||
// err = f.SaveAs(excelFile, excelize.Options{Password: u.String(password)})
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
|
||||
type Excel struct {
|
||||
filename string
|
||||
password string
|
||||
excel *excelize.File
|
||||
}
|
||||
|
||||
func OpenExcel(filename string, password *string, vm *goja.Runtime) (*Excel, error) {
|
||||
filename = gojs.FixPath(vm, filename)
|
||||
func OpenExcel(filename string, password *string) (*Excel, error) {
|
||||
xls := &Excel{
|
||||
filename: filename,
|
||||
password: u.String(password),
|
||||
@ -34,9 +57,8 @@ func OpenExcel(filename string, password *string, vm *goja.Runtime) (*Excel, err
|
||||
return xls, err
|
||||
}
|
||||
|
||||
func (xls *Excel) Save(filename, password *string, vm *goja.Runtime) error {
|
||||
func (xls *Excel) Save(filename, password *string) error {
|
||||
if filename != nil {
|
||||
*filename = gojs.FixPath(vm, *filename)
|
||||
xls.filename = *filename
|
||||
}
|
||||
if password != nil {
|
||||
@ -45,16 +67,25 @@ func (xls *Excel) Save(filename, password *string, vm *goja.Runtime) error {
|
||||
return xls.excel.SaveAs(xls.filename, excelize.Options{Password: xls.password})
|
||||
}
|
||||
|
||||
func (xls *Excel) WriteToBuffer() ([]byte, error) {
|
||||
buf, err := xls.excel.WriteToBuffer()
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
var isIntMatcher = regexp.MustCompile(`^\d{1,8}$`)
|
||||
var isFloatMatcher = regexp.MustCompile(`^[\d.]{1,8}$`)
|
||||
|
||||
func (xls *Excel) Set(sheetName string, table [][]any, start, end *string) error {
|
||||
sheetName = xls.GetSheetName(sheetName)
|
||||
if sheetName == "" {
|
||||
sheetName = "Sheet1"
|
||||
}
|
||||
if isIntMatcher.MatchString(sheetName) {
|
||||
sheetName = xls.excel.GetSheetName(u.Int(sheetName))
|
||||
} else {
|
||||
if idx, err := xls.excel.GetSheetIndex(sheetName); err != nil || idx == -1 {
|
||||
if _, err = xls.excel.NewSheet(sheetName); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
startX, startY := ParseCellID(u.String(start))
|
||||
endX, endY := ParseCellID(u.String(end))
|
||||
for y, row := range table {
|
||||
@ -75,71 +106,15 @@ func (xls *Excel) Set(sheetName string, table [][]any, start, end *string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (xls *Excel) RemoveSheet(sheetName string) error {
|
||||
sheetName = xls.GetSheetName(sheetName)
|
||||
return xls.excel.DeleteSheet(sheetName)
|
||||
}
|
||||
|
||||
func (xls *Excel) Sheets() []string {
|
||||
return xls.excel.GetSheetList()
|
||||
}
|
||||
|
||||
func (xls *Excel) SetColWidth(sheetName string, startCol string, endCol string, width float64) error {
|
||||
sheetName = xls.GetSheetName(sheetName)
|
||||
return xls.excel.SetColWidth(sheetName, startCol, endCol, width)
|
||||
}
|
||||
|
||||
func (xls *Excel) SetColWidths(sheetName string, widths []float64) error {
|
||||
sheetName = xls.GetSheetName(sheetName)
|
||||
for i, w := range widths {
|
||||
col := MakeColID(i)
|
||||
if err := xls.excel.SetColWidth(sheetName, col, col, w); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (xls *Excel) MakeStyle(style excelize.Style) (int, error) {
|
||||
fmt.Println(u.JsonP(style), 111)
|
||||
return xls.excel.NewStyle(&style)
|
||||
}
|
||||
|
||||
func (xls *Excel) SetCellStyle(sheetName string, start string, end string, styleID int) error {
|
||||
sheetName = xls.GetSheetName(sheetName)
|
||||
return xls.excel.SetCellStyle(sheetName, start, end, styleID)
|
||||
}
|
||||
|
||||
func (xls *Excel) SetPanes(sheetName string, panes excelize.Panes) error {
|
||||
sheetName = xls.GetSheetName(sheetName)
|
||||
return xls.excel.SetPanes(sheetName, &panes)
|
||||
}
|
||||
|
||||
func (xls *Excel) SetAutoFilter(sheetName string, start, end string, option *[]excelize.AutoFilterOptions) error {
|
||||
sheetName = xls.GetSheetName(sheetName)
|
||||
var opt []excelize.AutoFilterOptions
|
||||
if option != nil {
|
||||
opt = *option
|
||||
}
|
||||
return xls.excel.AutoFilter(sheetName, start+":"+end, opt)
|
||||
}
|
||||
|
||||
func (xls *Excel) GetSheetName(sheetName string) string {
|
||||
if sheetName == "" {
|
||||
sheetName = "Sheet1"
|
||||
}
|
||||
if isIntMatcher.MatchString(sheetName) {
|
||||
sheetName = xls.excel.GetSheetName(u.Int(sheetName))
|
||||
} else {
|
||||
if idx, err := xls.excel.GetSheetIndex(sheetName); err != nil || idx == -1 {
|
||||
_, _ = xls.excel.NewSheet(sheetName)
|
||||
}
|
||||
}
|
||||
return sheetName
|
||||
}
|
||||
|
||||
func (xls *Excel) Get(sheetName string, start, end *string) ([][]any, error) {
|
||||
sheetName = xls.GetSheetName(sheetName)
|
||||
if sheetName >= "0" && sheetName <= "9" {
|
||||
sheetName = xls.excel.GetSheetName(u.Int(sheetName))
|
||||
}
|
||||
|
||||
rows, err := xls.excel.GetRows(sheetName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -286,16 +261,6 @@ func MakeCellID(col, row int) string {
|
||||
return fmt.Sprint(colName, row+1)
|
||||
}
|
||||
|
||||
func MakeColID(col int) string {
|
||||
colName := ""
|
||||
for col >= 0 {
|
||||
i := col % 26
|
||||
colName = string(rune(i+65)) + colName
|
||||
col = col/26 - 1
|
||||
}
|
||||
return fmt.Sprint(colName)
|
||||
}
|
||||
|
||||
func ParseCellID(cell string) (col, row int) {
|
||||
row = 0
|
||||
col = 0
|
||||
|
||||
38
go.mod
38
go.mod
@ -1,32 +1,32 @@
|
||||
module apigo.cc/gojs/office
|
||||
|
||||
go 1.24.0
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
apigo.cc/gojs v0.0.28
|
||||
apigo.cc/gojs/console v0.0.3
|
||||
apigo.cc/gojs/file v0.0.6
|
||||
github.com/ssgo/u v1.7.23
|
||||
github.com/xuri/excelize/v2 v2.10.0
|
||||
apigo.cc/gojs v0.0.6
|
||||
apigo.cc/gojs/console v0.0.2
|
||||
apigo.cc/gojs/file v0.0.2
|
||||
github.com/ssgo/u v1.7.11
|
||||
github.com/xuri/excelize/v2 v2.9.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/dlclark/regexp2 v1.11.5 // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/dlclark/regexp2 v1.11.4 // indirect
|
||||
github.com/fsnotify/fsnotify v1.8.0 // indirect
|
||||
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
|
||||
github.com/google/pprof v0.0.0-20250903194437-c28834ac2320 // indirect
|
||||
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
|
||||
github.com/richardlehane/mscfb v1.0.4 // indirect
|
||||
github.com/richardlehane/msoleps v1.0.4 // indirect
|
||||
github.com/ssgo/config v1.7.10 // indirect
|
||||
github.com/ssgo/log v1.7.9 // indirect
|
||||
github.com/ssgo/config v1.7.9 // indirect
|
||||
github.com/ssgo/log v1.7.7 // indirect
|
||||
github.com/ssgo/standard v1.7.7 // indirect
|
||||
github.com/ssgo/tool v0.4.29 // indirect
|
||||
github.com/tiendc/go-deepcopy v1.7.1 // indirect
|
||||
github.com/xuri/efp v0.0.1 // indirect
|
||||
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 // indirect
|
||||
golang.org/x/crypto v0.44.0 // indirect
|
||||
golang.org/x/net v0.47.0 // indirect
|
||||
golang.org/x/sys v0.38.0 // indirect
|
||||
golang.org/x/text v0.31.0 // indirect
|
||||
github.com/ssgo/tool v0.4.27 // indirect
|
||||
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d // indirect
|
||||
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect
|
||||
golang.org/x/crypto v0.29.0 // indirect
|
||||
golang.org/x/net v0.31.0 // indirect
|
||||
golang.org/x/sys v0.27.0 // indirect
|
||||
golang.org/x/text v0.20.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user