diff --git a/Excel.go b/Excel.go index b0c175b..959c4ae 100644 --- a/Excel.go +++ b/Excel.go @@ -9,32 +9,6 @@ import ( "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 @@ -67,25 +41,16 @@ func (xls *Excel) Save(filename, password *string) 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 { - 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 - } - } - + sheetName = xls.GetSheetName(sheetName) startX, startY := ParseCellID(u.String(start)) endX, endY := ParseCellID(u.String(end)) for y, row := range table { @@ -106,15 +71,71 @@ 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) Get(sheetName string, start, end *string) ([][]any, error) { - if sheetName >= "0" && sheetName <= "9" { - sheetName = xls.excel.GetSheetName(u.Int(sheetName)) - } +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) rows, err := xls.excel.GetRows(sheetName) if err != nil { return nil, err @@ -261,6 +282,16 @@ 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 diff --git a/office.go b/office.go index ed3e68b..3691770 100644 --- a/office.go +++ b/office.go @@ -8,6 +8,7 @@ func init() { obj := map[string]any{ "openExcel": OpenExcel, "makeCellID": MakeCellID, + "makeColID": MakeColID, "parseCellID": ParseCellID, }