-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (79 loc) · 1.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"encoding/xml"
"fmt"
"log"
"os"
"path"
"path/filepath"
"github.com/dannywolfmx/cfdi-xls/sheet"
)
const DIR_NAME = "./cfdis"
type CFDI struct {
XMLName xml.Name `xml:"Comprobante"`
Version string `xml:"Version,attr"`
Receptor Receptor `xml:"Emisor"`
Folio string `xml:"Folio,attr"`
Fecha string `xml:"Fecha,attr"`
Total string `xml:"Total,attr"`
}
type Receptor struct {
RFC string `xml:"Rfc,attr"`
Nombre string `xml:"Nombre,attr"`
}
func main() {
if !directoryExist(DIR_NAME) {
ex, err := os.Executable()
if err != nil {
log.Fatal("Error al detectar el path del ejecutable")
}
path := filepath.Dir(ex)
log.Fatalf("No existe el directorio %s en la ruta actual %s", DIR_NAME, path)
}
files, err := os.ReadDir(DIR_NAME)
if err != nil {
log.Fatal(err)
}
f := sheet.NewFile("Book1.xlsx")
// Create a new sheet.
f.SetCellRight("Fecha").
SetCellRight("Nombre").
SetCellRight("Total").
SetCellRight("Folio").
SetCellRight("Version")
if f.Err != nil {
log.Fatal(f.Err)
}
for _, file := range files {
//Read the file
if file.IsDir() {
continue
}
pathFile := path.Join(DIR_NAME, file.Name())
//check if the extension is a XML
if filepath.Ext(pathFile) != ".xml" {
continue
}
content, err := os.ReadFile(pathFile)
if err != nil {
log.Fatal(err)
}
var cfdi CFDI
xml.Unmarshal(content, &cfdi)
//move to the next row down
f.MoveRowDownAndResetColumn()
f.SetCellRight(cfdi.Fecha).
SetCellRight(cfdi.Receptor.Nombre).
SetCellRight(cfdi.Total).
SetCellRight(cfdi.Folio).
SetCellRight(cfdi.Version)
}
// Save spreadsheet by the given path.
if err := f.Save(); err != nil {
fmt.Println(err)
}
}
func directoryExist(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}