Skip to content

Instantly share code, notes, and snippets.

@mishazapl
Created October 4, 2018 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishazapl/49c223db17a92ef736bfd031c078b1e8 to your computer and use it in GitHub Desktop.
Save mishazapl/49c223db17a92ef736bfd031c078b1e8 to your computer and use it in GitHub Desktop.
package errors
import "fmt"
type errorsImport struct{}
func (e errorsImport) OutRange(message string) {
fmt.Println("Out range" + message)
}
func (e errorsImport) SendUserError(error string) {
// отправка на страницу ошибки.
}
package _import
import (
"github.com/speedata/goxlsx"
"mini-project/app/vendor_packages"
"sync"
"os"
)
type ExcelImport struct {
File *goxlsx.Spreadsheet
Err vendor_packages.Err
Mux sync.Mutex
SaveChan chan map[int][]string
SkipColumn int
}
var ExcelImp ExcelImport
func (i ExcelImport) Start(path string, sChan chan map[int][]string) {
ExcelImp.SaveChan = sChan
ExcelImp.SetFile(path)
go ExcelImp.CallChannelReadExcel()
}
func (i ExcelImport) SetFile(path string) {
spreadsheet, err := goxlsx.OpenFile(path)
i.Err.FatalErr(err)
ExcelImp.File = spreadsheet
defer os.Remove(path)
}
func (i ExcelImport) CallChannelReadExcel() {
ch := make(chan goxlsx.Worksheet)
go ExcelImp.ReadWorkSheet(ch)
go ExcelImp.WatchChannel(ch)
}
func (i ExcelImport) ReadWorkSheet(ch chan goxlsx.Worksheet) {
num := ExcelImp.File.NumWorksheets()
go func() {
for c := 0; c < num; c++ {
ws := ExcelImp.getWorkSheet(c)
ExcelImp.setSkipHeader(c)
ch <- *ws
}
}()
}
func (i ExcelImport) getWorkSheet(c int) *goxlsx.Worksheet {
ExcelImp.Mux.Lock()
ws, _ := ExcelImp.File.GetWorksheet(c)
ExcelImp.Mux.Unlock()
//ExcelImp.Err.FatalErr(err)
return ws
}
func (i ExcelImport) setSkipHeader(c int) {
if c <= 0 {
ExcelImp.SkipColumn = 2
} else {
ExcelImp.SkipColumn = 0
}
}
func (i ExcelImport) WatchChannel(ch chan goxlsx.Worksheet) {
go func() {
for {
select {
case sheet := <-ch:
go ExcelImp.ReadSheet(sheet)
}
}
}()
}
func (i ExcelImport) ReadSheet(sheet goxlsx.Worksheet) {
go func() {
var row []string
rows := make(map[int][]string)
for i := ExcelImp.SkipColumn; i <= sheet.MaxRow; i++ {
for c := 0; c <= sheet.MaxColumn; c++ {
row = append(row, sheet.Cell(c, i))
}
rows[i] = row
}
ExcelImp.SaveChan <- rows
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment