Add excel export
This commit is contained in:
parent
f4a70121b8
commit
d2ff1bcc80
9 changed files with 179 additions and 13 deletions
|
|
@ -2,14 +2,17 @@ package cli
|
|||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"ronnyfriedland/timetracker/v2/internal/excel"
|
||||
"ronnyfriedland/timetracker/v2/internal/logic"
|
||||
)
|
||||
|
||||
const dateLayout = "02.01.2006"
|
||||
const timeLayout = "15:04:05"
|
||||
|
||||
func Run(configPath *string) {
|
||||
func Run(configPath *string, archiveData *bool) {
|
||||
duration := logic.Execute(configPath)
|
||||
|
||||
if duration.Complete {
|
||||
log.Printf("[%s] - Work duration: %2.2fh",
|
||||
duration.Date.Format(dateLayout),
|
||||
|
|
@ -20,5 +23,8 @@ func Run(configPath *string) {
|
|||
duration.StartTime.Format(timeLayout),
|
||||
duration.EndTime.Format(timeLayout))
|
||||
|
||||
if *archiveData {
|
||||
excel.Export(configPath, duration)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,8 +16,9 @@ func TestRunNotComplete(t *testing.T) {
|
|||
log.SetOutput(&logContent)
|
||||
|
||||
directory, _ := createStatusFile()
|
||||
archiveData := false
|
||||
|
||||
Run(&directory)
|
||||
Run(&directory, &archiveData)
|
||||
|
||||
if logContent.String() != "" {
|
||||
log.Fatalf("Expected empty logmessage")
|
||||
|
|
@ -31,10 +32,11 @@ func TestRunComplete(t *testing.T) {
|
|||
log.SetOutput(&logContent)
|
||||
|
||||
directory, fileName := createStatusFile()
|
||||
archiveData := false
|
||||
|
||||
setModificationDate(fileName, "28.02.2022")
|
||||
|
||||
Run(&directory)
|
||||
Run(&directory, &archiveData)
|
||||
|
||||
if logContent.String() == "" {
|
||||
log.Fatalf("Expected logmessage")
|
||||
|
|
|
|||
61
internal/excel/excel.go
Normal file
61
internal/excel/excel.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package excel
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/xuri/excelize/v2"
|
||||
|
||||
"ronnyfriedland/timetracker/v2/internal/logic"
|
||||
)
|
||||
|
||||
var SheetName = time.Now().Format("2006")
|
||||
var Headers = []string{"Date", "From", "To", "Duration"}
|
||||
|
||||
const dateLayout = "02.01.2006"
|
||||
const timeLayout = "15:04:05"
|
||||
|
||||
func Export(configPath *string, duration logic.Duration) string {
|
||||
archiveDataFile := *configPath + "/timetracker.xlsx"
|
||||
|
||||
file, err := excelize.OpenFile(archiveDataFile)
|
||||
if err != nil {
|
||||
file = excelize.NewFile()
|
||||
}
|
||||
defer func() {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = file.NewSheet(SheetName)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to ensure sheet created: %v", err)
|
||||
}
|
||||
file.DeleteSheet("Sheet1")
|
||||
|
||||
for i, header := range Headers {
|
||||
file.SetCellValue(SheetName, fmt.Sprintf("%s%d", string(rune(65+i)), 1), header)
|
||||
}
|
||||
|
||||
rows, err := file.GetRows(SheetName)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get rows from sheet: %v", err)
|
||||
}
|
||||
|
||||
next := len(rows) + 1
|
||||
|
||||
file.SetCellValue(SheetName, fmt.Sprintf("A%d", next), duration.Date.Format(dateLayout))
|
||||
file.SetCellValue(SheetName, fmt.Sprintf("B%d", next), duration.StartTime.Format(timeLayout))
|
||||
file.SetCellValue(SheetName, fmt.Sprintf("C%d", next), duration.EndTime.Format(timeLayout))
|
||||
file.SetCellValue(SheetName, fmt.Sprintf("D%d", next), fmt.Sprintf("%2.2f", duration.Duration.Hours()))
|
||||
|
||||
if err := file.SaveAs(archiveDataFile);
|
||||
err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return archiveDataFile
|
||||
}
|
||||
47
internal/excel/excel_test.go
Normal file
47
internal/excel/excel_test.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package excel
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"ronnyfriedland/timetracker/v2/internal/logic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
func TestExport(t *testing.T) {
|
||||
|
||||
testData := logic.Duration{
|
||||
Date: time.Now(),
|
||||
StartTime: time.Now(),
|
||||
EndTime: time.Now(),
|
||||
Duration: time.Duration(1000),
|
||||
}
|
||||
|
||||
excelDirectory := os.TempDir()
|
||||
excelFile := Export(&excelDirectory, testData)
|
||||
|
||||
file, err := excelize.OpenFile(excelFile)
|
||||
if err != nil {
|
||||
log.Fatalf("Expected excel file")
|
||||
}
|
||||
|
||||
rows, err := file.GetRows(SheetName)
|
||||
if err != nil {
|
||||
log.Fatalf("Expected no error getting rows")
|
||||
}
|
||||
|
||||
if len(rows) != 2 {
|
||||
log.Fatalf("Unexpected line count - expected 2, got %d", len(rows))
|
||||
}
|
||||
|
||||
headers := rows[0]
|
||||
for i := 0; i < len(headers); i++ {
|
||||
if headers[i] != Headers[i] {
|
||||
log.Fatalf("Unexpected header value - expected %s, got %s", Headers[i], headers[i])
|
||||
}
|
||||
}
|
||||
|
||||
defer os.Remove(excelFile)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue