'Bug golang on goroutines | Reading and writing in Excel
I have a big problem with a go script and despite my research I'm stuck.. I make a script that reads Excel sheets or other Excel sheets are filled in, and so for each of these sheets I retrieve the information then write it in an Excel file that I create. I have a version that works but as soon as I change the Excel package it doesn't work.
I get error messages like this:
goroutine 20151 [chan send]:
FilesDIR/pkg.CompilerFicheAppuiFt.func1()
C:/Users/doria/go/src/FilesDIR/pkg/compiler.go:130 +0x8c
created by FilesDIR/pkg.CompilerFicheAppuiFt
C:/Users/doria/go/src/FilesDIR/pkg/compiler.go:126 +0xa77
goroutine 20152 [chan send]:
FilesDIR/pkg.CompilerFicheAppuiFt.func1()
C:/Users/doria/go/src/FilesDIR/pkg/compiler.go:130 +0x8c
created by FilesDIR/pkg.CompilerFicheAppuiFt
C:/Users/doria/go/src/FilesDIR/pkg/compiler.go:126 +0xa77
goroutine 20153 [chan send]:
FilesDIR/pkg.CompilerFicheAppuiFt.func1()
C:/Users/doria/go/src/FilesDIR/pkg/compiler.go:130 +0x8c
created by FilesDIR/pkg.CompilerFicheAppuiFt
C:/Users/doria/go/src/FilesDIR/pkg/compiler.go:126 +0xa77
goroutine 20154 [chan send]:
FilesDIR/pkg.CompilerFicheAppuiFt.func1()
C:/Users/doria/go/src/FilesDIR/pkg/compiler.go:130 +0x8c
created by FilesDIR/pkg.CompilerFicheAppuiFt
C:/Users/doria/go/src/FilesDIR/pkg/compiler.go:126 +0xa77
goroutine 20155 [chan send]:
FilesDIR/pkg.CompilerFicheAppuiFt.func1()
C:/Users/doria/go/src/FilesDIR/pkg/compiler.go:130 +0x8c
created by FilesDIR/pkg.CompilerFicheAppuiFt
C:/Users/doria/go/src/FilesDIR/pkg/compiler.go:126 +0xa77
The problem is that I need to change the package because I need to get the Cell Styles and currently I can't do it. Does anyone have any idea what the problem is please? thanks in advance.
the code that works:
package pkg
import (
"FilesDIR/display"
"FilesDIR/globals"
"FilesDIR/loger"
"FilesDIR/task"
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/tealeg/xlsx"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
type compilData struct {
Path string
Id int
}
var (
wg sync.WaitGroup
jobs = make(chan compilData)
Wb = &excelize.File{}
Id int
)
//...
// ACTIONS:
func ClsTempFiles() {
_ = os.RemoveAll(globals.FolderLogs)
_ = os.RemoveAll(globals.FolderDumps)
_ = os.RemoveAll(globals.FolderExports)
}
func CompilerFicheAppuiFt(path string) {
path = "C:\\Users\\doria\\FilesDIR\\Nouveau dossier"
loger.BlankDateln(display.DrawInitCompiler())
time.Sleep(800 * time.Millisecond)
loger.Blankln(display.DrawRunCompiler())
Id = 1
Wb = excelize.NewFile()
_ = Wb.SetCellValue("Sheet1", "A1", "Chemin de la fiche")
_ = Wb.SetCellValue("Sheet1", "B1", "Adresse")
_ = Wb.SetCellValue("Sheet1", "C1", "Ville")
_ = Wb.SetCellValue("Sheet1", "D1", "Num appui")
_ = Wb.SetCellValue("Sheet1", "E1", "Type appui")
_ = Wb.SetCellValue("Sheet1", "F1", "Type_n_app")
_ = Wb.SetCellValue("Sheet1", "G1", "Nature TVX")
_ = Wb.SetCellValue("Sheet1", "H1", "Etiquette jaune")
_ = Wb.SetCellValue("Sheet1", "I1", "Effort avant ajout câble")
_ = Wb.SetCellValue("Sheet1", "J1", "Effort après ajout câble")
_ = Wb.SetCellValue("Sheet1", "K1", "Effort nouveau appui")
_ = Wb.SetCellValue("Sheet1", "L1", "Latitude")
_ = Wb.SetCellValue("Sheet1", "M1", "Longitude")
_ = Wb.SetCellValue("Sheet1", "N1", "Opérateur")
_ = Wb.SetCellValue("Sheet1", "O1", "Appui utilisable en l'état")
_ = Wb.SetCellValue("Sheet1", "P1", "Environnement")
_ = Wb.SetCellValue("Sheet1", "Q1", "Commentaire appui")
_ = Wb.SetCellValue("Sheet1", "R1", "Commentaire global")
_ = Wb.SetCellValue("Sheet1", "S1", "Proxi ENEDIS")
_ = Wb.SetCellValue("Sheet1", "T1", "id_metier_")
_ = Wb.SetCellValue("Sheet1", "U1", "Date")
_ = Wb.SetCellValue("Sheet1", "V1", "PB")
for w := 1; w <= 10; w++ {
go workerFicheAppuiFt()
}
files, err := ioutil.ReadDir(path)
if err != nil {
loger.Crashln(fmt.Sprintf("Crash with this path: %s", path))
}
for _, file := range files {
if !file.IsDir() && !strings.Contains(file.Name(), "__COMPILATION__") {
excelFile := filepath.Join(path, file.Name())
f, err := xlsx.OpenFile(excelFile)
if err != nil {
loger.Errorln(fmt.Sprintf("Crash with this files: %s", excelFile))
continue
}
sht := f.Sheets[0]
maxRow := sht.MaxRow
for i := 0; i < maxRow; i++ {
row, err := sht.Row(i)
if err != nil {
panic(err)
}
go func() {
wg.Add(1)
Id++
jobs <- compilData{
Path: row.GetCell(3).String(),
Id: Id,
}
}()
}
}
}
wg.Wait()
time.Sleep(1 * time.Second)
loger.BlankDateln(display.DrawEndCompiler())
loger.BlankDateln(fmt.Sprintf("Nombre de fiches compilées : %v", Id-1))
time.Sleep(800 * time.Millisecond)
if err := Wb.SaveAs(filepath.Join(path, fmt.Sprintf("__COMPILATION__%v.xlsx", time.Now().Format("20060102150405")))); err != nil {
fmt.Println(err)
}
loger.Blankln(display.DrawSaveExcel())
fmt.Println()
time.Sleep(200 * time.Millisecond)
}
//...
//WORKER:
func workerFicheAppuiFt() {
for job := range jobs {
loger.BlankDateln(fmt.Sprintf("N°%v | Files: %s", job.Id, filepath.Base(job.Path)))
excelFile := job.Path
f, err := excelize.OpenFile(excelFile)
if err != nil {
loger.Errorln(fmt.Sprintf("Crash with this files: %s", filepath.Base(excelFile)))
wg.Done()
continue
}
sht := f.GetSheetName(f.GetActiveSheetIndex())
adresse, _ := f.GetCellValue(sht, "D5")
ville, _ := f.GetCellValue(sht, "D4")
numAppui, _ := f.GetCellValue(sht, "D3")
type1, _ := f.GetCellValue(sht, "C26")
typeNApp, _ := f.GetCellValue(sht, "M52")
natureTvx, _ := f.GetCellValue(sht, "M53")
etiquetteJaune, _ := f.GetCellValue(sht, "U12")
switch task.StrToLower(etiquetteJaune) {
case "oui":
etiquetteJaune = "non"
case "non":
etiquetteJaune = "oui"
}
effort1, _ := f.GetCellValue(sht, "S26")
effort2, _ := f.GetCellValue(sht, "U26")
effort3, _ := f.GetCellValue(sht, "W26")
lat, _ := f.GetCellValue(sht, "P5")
lon, _ := f.GetCellValue(sht, "P6")
operateur, _ := f.GetCellValue(sht, "J3")
utilisableEnEtat, _ := f.GetCellValue(sht, "W12")
environnement, _ := f.GetCellValue(sht, "W52")
commentaireEtatAppui, _ := f.GetCellValue(sht, "F13")
commentaireGlobal, _ := f.GetCellValue(sht, "A55")
proxiEnedis, _ := f.GetCellValue(sht, "W53")
insee, _ := f.GetCellValue(sht, "V4")
idMetier := fmt.Sprintf("%s/%s", numAppui, insee)
date, _ := f.GetCellValue(sht, "T1")
pb, _ := f.GetCellValue(sht, "N18")
// insert value
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("A%v", job.Id), job.Path)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("B%v", job.Id), adresse)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("C%v", job.Id), ville)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("D%v", job.Id), numAppui)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("E%v", job.Id), type1)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("F%v", job.Id), typeNApp)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("G%v", job.Id), natureTvx)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("H%v", job.Id), etiquetteJaune)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("I%v", job.Id), effort1)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("J%v", job.Id), effort2)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("K%v", job.Id), effort3)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("L%v", job.Id), lat)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("M%v", job.Id), lon)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("N%v", job.Id), operateur)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("O%v", job.Id), utilisableEnEtat)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("P%v", job.Id), environnement)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("Q%v", job.Id), commentaireEtatAppui)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("R%v", job.Id), commentaireGlobal)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("S%v", job.Id), proxiEnedis)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("T%v", job.Id), idMetier)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("U%v", job.Id), date)
_ = Wb.SetCellValue("Sheet1", fmt.Sprintf("V%v", job.Id), pb)
err = f.Close()
if err != nil {
loger.Errorln(fmt.Sprintf("Crash with this files: %s", excelFile))
continue
}
wg.Done()
}
}
and the one that refuses:
package pkg
import (
"FilesDIR/display"
"FilesDIR/globals"
"FilesDIR/loger"
"FilesDIR/task"
"fmt"
"github.com/tealeg/xlsx"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
type compilData struct {
Path string
Id int
}
var (
wg sync.WaitGroup
jobs = make(chan compilData)
Wb = &xlsx.File{}
Sht = &xlsx.Sheet{}
Id int
)
//...
// ACTIONS:
func ClsTempFiles() {
_ = os.RemoveAll(globals.FolderLogs)
_ = os.RemoveAll(globals.FolderDumps)
_ = os.RemoveAll(globals.FolderExports)
}
func CompilerFicheAppuiFt(path string) {
path = "C:\\Users\\doria\\FilesDIR\\Nouveau dossier"
loger.BlankDateln(display.DrawInitCompiler())
time.Sleep(800 * time.Millisecond)
loger.Blankln(display.DrawRunCompiler())
Id = 1
Wb = xlsx.NewFile()
Sht, _ = Wb.AddSheet("Sheet1")
col0, _ := Sht.Cell(0, 0)
col0.SetValue("Chemin de la fiche")
col1, _ := Sht.Cell(0, 1)
col1.SetValue("Adresse")
col2, _ := Sht.Cell(0, 2)
col2.SetValue("Ville")
col3, _ := Sht.Cell(0, 3)
col3.SetValue("Num appui")
col4, _ := Sht.Cell(0, 4)
col4.SetValue("Type appui")
col5, _ := Sht.Cell(0, 5)
col5.SetValue("Type_n_app")
col6, _ := Sht.Cell(0, 6)
col6.SetValue("Nature TVX")
col7, _ := Sht.Cell(0, 7)
col7.SetValue("Etiquette jaune")
col8, _ := Sht.Cell(0, 8)
col8.SetValue("Effort avant ajout câble")
col9, _ := Sht.Cell(0, 9)
col9.SetValue("Effort après ajout câble")
col10, _ := Sht.Cell(0, 10)
col10.SetValue("Effort nouveau appui")
col11, _ := Sht.Cell(0, 11)
col11.SetValue("Latitude")
col12, _ := Sht.Cell(0, 12)
col12.SetValue("Longitude")
col13, _ := Sht.Cell(0, 13)
col13.SetValue("Opérateur")
col14, _ := Sht.Cell(0, 14)
col14.SetValue("Appui utilisable en l'état")
col15, _ := Sht.Cell(0, 15)
col15.SetValue("Environnement")
col16, _ := Sht.Cell(0, 16)
col16.SetValue("Commentaire appui")
col17, _ := Sht.Cell(0, 17)
col17.SetValue("Commentaire global")
col18, _ := Sht.Cell(0, 18)
col18.SetValue("Proxi ENEDIS")
col19, _ := Sht.Cell(0, 19)
col19.SetValue("id_metier_")
col20, _ := Sht.Cell(0, 20)
col20.SetValue("Date")
col21, _ := Sht.Cell(0, 21)
col21.SetValue("PB")
for w := 1; w <= 10; w++ {
go workerFicheAppuiFt()
}
files, err := ioutil.ReadDir(path)
if err != nil {
loger.Crashln(fmt.Sprintf("Crash with this path: %s", path))
}
for _, file := range files {
if !file.IsDir() && !strings.Contains(file.Name(), "__COMPILATION__") {
excelFile := filepath.Join(path, file.Name())
f, err := xlsx.OpenFile(excelFile)
if err != nil {
loger.Errorln(fmt.Sprintf("Crash with this files: %s", excelFile))
continue
}
sht := f.Sheets[0]
maxRow := sht.MaxRow
for i := 0; i < maxRow; i++ {
row, err := sht.Row(i)
if err != nil {
panic(err)
}
go func() {
wg.Add(1)
Id++
jobs <- compilData{
Path: row.GetCell(3).String(),
Id: Id,
}
}()
}
}
}
wg.Wait()
time.Sleep(1 * time.Second)
loger.BlankDateln(display.DrawEndCompiler())
loger.BlankDateln(fmt.Sprintf("Nombre de fiches compilées : %v", Id-1))
time.Sleep(800 * time.Millisecond)
if err := Wb.Save(filepath.Join(path, fmt.Sprintf("__COMPILATION__%v.xlsx", time.Now().Format("20060102150405")))); err != nil {
fmt.Println(err)
}
loger.Blankln(display.DrawSaveExcel())
fmt.Println()
time.Sleep(200 * time.Millisecond)
}
//...
//WORKER:
func workerFicheAppuiFt() {
for job := range jobs {
loger.BlankDateln(fmt.Sprintf("N°%v | Files: %s", job.Id, filepath.Base(job.Path)))
excelFile := job.Path
f, err := xlsx.OpenFile(excelFile)
if err != nil {
loger.Errorln(fmt.Sprintf("Crash with this files: %s", filepath.Base(excelFile)))
wg.Done()
continue
}
sht := f.Sheets[0]
adresse, _ := sht.Cell(4, 3)
ville, _ := sht.Cell(3, 3)
numAppui, _ := sht.Cell(2, 3)
type1, _ := sht.Cell(25, 2)
typeNApp, _ := sht.Cell(51, 12)
natureTvx, _ := sht.Cell(52, 12)
cellEtiquetteJaune, _ := sht.Cell(11, 20)
etiquetteJaune := ""
switch task.StrToLower(cellEtiquetteJaune.Value) {
case "oui":
etiquetteJaune = "non"
case "non":
etiquetteJaune = "oui"
}
effort1, _ := sht.Cell(25, 18)
effort2, _ := sht.Cell(25, 20)
effort3, _ := sht.Cell(25, 22)
lat, _ := sht.Cell(4, 15)
lon, _ := sht.Cell(5, 15)
operateur, _ := sht.Cell(2, 9)
utilisableEnEtat, _ := sht.Cell(11, 22)
environnement, _ := sht.Cell(51, 22)
commentaireEtatAppui, _ := sht.Cell(12, 5)
commentaireGlobal, _ := sht.Cell(54, 0)
proxiEnedis, _ := sht.Cell(52, 22)
insee, _ := sht.Cell(3, 21)
idMetier := fmt.Sprintf("%s/%s", numAppui.Value, insee.Value)
date, _ := sht.Cell(0, 19)
pb, _ := sht.Cell(17, 13)
// insert value
col0, _ := Sht.Cell(job.Id, 0)
col0.SetValue(job.Path)
col1, _ := Sht.Cell(job.Id, 1)
col1.SetValue(adresse.Value)
col2, _ := Sht.Cell(job.Id, 2)
col2.SetValue(ville.Value)
col3, _ := Sht.Cell(job.Id, 3)
col3.SetValue(numAppui.Value)
col4, _ := Sht.Cell(job.Id, 4)
col4.SetValue(type1.Value)
col5, _ := Sht.Cell(job.Id, 5)
col5.SetValue(typeNApp.Value)
col6, _ := Sht.Cell(job.Id, 6)
col6.SetValue(natureTvx.Value)
col7, _ := Sht.Cell(job.Id, 7)
col7.SetValue(etiquetteJaune)
col8, _ := Sht.Cell(job.Id, 8)
col8.SetValue(effort1.Value)
col9, _ := Sht.Cell(job.Id, 9)
col9.SetValue(effort2.Value)
col10, _ := Sht.Cell(job.Id, 10)
col10.SetValue(effort3.Value)
col11, _ := Sht.Cell(job.Id, 11)
col11.SetValue(lat.Value)
col12, _ := Sht.Cell(job.Id, 12)
col12.SetValue(lon.Value)
col13, _ := Sht.Cell(job.Id, 13)
col13.SetValue(operateur.Value)
col14, _ := Sht.Cell(job.Id, 14)
col14.SetValue(utilisableEnEtat.Value)
col15, _ := Sht.Cell(job.Id, 15)
col15.SetValue(environnement.Value)
col16, _ := Sht.Cell(job.Id, 16)
col16.SetValue(commentaireEtatAppui.Value)
col17, _ := Sht.Cell(job.Id, 17)
col17.SetValue(commentaireGlobal.Value)
col18, _ := Sht.Cell(job.Id, 18)
col18.SetValue(proxiEnedis.Value)
col19, _ := Sht.Cell(job.Id, 19)
col19.SetValue(idMetier)
col20, _ := Sht.Cell(job.Id, 20)
col20.SetValue(date.Value)
col21, _ := Sht.Cell(job.Id, 21)
col21.SetValue(pb.Value)
wg.Done()
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
