Skip to content

Instantly share code, notes, and snippets.

@longfellowone
Created January 24, 2020 15:54
Show Gist options
  • Save longfellowone/ddcbfe03d2038ef8b064a3d5eeab8132 to your computer and use it in GitHub Desktop.
Save longfellowone/ddcbfe03d2038ef8b064a3d5eeab8132 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/fsnotify/fsnotify"
"gopkg.in/toast.v1"
)
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Create == fsnotify.Create && filepath.Ext(event.Name) == ".xlsx" && !strings.HasPrefix(filepath.Base(event.Name), "~$") {
// Wait for file to be written
time.Sleep(100 * time.Millisecond)
excelDoc, err := excelize.OpenFile(event.Name)
if err != nil {
log.Fatal(err)
}
WeekEnding, err := excelDoc.GetCellValue("Upload", "C2")
if err != nil {
log.Fatal("cannot get week ending date: ", err)
}
JobName, err := excelDoc.GetCellValue("Upload", "C3")
if err != nil {
log.Fatal("cannot get job name: ", err)
}
if JobName == "Click Here To Select Job" {
JobName = "NA"
}
JobNumber, err := excelDoc.GetCellValue("Upload", "C4")
if err != nil {
log.Fatal("cannot get job number: ", err)
}
JobNumber = strings.Replace(JobNumber, "/", "", -1)
currentDirectory := filepath.Dir(event.Name)
weekEndingFolderPath := filepath.Join(currentDirectory, WeekEnding)
err = os.MkdirAll(weekEndingFolderPath, os.ModePerm)
if err != nil {
log.Fatal("cannot create folder for week ending: ", err)
}
textDocFileName := JobName + "_" + JobNumber + "_" + WeekEnding + ".txt"
textDocFilePath := filepath.Join(weekEndingFolderPath, textDocFileName)
textDoc, err := os.Create(textDocFilePath)
if err != nil {
log.Fatal("cannot create file: ", err)
}
w := csv.NewWriter(textDoc)
w.Comma = '\t'
rows, err := excelDoc.GetRows("Upload")
if err != nil {
log.Fatal("cannot get rows: ", err)
}
for i, row := range rows {
// Start checking for names at B10, skip if name is empty
name := row[1]
if i >= 9 && name == "" {
continue
}
if err := w.Write(row); err != nil {
log.Fatal("error writing record to txt:", err)
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
textDoc.Close()
message := fmt.Sprintln("Text file created for:", JobName, WeekEnding, filepath.Base(event.Name))
fmt.Print(message)
notification := toast.Notification{
AppID: "Microsoft.Windows.Explorer",
Title: "Success",
Message: message,
Actions: []toast.Action{
{"protocol", "Open folder", weekEndingFolderPath},
{"protocol", "Dismiss", ""},
},
}
err = notification.Push()
if err != nil {
log.Fatal(err)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Fatal("watcher event error:", err)
}
}
}()
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
timesheetsDir := filepath.Join(homeDir, "Documents", "Timesheets")
err = os.MkdirAll(timesheetsDir, os.ModePerm)
if err != nil {
log.Fatal("cannot create folder for timesheets: ", err)
}
err = watcher.Add(timesheetsDir)
if err != nil {
log.Fatal(err)
}
fmt.Println("Watching for new files in:", timesheetsDir)
<-done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment