package excel import ( "github.com/xuri/excelize/v2" "github.com/yaoapp/gou/process" "github.com/yaoapp/kun/exception" ) func init() { process.RegisterGroup("excel", map[string]process.Handler{ "open": processOpen, "close": processClose, "save": processSave, "sheets": processSheets, "read.cell": processReadCell, "write.cell": processWriteCell, "set.style": processSetStyle, "set.formula": processSetFormula, "set.link": processSetLink, "set.mergecell": processMergeCell, "set.unmergecell": processUnmergeCell, "convert.columnnametonumber": processColumnNameToNumber, "convert.columnnumbertoname": processColumnNumberToName, "convert.cellnametocoordinates": processCellNameToCoordinates, "convert.coordinatestocellname": processCoordinatesToCellName, }) } // processOpen process the excel.open func processOpen(process *process.Process) interface{} { process.ValidateArgNums(1) file := process.ArgsString(0) readonly := false if len(process.Args) > 1 { readonly = process.ArgsBool(1) } handle, err := Open(file, readonly) if err != nil { exception.New("excel.open %s error: %s", 500, file, err.Error()).Throw() } return handle } // processClose process the excel.close func processClose(process *process.Process) interface{} { process.ValidateArgNums(1) handle := process.ArgsString(0) err := Close(handle) if err != nil { exception.New("excel.close %s error: %s", 500, handle, err.Error()).Throw() } return nil } // processSave process the excel.save func processSave(process *process.Process) interface{} { process.ValidateArgNums(1) handle := process.ArgsString(0) xls, err := Get(handle) if err != nil { exception.New("excel.save %s error: %s", 500, handle, err.Error()).Throw() } err = xls.Save() if err != nil { exception.New("excel.save %s error: %s", 500, handle, err.Error()).Throw() } return nil } // processSheets process the excel.sheets func processSheets(process *process.Process) interface{} { process.ValidateArgNums(1) handle := process.ArgsString(0) xls, err := Get(handle) if err != nil { exception.New("excel.sheets %s error: %s", 500, handle, err.Error()).Throw() } return xls.GetSheetList() } // processReadCell process the excel.read.cell func processReadCell(process *process.Process) interface{} { process.ValidateArgNums(3) handle := process.ArgsString(0) sheet := process.ArgsString(1) cell := process.ArgsString(2) xls, err := Get(handle) if err != nil { exception.New("excel.read.cell %s error: %s", 500, handle, err.Error()).Throw() } value, err := xls.GetCellValue(sheet, cell) if err != nil { exception.New("excel.read.cell %s:%s:%s error: %s", 500, handle, sheet, cell, err.Error()).Throw() } return value } // processWriteCell process the excel.write.cell func processWriteCell(process *process.Process) interface{} { process.ValidateArgNums(4) handle := process.ArgsString(0) sheet := process.ArgsString(1) cell := process.ArgsString(2) value := process.Args[3] xls, err := Get(handle) if err != nil { exception.New("excel.write.cell %s error: %s", 500, handle, err.Error()).Throw() } err = xls.SetCellValue(sheet, cell, value) if err != nil { exception.New("excel.write.cell %s:%s:%s error: %s", 500, handle, sheet, cell, err.Error()).Throw() } return nil } // processSetStyle process the excel.set.style