Open go-zen-chu opened 1 year ago
type ExcelProcessStore func(f *excelize.File) error
type ExcelHandler struct {
ExcelConfig *config.ExcelConfig
}
func (eh *ExcelHandler) Handle(eps ExcelProcessStore) error {
f, err := excelize.OpenFile(eh.ExcelConfig.Path)
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
log.Fatalf("closing excel file: %s", err)
}
}()
return eps(f)
}
func GetExcelHandler() (*ExcelHandler, error) {
_, file, _, ok := runtime.Caller(0)
if !ok {
return nil, errors.New("could not identify caller")
}
fileName := filepath.Base(file)
// TBD
importerId := "excel/" + fileName
eh := excelHandlers[importerId]
return &eh, nil
}
---
func main() {
eh, err := importer.GetExcelHandler()
if err != nil {
log.Fatalf("get excel handler: %s", err)
}
if err := eh.Handle(func(f *excelize.File) error {
rows, err := f.GetRows("")
if err != nil {
return err
}
for idx, row := range rows {
fmt.Printf("line: %d\n", idx)
for _, colCell := range row {
fmt.Print(colCell, "\t")
}
fmt.Println()
}
return nil
}); err != nil {
log.Fatalf("get excel handler: %s", err)
}
}
func getAbsPath(configFilePath string, targetPath string) (string, error) {
absPath := targetPath
if !filepath.IsAbs(targetPath) {
parent := filepath.Dir(configFilePath)
absPath = filepath.Join(parent, targetPath)
}
if _, err := os.Stat(absPath); err != nil {
return "", err
}
return absPath, nil
}
TODO